Skip to content
This repository has been archived by the owner on Jul 6, 2020. It is now read-only.

Commit

Permalink
Add improved warning and invariant to SchemaPredicates.isFieldNullable
Browse files Browse the repository at this point in the history
  • Loading branch information
kitten committed Sep 4, 2019
1 parent 64cc41e commit 517c33d
Showing 1 changed file with 34 additions and 11 deletions.
45 changes: 34 additions & 11 deletions src/ast/schemaPredicates.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
}
Expand Down

0 comments on commit 517c33d

Please sign in to comment.