From 1fb898335dea2330c8c7eac28991b94b0c8a5ffb Mon Sep 17 00:00:00 2001 From: mmahoney Date: Thu, 17 Aug 2017 18:28:51 -0400 Subject: [PATCH 1/2] Remove instanceof checks from some utilities --- src/type/definition.js | 8 ++++++++ src/type/directives.js | 1 + src/utilities/astFromValue.js | 10 +++++----- src/utilities/buildASTSchema.js | 4 ++-- src/utilities/buildClientSchema.js | 6 +++--- src/utilities/extendSchema.js | 25 ++++++++++--------------- src/utilities/isValidJSValue.js | 4 ++-- src/utilities/isValidLiteralValue.js | 4 ++-- src/utilities/schemaPrinter.js | 12 ++++++------ src/utilities/typeComparators.js | 2 +- src/utilities/valueFromAST.js | 4 ++-- 11 files changed, 42 insertions(+), 38 deletions(-) diff --git a/src/type/definition.js b/src/type/definition.js index 17c3c16e87..c4b6ce510e 100644 --- a/src/type/definition.js +++ b/src/type/definition.js @@ -302,6 +302,7 @@ function resolveThunk(thunk: Thunk): T { * */ export class GraphQLScalarType { + kind: 'GraphQLScalarType'; name: string; description: ?string; astNode: ?ScalarTypeDefinitionNode; @@ -422,6 +423,7 @@ export type GraphQLScalarTypeConfig = { * */ export class GraphQLObjectType { + kind: 'GraphQLObjectType'; name: string; description: ?string; astNode: ?ObjectTypeDefinitionNode; @@ -713,6 +715,7 @@ export type GraphQLFieldMap = { * */ export class GraphQLInterfaceType { + kind: 'GraphQLInterfaceType'; name: string; description: ?string; astNode: ?InterfaceTypeDefinitionNode; @@ -793,6 +796,7 @@ export type GraphQLInterfaceTypeConfig = { * */ export class GraphQLUnionType { + kind: 'GraphQLUnionType'; name: string; description: ?string; astNode: ?UnionTypeDefinitionNode; @@ -910,6 +914,7 @@ export type GraphQLUnionTypeConfig = { * will be used as its internal value. */ export class GraphQLEnumType/* */ { + kind: 'GraphQLEnumType'; name: string; description: ?string; astNode: ?EnumTypeDefinitionNode; @@ -1097,6 +1102,7 @@ export type GraphQLEnumValue/* */ = { * */ export class GraphQLInputObjectType { + kind: 'GraphQLInputObjectType'; name: string; description: ?string; astNode: ?InputObjectTypeDefinitionNode; @@ -1215,6 +1221,7 @@ export type GraphQLInputFieldMap = { * */ export class GraphQLList { + kind: 'GraphQLList'; ofType: T; constructor(type: T): void { @@ -1260,6 +1267,7 @@ GraphQLList.prototype.toJSON = * Note: the enforcement of non-nullability occurs within the executor. */ export class GraphQLNonNull { + kind: 'GraphQLNonNull'; ofType: T; constructor(type: T): void { diff --git a/src/type/directives.js b/src/type/directives.js index ad0298bced..42b892e600 100644 --- a/src/type/directives.js +++ b/src/type/directives.js @@ -49,6 +49,7 @@ export type DirectiveLocationEnum = $Keys; // eslint-d * behavior. Type system creators will usually not create these directly. */ export class GraphQLDirective { + kind: 'GraphQLDirective'; name: string; description: ?string; locations: Array; diff --git a/src/utilities/astFromValue.js b/src/utilities/astFromValue.js index 6598103982..e47764aa68 100644 --- a/src/utilities/astFromValue.js +++ b/src/utilities/astFromValue.js @@ -60,7 +60,7 @@ export function astFromValue( // Ensure flow knows that we treat function params as const. const _value = value; - if (type instanceof GraphQLNonNull) { + if (type.kind === 'GraphQLNonNull') { const astValue = astFromValue(_value, type.ofType); if (astValue && astValue.kind === Kind.NULL) { return null; @@ -80,7 +80,7 @@ export function astFromValue( // Convert JavaScript array to GraphQL list. If the GraphQLType is a list, but // the value is not an array, convert the value using the list's item type. - if (type instanceof GraphQLList) { + if (type.kind === 'GraphQLList') { const itemType = type.ofType; if (isCollection(_value)) { const valuesNodes = []; @@ -97,7 +97,7 @@ export function astFromValue( // Populate the fields of the input object by creating ASTs from each value // in the JavaScript object according to the fields in the input type. - if (type instanceof GraphQLInputObjectType) { + if (type.kind === 'GraphQLInputObjectType') { if (_value === null || typeof _value !== 'object') { return null; } @@ -118,7 +118,7 @@ export function astFromValue( } invariant( - type instanceof GraphQLScalarType || type instanceof GraphQLEnumType, + type.kind === 'GraphQLScalarType' || type.kind === 'GraphQLEnumType', 'Must provide Input Type, cannot use: ' + String(type) ); @@ -144,7 +144,7 @@ export function astFromValue( if (typeof serialized === 'string') { // Enum types use Enum literals. - if (type instanceof GraphQLEnumType) { + if (type.kind === 'GraphQLEnumType') { return ({ kind: Kind.ENUM, value: serialized }: EnumValueNode); } diff --git a/src/utilities/buildASTSchema.js b/src/utilities/buildASTSchema.js index f1038b3955..8037aa737b 100644 --- a/src/utilities/buildASTSchema.js +++ b/src/utilities/buildASTSchema.js @@ -99,7 +99,7 @@ function buildWrappedType( } if (inputTypeNode.kind === Kind.NON_NULL_TYPE) { const wrappedType = buildWrappedType(innerType, inputTypeNode.type); - invariant(!(wrappedType instanceof GraphQLNonNull), 'No nesting nonnull.'); + invariant(wrappedType.kind !== 'GraphQLNonNull', 'No nesting nonnull.'); return new GraphQLNonNull(wrappedType); } return innerType; @@ -283,7 +283,7 @@ export function buildASTSchema(ast: DocumentNode): GraphQLSchema { function getObjectType(typeNode: TypeDefinitionNode): GraphQLObjectType { const type = typeDefNamed(typeNode.name.value); invariant( - type instanceof GraphQLObjectType, + type.kind === 'GraphQLObjectType', 'AST must provide object type.' ); return (type: any); diff --git a/src/utilities/buildClientSchema.js b/src/utilities/buildClientSchema.js index fca78567ed..816dff92c6 100644 --- a/src/utilities/buildClientSchema.js +++ b/src/utilities/buildClientSchema.js @@ -131,7 +131,7 @@ export function buildClientSchema( } const nullableType = getType(nullableRef); invariant( - !(nullableType instanceof GraphQLNonNull), + nullableType.kind !== 'GraphQLNonNull', 'No nesting nonnull.' ); return new GraphQLNonNull(nullableType); @@ -177,7 +177,7 @@ export function buildClientSchema( function getObjectType(typeRef: IntrospectionTypeRef): GraphQLObjectType { const type = getType(typeRef); invariant( - type instanceof GraphQLObjectType, + type.kind === 'GraphQLObjectType', 'Introspection must provide object type for possibleTypes.' ); return type; @@ -188,7 +188,7 @@ export function buildClientSchema( ): GraphQLInterfaceType { const type = getType(typeRef); invariant( - type instanceof GraphQLInterfaceType, + type.kind === 'GraphQLInterfaceType', 'Introspection must provide interface type for interfaces.' ); return type; diff --git a/src/utilities/extendSchema.js b/src/utilities/extendSchema.js index 70776ed31c..1fb93d479c 100644 --- a/src/utilities/extendSchema.js +++ b/src/utilities/extendSchema.js @@ -100,11 +100,6 @@ export function extendSchema( schema: GraphQLSchema, documentAST: DocumentNode ): GraphQLSchema { - invariant( - schema instanceof GraphQLSchema, - 'Must provide valid GraphQLSchema' - ); - invariant( documentAST && documentAST.kind === Kind.DOCUMENT, 'Must provide valid Document AST' @@ -151,7 +146,7 @@ export function extendSchema( [ def.definition ] ); } - if (!(existingType instanceof GraphQLObjectType)) { + if (!(existingType.kind === 'GraphQLObjectType')) { throw new GraphQLError( `Cannot extend non-object type "${extendedTypeName}".`, [ def.definition ] @@ -276,13 +271,13 @@ export function extendSchema( function getObjectTypeFromAST(node: NamedTypeNode): GraphQLObjectType { const type = getTypeFromAST(node); - invariant(type instanceof GraphQLObjectType, 'Must be Object type.'); + invariant(type.kind === 'GraphQLObjectType', 'Must be Object type.'); return type; } function getInterfaceTypeFromAST(node: NamedTypeNode): GraphQLInterfaceType { const type = getTypeFromAST(node); - invariant(type instanceof GraphQLInterfaceType, 'Must be Interface type.'); + invariant(type.kind === 'GraphQLInterfaceType', 'Must be Interface type.'); return type; } @@ -320,13 +315,13 @@ export function extendSchema( // Given a type's introspection result, construct the correct // GraphQLType instance. function extendType(type: GraphQLNamedType): GraphQLNamedType { - if (type instanceof GraphQLObjectType) { + if (type.kind === 'GraphQLObjectType') { return extendObjectType(type); } - if (type instanceof GraphQLInterfaceType) { + if (type.kind === 'GraphQLInterfaceType') { return extendInterfaceType(type); } - if (type instanceof GraphQLUnionType) { + if (type.kind === 'GraphQLUnionType') { return extendUnionType(type); } return type; @@ -441,10 +436,10 @@ export function extendSchema( } function extendFieldType(typeDef: T): T { - if (typeDef instanceof GraphQLList) { + if (typeDef.kind === 'GraphQLList') { return (new GraphQLList(extendFieldType(typeDef.ofType)): any); } - if (typeDef instanceof GraphQLNonNull) { + if (typeDef.kind === 'GraphQLNonNull') { return (new GraphQLNonNull(extendFieldType(typeDef.ofType)): any); } return getTypeFromDef(typeDef); @@ -590,7 +585,7 @@ export function extendSchema( } if (typeNode.kind === Kind.NON_NULL_TYPE) { const nullableType = buildInputFieldType(typeNode.type); - invariant(!(nullableType instanceof GraphQLNonNull), 'Must be nullable'); + invariant(nullableType.kind !== 'GraphQLNonNull', 'Must be nullable'); return new GraphQLNonNull(nullableType); } return getInputTypeFromAST(typeNode); @@ -602,7 +597,7 @@ export function extendSchema( } if (typeNode.kind === Kind.NON_NULL_TYPE) { const nullableType = buildOutputFieldType(typeNode.type); - invariant(!(nullableType instanceof GraphQLNonNull), 'Must be nullable'); + invariant(nullableType.kind !== 'GraphQLNonNull', 'Must be nullable'); return new GraphQLNonNull(nullableType); } return getOutputTypeFromAST(typeNode); diff --git a/src/utilities/isValidJSValue.js b/src/utilities/isValidJSValue.js index 60c1a0d876..fddebfe8d0 100644 --- a/src/utilities/isValidJSValue.js +++ b/src/utilities/isValidJSValue.js @@ -59,7 +59,7 @@ export function isValidJSValue( } // Input objects check each defined field. - if (type instanceof GraphQLInputObjectType) { + if (type.kind === 'GraphQLInputObjectType') { if (typeof value !== 'object' || value === null) { return [ `Expected "${type.name}", found not an object.` ]; } @@ -87,7 +87,7 @@ export function isValidJSValue( } invariant( - type instanceof GraphQLScalarType || type instanceof GraphQLEnumType, + type.kind === 'GraphQLScalarType' || type.kind === 'GraphQLEnumType', 'Must be input type' ); diff --git a/src/utilities/isValidLiteralValue.js b/src/utilities/isValidLiteralValue.js index 7fbdfb82f3..6f956671b2 100644 --- a/src/utilities/isValidLiteralValue.js +++ b/src/utilities/isValidLiteralValue.js @@ -71,7 +71,7 @@ export function isValidLiteralValue( } // Input objects check each defined field and look for undefined fields. - if (type instanceof GraphQLInputObjectType) { + if (type.kind === 'GraphQLInputObjectType') { if (valueNode.kind !== Kind.OBJECT) { return [ `Expected "${type.name}", found not an object.` ]; } @@ -105,7 +105,7 @@ export function isValidLiteralValue( } invariant( - type instanceof GraphQLScalarType || type instanceof GraphQLEnumType, + type.kind === 'GraphQLScalarType' || type.kind === 'GraphQLEnumType', 'Must be input type' ); diff --git a/src/utilities/schemaPrinter.js b/src/utilities/schemaPrinter.js index 94794b3a4b..ac1470291f 100644 --- a/src/utilities/schemaPrinter.js +++ b/src/utilities/schemaPrinter.js @@ -137,18 +137,18 @@ function isSchemaOfCommonNames(schema: GraphQLSchema): boolean { } export function printType(type: GraphQLType): string { - if (type instanceof GraphQLScalarType) { + if (type.kind === 'GraphQLScalarType') { return printScalar(type); - } else if (type instanceof GraphQLObjectType) { + } else if (type.kind === 'GraphQLObjectType') { return printObject(type); - } else if (type instanceof GraphQLInterfaceType) { + } else if (type.kind === 'GraphQLInterfaceType') { return printInterface(type); - } else if (type instanceof GraphQLUnionType) { + } else if (type.kind === 'GraphQLUnionType') { return printUnion(type); - } else if (type instanceof GraphQLEnumType) { + } else if (type.kind === 'GraphQLEnumType') { return printEnum(type); } - invariant(type instanceof GraphQLInputObjectType); + invariant(type.kind === 'GraphQLInputObjectType'); return printInputObject(type); } diff --git a/src/utilities/typeComparators.js b/src/utilities/typeComparators.js index 330b3c4575..9e8e7d980f 100644 --- a/src/utilities/typeComparators.js +++ b/src/utilities/typeComparators.js @@ -85,7 +85,7 @@ export function isTypeSubTypeOf( // If superType type is an abstract type, maybeSubType type may be a currently // possible object type. if (isAbstractType(superType) && - maybeSubType instanceof GraphQLObjectType && + maybeSubType.kind === 'GraphQLObjectType' && schema.isPossibleType(superType, maybeSubType)) { return true; } diff --git a/src/utilities/valueFromAST.js b/src/utilities/valueFromAST.js index 7eb28019cc..cd0172d6fd 100644 --- a/src/utilities/valueFromAST.js +++ b/src/utilities/valueFromAST.js @@ -114,7 +114,7 @@ export function valueFromAST( return [ coercedValue ]; } - if (type instanceof GraphQLInputObjectType) { + if (type.kind === 'GraphQLInputObjectType') { if (valueNode.kind !== Kind.OBJECT) { return; // Invalid: intentionally return no value. } @@ -147,7 +147,7 @@ export function valueFromAST( } invariant( - type instanceof GraphQLScalarType || type instanceof GraphQLEnumType, + type.kind === 'GraphQLScalarType' || type.kind === 'GraphQLEnumType', 'Must be input type' ); From ff174f0f2b41538938e9e349fd219d69b378caca Mon Sep 17 00:00:00 2001 From: mmahoney Date: Thu, 17 Aug 2017 18:30:29 -0400 Subject: [PATCH 2/2] Remove kind from GraphQLDirective --- src/type/directives.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/type/directives.js b/src/type/directives.js index 42b892e600..ad0298bced 100644 --- a/src/type/directives.js +++ b/src/type/directives.js @@ -49,7 +49,6 @@ export type DirectiveLocationEnum = $Keys; // eslint-d * behavior. Type system creators will usually not create these directly. */ export class GraphQLDirective { - kind: 'GraphQLDirective'; name: string; description: ?string; locations: Array;