From 517c33d84ad5e262088a030ffa7caef43955c658 Mon Sep 17 00:00:00 2001 From: Phil Pluckthun Date: Wed, 4 Sep 2019 14:01:11 +0100 Subject: [PATCH] Add improved warning and invariant to SchemaPredicates.isFieldNullable --- src/ast/schemaPredicates.ts | 45 ++++++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/src/ast/schemaPredicates.ts b/src/ast/schemaPredicates.ts index 1b222bb..279ee43 100644 --- a/src/ast/schemaPredicates.ts +++ b/src/ast/schemaPredicates.ts @@ -1,11 +1,13 @@ +import invariant from 'invariant'; +import warning from 'warning'; + import { buildClientSchema, - GraphQLSchema, isNullableType, + GraphQLSchema, GraphQLAbstractType, GraphQLObjectType, } from 'graphql'; -import invariant from 'invariant'; export class SchemaPredicates { schema: GraphQLSchema; @@ -15,19 +17,40 @@ export class SchemaPredicates { } isFieldNullable(typename: string, fieldName: string): boolean { - const objectTypeNode = this.schema.getType(typename) as GraphQLObjectType; - + const type = this.schema.getType(typename) as GraphQLObjectType; invariant( - !!objectTypeNode, - `The type ${typename} does not exist in your schema` + type instanceof GraphQLObjectType, + 'Invalid type: The type `%s` is not an object in the defined schema, ' + + 'but the GraphQL document is traversing it.', + typename ); - const field = objectTypeNode.getFields()[fieldName]; + const object = type as GraphQLObjectType; + if (object === undefined) { + warning( + false, + 'Invalid type: The type `%s` is not a type in the defined schema, ' + + 'but the GraphQL document expects it to exist.\n' + + 'Traversal will continue, however this may lead to undefined behavior!', + typename + ); - invariant( - !!field, - `The type ${typename}.${fieldName} does not exist in your schema` - ); + return false; + } + + const field = object.getFields()[fieldName]; + if (field === undefined) { + warning( + false, + 'Invalid field: The field `%s` does not exist on `%s`, ' + + 'but the GraphQL document expects it to exist.\n' + + 'Traversal will continue, however this may lead to undefined behavior!', + fieldName, + typename + ); + + return false; + } return isNullableType(field.type); }