question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

All `astNode` fields are undefined when schema is loaded using buildClientSchema

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
tovbinmcommented, 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))
2reactions
IvanGoncharovcommented, 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));
Read more comments on GitHub >

github_iconTop Results From Across the Web

Graphql Ruby - Cannot read property 'types' of undefined at ...
The issue is with schemaIntrospection.types , schemaIntrospection is undefined. So above, I inspected the introspection variable which calls ...
Read more >
API Reference: graphql-tools - Apollo GraphQL Docs
Apollo server directly exports all the function from graphql-tools , enabling a migration ... A root field in a subschema from which the...
Read more >
graphql SelectionSetNode TypeScript Examples
The following examples show how to use graphql. ... leave(node: ASTNode) { if (!(node.kind === 'Field' || node.kind === 'FragmentDefinition')) return ...
Read more >
graphql/utilities
function buildClientSchema. Produces a client schema given the result of querying a schema with `introspectionQuery`. Schema Language. function buildSchema
Read more >
graphql-toolkit @ 0.0.5 .. 0.2.9 - Package Diff
+ if (fields.length) {. + return `schema { ${fields.join('\n')} }`;. + }. + return undefined;. +}. +function mergeGraphQLTypes(types, config) {.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found