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

All astNode fields are undefined when schema is loaded using buildClientSchema #1575

Closed
dotansimha opened this issue Nov 17, 2018 · 6 comments
Labels

Comments

@dotansimha
Copy link
Member

dotansimha commented Nov 17, 2018

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
@IvanGoncharov
Copy link
Member

IvanGoncharov commented Jan 16, 2019

@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));

@langpavel
Copy link
Contributor

langpavel commented Jan 17, 2019

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.

@mjmahone
Copy link
Contributor

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.

@IvanGoncharov
Copy link
Member

@mjmahone It's already tracked as #1343

@IvanGoncharov
Copy link
Member

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

@tovbinm
Copy link

tovbinm commented Mar 24, 2022

Our workaround was to print and reparse the schema to have astNode populated:

import {parse, printSchema} from 'graphql';

const schemaNode = parse(printSchema(schema))

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

5 participants