Skip to content

Commit

Permalink
Merge pull request #695 from graphql/type-checks
Browse files Browse the repository at this point in the history
Use flow %checks to reduce occurance of :any
  • Loading branch information
leebyron authored Apr 29, 2017
2 parents a9d1f20 + 333c248 commit d7fe661
Show file tree
Hide file tree
Showing 13 changed files with 105 additions and 114 deletions.
16 changes: 5 additions & 11 deletions src/execution/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,11 @@ import { typeFromAST } from '../utilities/typeFromAST';
import * as Kind from '../language/kinds';
import { getVariableValues, getArgumentValues } from './values';
import {
GraphQLScalarType,
GraphQLObjectType,
GraphQLEnumType,
GraphQLList,
GraphQLNonNull,
GraphQLInterfaceType,
GraphQLUnionType,
isAbstractType
isAbstractType,
isLeafType,
} from '../type/definition';
import type {
GraphQLType,
Expand Down Expand Up @@ -527,8 +524,7 @@ function doesFragmentConditionMatch(
return true;
}
if (isAbstractType(conditionalType)) {
const abstractType = ((conditionalType: any): GraphQLAbstractType);
return exeContext.schema.isPossibleType(abstractType, type);
return exeContext.schema.isPossibleType(conditionalType, type);
}
return false;
}
Expand Down Expand Up @@ -831,15 +827,13 @@ function completeValue(

// If field type is a leaf type, Scalar or Enum, serialize to a valid value,
// returning null if serialization is not possible.
if (returnType instanceof GraphQLScalarType ||
returnType instanceof GraphQLEnumType) {
if (isLeafType(returnType)) {
return completeLeafValue(returnType, result);
}

// If field type is an abstract type, Interface or Union, determine the
// runtime Object type and complete for that type.
if (returnType instanceof GraphQLInterfaceType ||
returnType instanceof GraphQLUnionType) {
if (isAbstractType(returnType)) {
return completeAbstractValue(
exeContext,
returnType,
Expand Down
3 changes: 1 addition & 2 deletions src/execution/values.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,14 @@ export function getVariableValues(
for (let i = 0; i < varDefNodes.length; i++) {
const varDefNode = varDefNodes[i];
const varName = varDefNode.variable.name.value;
let varType = typeFromAST(schema, varDefNode.type);
const varType = typeFromAST(schema, varDefNode.type);
if (!isInputType(varType)) {
throw new GraphQLError(
`Variable "$${varName}" expected value of type ` +
`"${print(varDefNode.type)}" which cannot be used as an input type.`,
[ varDefNode.type ]
);
}
varType = ((varType: any): GraphQLInputType);

const value = inputs[varName];
if (isInvalid(value)) {
Expand Down
70 changes: 39 additions & 31 deletions src/type/definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,13 @@ export type GraphQLInputType =
GraphQLList<GraphQLInputType>
>;

export function isInputType(type: ?GraphQLType): boolean {
const namedType = getNamedType(type);
export function isInputType(type: ?GraphQLType): boolean %checks {
return (
namedType instanceof GraphQLScalarType ||
namedType instanceof GraphQLEnumType ||
namedType instanceof GraphQLInputObjectType
type instanceof GraphQLScalarType ||
type instanceof GraphQLEnumType ||
type instanceof GraphQLInputObjectType ||
type instanceof GraphQLNonNull && isInputType(type.ofType) ||
type instanceof GraphQLList && isInputType(type.ofType)
);
}

Expand All @@ -86,7 +87,7 @@ export function assertInputType(type: ?GraphQLType): GraphQLInputType {
isInputType(type),
`Expected ${String(type)} to be a GraphQL input type.`
);
return (type: any);
return type;
}

/**
Expand All @@ -108,14 +109,15 @@ export type GraphQLOutputType =
GraphQLList<GraphQLOutputType>
>;

export function isOutputType(type: ?GraphQLType): boolean {
const namedType = getNamedType(type);
export function isOutputType(type: ?GraphQLType): boolean %checks {
return (
namedType instanceof GraphQLScalarType ||
namedType instanceof GraphQLObjectType ||
namedType instanceof GraphQLInterfaceType ||
namedType instanceof GraphQLUnionType ||
namedType instanceof GraphQLEnumType
type instanceof GraphQLScalarType ||
type instanceof GraphQLObjectType ||
type instanceof GraphQLInterfaceType ||
type instanceof GraphQLUnionType ||
type instanceof GraphQLEnumType ||
type instanceof GraphQLNonNull && isOutputType(type.ofType) ||
type instanceof GraphQLList && isOutputType(type.ofType)
);
}

Expand All @@ -124,7 +126,7 @@ export function assertOutputType(type: ?GraphQLType): GraphQLOutputType {
isOutputType(type),
`Expected ${String(type)} to be a GraphQL output type.`,
);
return (type: any);
return type;
}

/**
Expand All @@ -134,7 +136,7 @@ export type GraphQLLeafType =
GraphQLScalarType |
GraphQLEnumType;

export function isLeafType(type: ?GraphQLType): boolean {
export function isLeafType(type: ?GraphQLType): boolean %checks {
return (
type instanceof GraphQLScalarType ||
type instanceof GraphQLEnumType
Expand All @@ -146,7 +148,7 @@ export function assertLeafType(type: ?GraphQLType): GraphQLLeafType {
isLeafType(type),
`Expected ${String(type)} to be a GraphQL leaf type.`,
);
return (type: any);
return type;
}

/**
Expand All @@ -157,7 +159,7 @@ export type GraphQLCompositeType =
GraphQLInterfaceType |
GraphQLUnionType;

export function isCompositeType(type: ?GraphQLType): boolean {
export function isCompositeType(type: ?GraphQLType): boolean %checks {
return (
type instanceof GraphQLObjectType ||
type instanceof GraphQLInterfaceType ||
Expand All @@ -170,7 +172,7 @@ export function assertCompositeType(type: ?GraphQLType): GraphQLCompositeType {
isCompositeType(type),
`Expected ${String(type)} to be a GraphQL composite type.`,
);
return (type: any);
return type;
}

/**
Expand All @@ -180,7 +182,7 @@ export type GraphQLAbstractType =
GraphQLInterfaceType |
GraphQLUnionType;

export function isAbstractType(type: ?GraphQLType): boolean {
export function isAbstractType(type: ?GraphQLType): boolean %checks {
return (
type instanceof GraphQLInterfaceType ||
type instanceof GraphQLUnionType
Expand All @@ -192,7 +194,7 @@ export function assertAbstractType(type: ?GraphQLType): GraphQLAbstractType {
isAbstractType(type),
`Expected ${String(type)} to be a GraphQL abstract type.`,
);
return (type: any);
return type;
}

/**
Expand Down Expand Up @@ -224,7 +226,7 @@ export type GraphQLNamedType =
GraphQLEnumType |
GraphQLInputObjectType;

export function isNamedType(type: ?GraphQLType): boolean {
export function isNamedType(type: ?GraphQLType): boolean %checks {
return (
type instanceof GraphQLScalarType ||
type instanceof GraphQLObjectType ||
Expand All @@ -240,18 +242,24 @@ export function assertNamedType(type: ?GraphQLType): GraphQLNamedType {
isNamedType(type),
`Expected ${String(type)} to be a GraphQL named type.`,
);
return (type: any);
return type;
}

export function getNamedType(type: ?GraphQLType): ?GraphQLNamedType {
let unmodifiedType = type;
while (
unmodifiedType instanceof GraphQLList ||
unmodifiedType instanceof GraphQLNonNull
) {
unmodifiedType = unmodifiedType.ofType;
/* eslint-disable no-redeclare */
declare function getNamedType(type: void | null): void;
declare function getNamedType(type: GraphQLType): GraphQLNamedType;
export function getNamedType(type) {
/* eslint-enable no-redeclare */
if (type) {
let unmodifiedType = type;
while (
unmodifiedType instanceof GraphQLList ||
unmodifiedType instanceof GraphQLNonNull
) {
unmodifiedType = unmodifiedType.ofType;
}
return unmodifiedType;
}
return unmodifiedType;
}


Expand Down Expand Up @@ -556,7 +564,7 @@ function isPlainObj(obj) {
}

// If a resolver is defined, it must be a function.
function isValidResolver(resolver: any): boolean {
function isValidResolver(resolver: mixed): boolean {
return (resolver == null || typeof resolver === 'function');
}

Expand Down
4 changes: 2 additions & 2 deletions src/type/introspection.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
GraphQLInputObjectType,
GraphQLList,
GraphQLNonNull,
isAbstractType,
} from './definition';
import { GraphQLString, GraphQLBoolean } from './scalars';
import { DirectiveLocation } from './directives';
Expand Down Expand Up @@ -267,8 +268,7 @@ export const __Type = new GraphQLObjectType({
possibleTypes: {
type: new GraphQLList(new GraphQLNonNull(__Type)),
resolve(type, args, context, { schema }) {
if (type instanceof GraphQLInterfaceType ||
type instanceof GraphQLUnionType) {
if (isAbstractType(type)) {
return schema.getPossibleTypes(type);
}
}
Expand Down
19 changes: 4 additions & 15 deletions src/utilities/TypeInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
getNamedType,
GraphQLObjectType,
GraphQLInterfaceType,
GraphQLUnionType,
GraphQLInputObjectType,
GraphQLEnumType,
GraphQLList,
Expand Down Expand Up @@ -120,9 +119,7 @@ export class TypeInfo {
case Kind.SELECTION_SET:
const namedType = getNamedType(this.getType());
this._parentTypeStack.push(
isCompositeType(namedType) ?
((namedType: any): GraphQLCompositeType) :
undefined
isCompositeType(namedType) ? namedType : undefined
);
break;
case Kind.FIELD:
Expand Down Expand Up @@ -155,17 +152,13 @@ export class TypeInfo {
typeFromAST(schema, typeConditionAST) :
this.getType();
this._typeStack.push(
isOutputType(outputType) ?
((outputType: any): GraphQLOutputType) :
undefined
isOutputType(outputType) ? outputType : undefined
);
break;
case Kind.VARIABLE_DEFINITION:
const inputType = typeFromAST(schema, node.type);
this._inputTypeStack.push(
isInputType(inputType) ?
((inputType: any): GraphQLInputType) :
undefined
isInputType(inputType) ? inputType : undefined
);
break;
case Kind.ARGUMENT:
Expand Down Expand Up @@ -264,11 +257,7 @@ function getFieldDef(
schema.getQueryType() === parentType) {
return TypeMetaFieldDef;
}
if (name === TypeNameMetaFieldDef.name &&
(parentType instanceof GraphQLObjectType ||
parentType instanceof GraphQLInterfaceType ||
parentType instanceof GraphQLUnionType)
) {
if (name === TypeNameMetaFieldDef.name && isCompositeType(parentType)) {
return TypeNameMetaFieldDef;
}
if (parentType instanceof GraphQLObjectType ||
Expand Down
12 changes: 4 additions & 8 deletions src/utilities/buildASTSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ import {
GraphQLInputObjectType,
GraphQLList,
GraphQLNonNull,
isInputType,
isOutputType,
assertInputType,
assertOutputType,
} from '../type/definition';

import type {
Expand Down Expand Up @@ -299,15 +299,11 @@ export function buildASTSchema(ast: DocumentNode): GraphQLSchema {
}

function produceInputType(typeNode: TypeNode): GraphQLInputType {
const type = produceType(typeNode);
invariant(isInputType(type), 'Expected Input type.');
return (type: any);
return assertInputType(produceType(typeNode));
}

function produceOutputType(typeNode: TypeNode): GraphQLOutputType {
const type = produceType(typeNode);
invariant(isOutputType(type), 'Expected Output type.');
return (type: any);
return assertOutputType(produceType(typeNode));
}

function produceObjectType(typeNode: TypeNode): GraphQLObjectType {
Expand Down
8 changes: 4 additions & 4 deletions src/utilities/buildClientSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export function buildClientSchema(
isInputType(type),
'Introspection must provide input type for arguments.'
);
return (type: any);
return type;
}

function getOutputType(typeRef: IntrospectionTypeRef): GraphQLOutputType {
Expand All @@ -171,7 +171,7 @@ export function buildClientSchema(
isOutputType(type),
'Introspection must provide output type for fields.'
);
return (type: any);
return type;
}

function getObjectType(typeRef: IntrospectionTypeRef): GraphQLObjectType {
Expand All @@ -180,7 +180,7 @@ export function buildClientSchema(
type instanceof GraphQLObjectType,
'Introspection must provide object type for possibleTypes.'
);
return (type: any);
return type;
}

function getInterfaceType(
Expand All @@ -191,7 +191,7 @@ export function buildClientSchema(
type instanceof GraphQLInterfaceType,
'Introspection must provide interface type for interfaces.'
);
return (type: any);
return type;
}


Expand Down
Loading

0 comments on commit d7fe661

Please sign in to comment.