Skip to content

Fix: Type safety for TypeInfo. #698

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

Merged
merged 1 commit into from
Jan 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions src/utilities/TypeInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import * as Kind from '../language/kinds';
import {
isCompositeType,
isInputType,
isOutputType,
getNullableType,
getNamedType,
GraphQLObjectType,
Expand Down Expand Up @@ -117,12 +119,11 @@ export class TypeInfo {
switch (node.kind) {
case Kind.SELECTION_SET:
const namedType = getNamedType(this.getType());
let compositeType: ?GraphQLCompositeType;
if (isCompositeType(namedType)) {
// isCompositeType is a type refining predicate, so this is safe.
compositeType = ((namedType: any): GraphQLCompositeType);
}
this._parentTypeStack.push(compositeType);
this._parentTypeStack.push(
isCompositeType(namedType) ?
((namedType: any): GraphQLCompositeType) :
undefined
);
break;
case Kind.FIELD:
const parentType = this.getParentType();
Expand Down Expand Up @@ -153,11 +154,19 @@ export class TypeInfo {
const outputType = typeConditionAST ?
typeFromAST(schema, typeConditionAST) :
this.getType();
this._typeStack.push(((outputType: any): GraphQLOutputType));
this._typeStack.push(
isOutputType(outputType) ?
((outputType: any): GraphQLOutputType) :
undefined
);
break;
case Kind.VARIABLE_DEFINITION:
const inputType = typeFromAST(schema, node.type);
this._inputTypeStack.push(((inputType: any): GraphQLInputType));
this._inputTypeStack.push(
isInputType(inputType) ?
((inputType: any): GraphQLInputType) :
undefined
);
break;
case Kind.ARGUMENT:
let argDef;
Expand Down
17 changes: 10 additions & 7 deletions src/validation/rules/FragmentsOnCompositeTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { GraphQLError } from '../../error';
import { print } from '../../language/printer';
import { isCompositeType } from '../../type/definition';
import type { GraphQLType } from '../../type/definition';
import { typeFromAST } from '../../utilities/typeFromAST';


export function inlineFragmentOnNonCompositeErrorMessage(
Expand All @@ -39,16 +40,18 @@ export function fragmentOnNonCompositeErrorMessage(
export function FragmentsOnCompositeTypes(context: ValidationContext): any {
return {
InlineFragment(node) {
const type = context.getType();
if (node.typeCondition && type && !isCompositeType(type)) {
context.reportError(new GraphQLError(
inlineFragmentOnNonCompositeErrorMessage(print(node.typeCondition)),
[ node.typeCondition ]
));
if (node.typeCondition) {
const type = typeFromAST(context.getSchema(), node.typeCondition);
if (type && !isCompositeType(type)) {
context.reportError(new GraphQLError(
inlineFragmentOnNonCompositeErrorMessage(print(node.typeCondition)),
[ node.typeCondition ]
));
}
}
},
FragmentDefinition(node) {
const type = context.getType();
const type = typeFromAST(context.getSchema(), node.typeCondition);
if (type && !isCompositeType(type)) {
context.reportError(new GraphQLError(
fragmentOnNonCompositeErrorMessage(
Expand Down