Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

buildClientSchema fails when converting apollo introspection json #1970

Closed
tjtaill opened this issue Jun 11, 2019 · 2 comments
Closed

buildClientSchema fails when converting apollo introspection json #1970

tjtaill opened this issue Jun 11, 2019 · 2 comments
Labels

Comments

@tjtaill
Copy link

tjtaill commented Jun 11, 2019

BUG/FIX

Hi I am using a vscode plugin to do graphql autocompletes https://marketplace.visualstudio.com/items?itemName=kumar-harsh.graphql-for-vscode

It requires a gql file so I use grahpql-tools https://www.npmjs.com/package/gql-tools?activeTab=readme.

I download the json scheme
gqlschema http://example.com/graphql -i

Then I convert it to the gql using this library
in a script I call gql-json-to-sdl.js
with the following

const { buildClientSchema, printSchema } = require("graphql");
const fs = require("fs");

const introspectionSchemaResult = JSON.parse(fs.readFileSync("graphql_schema.json"));
const graphqlSchemaObj = buildClientSchema(introspectionSchemaResult);
fs.writeFileSync('graphql_schema.gql', printSchema(graphqlSchemaObj));

I get the following stack trace

TypeError: Cannot read property 'types' of undefined
at buildClientSchema (.../node_modules/graphql/utilities/buildClientSchema.js:57:61)
at Object. (.../gql-json-to-sdl.js:5:26)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)

If I change the following https://github.com/graphql/graphql-js/blob/master/src/utilities/buildClientSchema.js#L76

to

var schemaIntrospection = introspection.__schema || introspection.data.__schema;

it works wondering if that change is a valid fix for this repo or just a one
off solution for my own situation

@IvanGoncharov
Copy link
Member

@tjtaill Every graphql response including introspection one have data and errors and buildClientSchema accepts data. So your code should look like:

if (introspectionSchemaResult.errors) {
  // do something with errors
}
const graphqlSchemaObj = buildClientSchema(introspectionSchemaResult.data);

@IvanGoncharov
Copy link
Member

@tjtaill It's common issue so I added dev check (in #1974) to prevent such situations in future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants