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

Commit

Permalink
Add improved invariant to SchemaPredicates.isInterfaceOfType
Browse files Browse the repository at this point in the history
  • Loading branch information
kitten committed Sep 4, 2019
1 parent 517c33d commit 16383d4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 26 deletions.
42 changes: 30 additions & 12 deletions src/ast/schemaPredicates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import warning from 'warning';
import {
buildClientSchema,
isNullableType,
GraphQLType,
GraphQLSchema,
GraphQLAbstractType,
GraphQLObjectType,
GraphQLInterfaceType,
GraphQLUnionType,
} from 'graphql';

export class SchemaPredicates {
Expand All @@ -17,13 +20,8 @@ export class SchemaPredicates {
}

isFieldNullable(typename: string, fieldName: string): boolean {
const type = this.schema.getType(typename) as GraphQLObjectType;
invariant(
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 type = this.schema.getType(typename);
expectObjectType(type);

const object = type as GraphQLObjectType;
if (object === undefined) {
Expand Down Expand Up @@ -56,14 +54,34 @@ export class SchemaPredicates {
}

isInterfaceOfType(typeCondition: string, typename: string | void): boolean {
if (!typename) return false;
if (typename === undefined) return false;
if (typename === typeCondition) return true;

const abstractNode = this.schema.getType(
typeCondition
) as GraphQLAbstractType;
const concreteNode = this.schema.getType(typename) as GraphQLObjectType;
const abstractType = this.schema.getType(typeCondition);
expectAbstractType(type);
const objectType = this.schema.getType(typename);
expectObjectType(type);

const abstractNode = typeCondition as GraphQLAbstractType;
const concreteNode = objectType as GraphQLObjectType;
return this.schema.isPossibleType(abstractNode, concreteNode);
}
}

const expectObjectType = (type: GraphQLType) => {
invariant(
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 expectAbstractType = (type: GraphQLType) => {
invariant(
type instanceof GraphQLInterfaceType || type instanceof GraphQLUnionType,
'Invalid type: The type `%s` is not an Interface or Union type in the defined schema, ' +
'but a fragment in the GraphQL document is using it as a type condition.',
typename
);
};
26 changes: 12 additions & 14 deletions src/operations/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,18 @@ export class SelectionIterator {

if (fragmentNode !== undefined) {
const typeCondition = getTypeCondition(fragmentNode);
let isMatching;
if (this.context.schemaPredicates) {
isMatching = this.context.schemaPredicates.isInterfaceOfType(
typeCondition as string,
this.typename as string
);
} else {
isMatching = isFragmentHeuristicallyMatching(
fragmentNode,
this.typename,
this.entityKey,
this.context
);
}
const isMatching =
this.context.schemaPredicates !== undefined
? this.context.schemaPredicates.isInterfaceOfType(
getTypeCondition(fragmentNode),
this.typename
)
: isFragmentHeuristicallyMatching(
fragmentNode,
this.typename,
this.entityKey,
this.context
);

if (!isMatching) continue;

Expand Down

0 comments on commit 16383d4

Please sign in to comment.