marmelab / graphql-schema-from-json

Guess a GraphQL schema from json data
MIT License
183 stars 24 forks source link

Error with showing results #7

Open molinto opened 7 years ago

molinto commented 7 years ago

I used the code in the example & ran 'npm start'

"start": "babel-node index.js --presets es2015,stage-2"
Now using node v8.9.0 (npm v5.5.1)
me@me-Latitude-E7470:~/Development/Projects/g$ npm start
> g@1.0.0 start /home/me/Development/Projects/g
> babel-node index.js --presets es2015,stage-2

/home/me/Development/Projects/g/node_modules/graphql/jsutils/invariant.js:18
    throw new Error(message);
    ^

Error
    at invariant (/home/me/Development/Projects/g/node_modules/graphql/jsutils/invariant.js:18:11)
    at printType (/home/me/Development/Projects/g/node_modules/graphql/utilities/schemaPrinter.js:151:83)
    at Array.map (<anonymous>)
    at printFilteredSchema (/home/me/Development/Projects/g/node_modules/graphql/utilities/schemaPrinter.js:80:87)
    at printSchema (/home/me/Development/Projects/g/node_modules/graphql/utilities/schemaPrinter.js:44:10)
    at Object.<anonymous> (/home/me/Development/Projects/g/index.js:26:13)
    at Module._compile (module.js:635:30)
    at loader (/home/me/Development/Projects/g/node_modules/babel-register/lib/node.js:144:5)
    at Object.require.extensions.(anonymous function) [as .js] (/home/me/Development/Projects/g/node_modules/babel-register/lib/node.js:154:7)
    at Module.load (module.js:554:32)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! g@1.0.0 start: `babel-node index.js --presets es2015,stage-2`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the g@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/me/.npm/_logs/2017-11-03T12_10_43_198Z-debug.log
fzaninotto commented 7 years ago

ping @djhi

djhi commented 7 years ago

Yes, that's due to conflicting references to the graphql package. Haven't found time to investigate and fix this yet

lucianlature commented 7 years ago

Different graphql packages are being used across, so a quick fix for it is to import printSchema, then make that function available in the lib, and use that specific function.

WhynoChinese commented 7 years ago

How can I get the generated schemaString ? It does not look like this format image

acusti commented 6 years ago

@djhi I ran into a similar error as the original poster even when I created a brand new project with no other dependencies. I made a repo you can use to try it out: https://github.com/acusti/graphql-schema-from-json-test/. From that repo, I’ve listed the relevant files below.

Here’s package.json:

{
  "name": "a",
  "version": "1.0.0",
  "scripts": {
    "start": "node index.js"
  },
  "dependencies": {
    "graphql-schema-from-json": "^0.0.3"
  }
}

Here’s index.js (based on the README example):

const getSchemaFromData = require('graphql-schema-from-json').default;
const { printSchema } = require('graphql');

const data = {
    posts: [
        { id: 1, title: "Lorem Ipsum", views: 254, user_id: 123 },
        { id: 2, title: "Sic Dolor amet", views: 65, user_id: 456 },
    ],
    users: [
        { id: 123, name: "John Doe" },
        { id: 456, name: "Jane Doe" }
    ],
    comments: [
        { id: 987, post_id: 1, body: "Consectetur adipiscing elit", date: new Date('2017-07-03') },
        { id: 995, post_id: 1, body: "Nam molestie pellentesque dui", date: new Date('2017-08-17') }
    ]
}

// Get the schema as a JSON object
const schema = getSchemaFromData(data);

// Print the GQL for this schema
console.log(printSchema(schema));

When I run yarn start (or npm start), I get:

/Users/andrew/Projects/a/node_modules/graphql/jsutils/invariant.js:18
    throw new Error(message);
    ^

Error
    at invariant (/a/node_modules/graphql/jsutils/invariant.js:18:11)
    at printType (/a/node_modules/graphql/utilities/schemaPrinter.js:151:83)
    at Array.map (<anonymous>)
    at printFilteredSchema (/a/node_modules/graphql/utilities/schemaPrinter.js:80:87)
    at printSchema (/a/node_modules/graphql/utilities/schemaPrinter.js:44:10)
    at Object.<anonymous> (/a/index.js:23:13)
    at Module._compile (module.js:635:30)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
error Command failed with exit code 1.

When I then run yarn list, the results show no duplicate graphql dependency:

yarn list v1.3.2
├─ graphql-schema-from-json@0.0.3
│  ├─ graphql-type-json@~0.1.4
│  ├─ graphql@~0.11.7
│  └─ inflection@~1.12.0
├─ graphql-type-json@0.1.4
├─ graphql@0.11.7
│  └─ iterall@1.1.3
├─ inflection@1.12.0
└─ iterall@1.1.3
✨  Done in 0.17s.

Platform versions:

acusti commented 6 years ago

As a follow-up, I did another test that uses yarn resolutions to make graphql-schema-from-json use the latest version of graphql, 0.12.3 (see this branch in the test repo). Here’s how I set that up in package.json:

{
  "name": "a",
  "version": "1.0.0",
  "scripts": {
    "start": "node index.js"
  },
  "dependencies": {
    "graphql": "0.12.3",
    "graphql-schema-from-json": "0.0.3"
  },
  "resolutions": {
    "graphql-schema-from-json/graphql": "0.12.3"
  }
}

Looking in node_modules, I confirmed that only one version of the graphql package is installed, v0.12.3, and that graphql-schema-from-json has no nested node_modules, so the resolutions directive worked correctly. In this case, when I run yarn start, I get:

/Users/andrew/Projects/a/node_modules/graphql/jsutils/instanceOf.js:17
      throw new Error("Cannot use " + constructor.name + " \"" + value + "\" from another module or realm.\n\nEnsure that there is only one instance of \"graphql\" in the node_modules\ndirectory. If different versions of \"graphql\" are the dependencies of other\nrelied on modules, use \"resolutions\" to ensure only one version is installed.\n\nhttps://yarnpkg.com/en/docs/selective-version-resolutions\n\nDuplicate \"graphql\" modules cannot be used at the same time since different\nversions may have different capabilities and behavior. The data from one\nversion used in the function from another could produce confusing and\nspurious results.");
      ^

Error: Cannot use GraphQLObjectType "__Directive" from another module or realm.

Ensure that there is only one instance of "graphql" in the node_modules
directory. If different versions of "graphql" are the dependencies of other
relied on modules, use "resolutions" to ensure only one version is installed.

https://yarnpkg.com/en/docs/selective-version-resolutions

Duplicate "graphql" modules cannot be used at the same time since different
versions may have different capabilities and behavior. The data from one
version used in the function from another could produce confusing and
spurious results.
    at instanceOf (/Users/andrew/Projects/a/node_modules/graphql/jsutils/instanceOf.js:17:13)
    at isObjectType (/Users/andrew/Projects/a/node_modules/graphql/type/definition.js:112:35)
    at isNamedType (/Users/andrew/Projects/a/node_modules/graphql/type/definition.js:277:32)
    at isSpecifiedScalarType (/Users/andrew/Projects/a/node_modules/graphql/type/scalars.js:126:38)
    at isDefinedType (/Users/andrew/Projects/a/node_modules/graphql/utilities/schemaPrinter.js:59:46)
    at Array.filter (<anonymous>)
    at printFilteredSchema (/Users/andrew/Projects/a/node_modules/graphql/utilities/schemaPrinter.js:69:6)
    at printSchema (/Users/andrew/Projects/a/node_modules/graphql/utilities/schemaPrinter.js:49:10)
    at Object.<anonymous> (/Users/andrew/Projects/a/index.js:23:13)
    at Module._compile (module.js:635:30)
error Command failed with exit code 1.
konsumer commented 6 years ago

I have the same issue, even when I lock in to the same version of graphql that graphql-schema-from-json uses:

package.json

{
  "name": "tools",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "genSchema": "babel-node genSchema"
  },
  "dependencies": {
    "babel-cli": "^6.26.0",
    "babel-preset-env": "^1.6.1",
    "babel-preset-stage-3": "^6.24.1",
    "graphql-schema-from-json": "0.0.3",
    "graphql": "~0.11.7"
  },
  "babel": {
    "presets": [
      "env",
      "stage-3"
    ]
  }
}

genSchema.js

import getSchemaFromData from 'graphql-schema-from-json'
import { printSchema } from 'graphql'

const data = {
  posts: [
    { id: 1, title: 'Lorem Ipsum', views: 254, user_id: 123 },
    { id: 2, title: 'Sic Dolor amet', views: 65, user_id: 456 }
  ],
  users: [
    { id: 123, name: 'John Doe' },
    { id: 456, name: 'Jane Doe' }
  ],
  comments: [
    { id: 987, post_id: 1, body: 'Consectetur adipiscing elit', date: new Date('2017-07-03') },
    { id: 995, post_id: 1, body: 'Nam molestie pellentesque dui', date: new Date('2017-08-17') }
  ]
}

// Get the schema as a JSON object
const schema = getSchemaFromData(data)

// Print the GQL for this schema
console.log(printSchema(schema))

ERROR

npm run genSchema

> tools@0.0.0 genSchema /Users/konsumer/Desktop/data-server/tools
> babel-node genSchema

/Users/konsumer/Desktop/data-server/tools/node_modules/graphql/jsutils/invariant.js:18
    throw new Error(message);
    ^

Error
    at invariant (/Users/konsumer/Desktop/data-server/tools/node_modules/graphql/jsutils/invariant.js:18:11)
    at printType (/Users/konsumer/Desktop/data-server/tools/node_modules/graphql/utilities/schemaPrinter.js:151:83)
    at Array.map (<anonymous>)
    at printFilteredSchema (/Users/konsumer/Desktop/data-server/tools/node_modules/graphql/utilities/schemaPrinter.js:80:87)
    at printSchema (/Users/konsumer/Desktop/data-server/tools/node_modules/graphql/utilities/schemaPrinter.js:44:10)
    at Object.<anonymous> (/Users/konsumer/Desktop/data-server/tools/genSchema.js:23:13)
    at Module._compile (module.js:662:30)
    at loader (/Users/konsumer/Desktop/data-server/tools/node_modules/babel-register/lib/node.js:144:5)
    at Object.require.extensions.(anonymous function) [as .js] (/Users/konsumer/Desktop/data-server/tools/node_modules/babel-register/lib/node.js:154:7)
    at Module.load (module.js:575:32)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! tools@0.0.0 genSchema: `babel-node genSchema`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the tools@0.0.0 genSchema script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/konsumer/.npm/_logs/2018-03-21T21_53_52_872Z-debug.log
konsumer commented 6 years ago

schema looks like this:

GraphQLSchema {
  _queryType: Query,
  _mutationType: Mutation,
  _subscriptionType: null,
  _directives:
   [ GraphQLDirective {
       name: 'include',
       description: 'Directs the executor to include this field or fragment only when the `if` argument is true.',
       locations: [Array],
       astNode: undefined,
       args: [Array] },
     GraphQLDirective {
       name: 'skip',
       description: 'Directs the executor to skip this field or fragment when the `if` argument is true.',
       locations: [Array],
       astNode: undefined,
       args: [Array] },
     GraphQLDirective {
       name: 'deprecated',
       description: 'Marks an element of a GraphQL schema as no longer supported.',
       locations: [Array],
       astNode: undefined,
       args: [Array] } ],
  astNode: null,
  _typeMap:
   { Query: Query,
     ID: ID,
     Post: Post,
     String: String,
     Int: Int,
     User: User,
     Comment: Comment,
     Date: Date,
     PostFilter: PostFilter,
     ListMetadata: ListMetadata,
     UserFilter: UserFilter,
     CommentFilter: CommentFilter,
     Mutation: Mutation,
     Boolean: Boolean,
     __Schema: __Schema,
     __Type: __Type,
     __TypeKind: __TypeKind,
     __Field: __Field,
     __InputValue: __InputValue,
     __EnumValue: __EnumValue,
     __Directive: __Directive,
     __DirectiveLocation: __DirectiveLocation },
  _implementations: {} }
codeguy commented 6 years ago

I am also running into this issue. Do you have an ETA on a fix?

djhi commented 6 years ago

No, but you're welcome to investigate too and send a pull request ;)

codeguy commented 6 years ago

That's fair. You should drop a note in the readme that says the project doesn't work until this issue is fixed.

konsumer commented 6 years ago

I made this, which is not as complete, but it fit my purpose and doesn't throw errors. It also works for protobuf & jsonschema.

fzaninotto commented 6 years ago

ping @djhi

keshann93 commented 6 years ago

are we having a fix for this issue?

flomair commented 6 years ago

While this is brocken i recomend this workarround: create GraphQL server from data with json-graphql-server. Extract GraphQL schema with get-graphql-schema