Graphql-js: All `astNode` fields are undefined when schema is loaded using buildClientSchema

Created on 17 Nov 2018  路  5Comments  路  Source: graphql/graphql-js

I noticed an issue with loading a GraphQLSchema using introspection JSON.

Given the following introspection JSON:
https://gist.github.com/dotansimha/c25f0ce38382086f55f5382da7dcbcb8

If I'm running the following code:

const introspection = require('./schema.json');
const { buildClientSchema } = require('graphql');
const schema = buildClientSchema(introspection);

const allTypes = schema.getTypeMap(); // Valid, all types are there
const rootNode = schema.astNode; // undefined
const allTypesAst = Object.keys(allTypes).map(key => allTypes[key].astNode); // Array of `undefined`

I know buildClientSchema should return a GraphQLSchema without resolvers and anything else, but is there a reason for removing the astNode fields?

My current workaround is to print the schema into a string, parse to into a Document using parse and then build the schema again using buildASTSchema:

const introspection = require('./schema.json');
const { buildClientSchema, buildASTSchema, parse, printSchema } = require('graphql');
const schema = buildClientSchema(introspection);

const validSchema = buildASTSchema(parse(printSchema(schema)));

const allTypesAst = Object.keys(allTypes).map(key => allTypes[key].astNode); // Valid ASTs
question

Most helpful comment

@dotansimha Sorry for the delay.

I know buildClientSchema should return a GraphQLSchema without resolvers and anything else, but is there a reason for removing the astNode fields?

astNode should always point to the original location provided by the user and intended to be used mainly for error reporting (tie error to the particular location inside user input).

Since buildClientSchema accepts JSON it would be strange to report error showing SDL and pointing to the non-existing location.

If you have a different use case for astNode can you please describe it?

const validSchema = buildASTSchema(parse(printSchema(schema)));

You can use buildSchema which is basically parse + buildASTSchema.

const validSchema = builtSchema(printSchema(schema));

All 5 comments

@dotansimha Sorry for the delay.

I know buildClientSchema should return a GraphQLSchema without resolvers and anything else, but is there a reason for removing the astNode fields?

astNode should always point to the original location provided by the user and intended to be used mainly for error reporting (tie error to the particular location inside user input).

Since buildClientSchema accepts JSON it would be strange to report error showing SDL and pointing to the non-existing location.

If you have a different use case for astNode can you please describe it?

const validSchema = buildASTSchema(parse(printSchema(schema)));

You can use buildSchema which is basically parse + buildASTSchema.

const validSchema = builtSchema(printSchema(schema));

I noticed an issue with loading a GraphQLSchema using introspection JSON.

AST refers to abstract syntax tree which is directly derived from SDL source. If you have no source, you cannot have AST.

I think that this make sense.

One thing I think we do eventually need to solve: currently the only way to access directives placed on GraphQLSchema definitions is via the definition's AST node. In this case it's a bit of a non-issue, as the introspection query doesn't expose field-definition directives anyways. But that could be a good project for someone to improve the consistency across introspection and SDL based schemas.

@mjmahone It's already tracked as #1343

@dotansimha If you want this issue to be reopened, please free to ping me.

Was this page helpful?
0 / 5 - 0 ratings