Skip to content
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

Remove some instanceof checks #996

Closed
wants to merge 2 commits into from
Closed
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
8 changes: 8 additions & 0 deletions src/type/definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ function resolveThunk<T>(thunk: Thunk<T>): T {
*
*/
export class GraphQLScalarType {
kind: 'GraphQLScalarType';
name: string;
description: ?string;
astNode: ?ScalarTypeDefinitionNode;
Expand Down Expand Up @@ -422,6 +423,7 @@ export type GraphQLScalarTypeConfig<TInternal, TExternal> = {
*
*/
export class GraphQLObjectType {
kind: 'GraphQLObjectType';
name: string;
description: ?string;
astNode: ?ObjectTypeDefinitionNode;
Expand Down Expand Up @@ -713,6 +715,7 @@ export type GraphQLFieldMap<TSource, TContext> = {
*
*/
export class GraphQLInterfaceType {
kind: 'GraphQLInterfaceType';
name: string;
description: ?string;
astNode: ?InterfaceTypeDefinitionNode;
Expand Down Expand Up @@ -793,6 +796,7 @@ export type GraphQLInterfaceTypeConfig<TSource, TContext> = {
*
*/
export class GraphQLUnionType {
kind: 'GraphQLUnionType';
name: string;
description: ?string;
astNode: ?UnionTypeDefinitionNode;
Expand Down Expand Up @@ -910,6 +914,7 @@ export type GraphQLUnionTypeConfig<TSource, TContext> = {
* will be used as its internal value.
*/
export class GraphQLEnumType/* <T> */ {
kind: 'GraphQLEnumType';
name: string;
description: ?string;
astNode: ?EnumTypeDefinitionNode;
Expand Down Expand Up @@ -1097,6 +1102,7 @@ export type GraphQLEnumValue/* <T> */ = {
*
*/
export class GraphQLInputObjectType {
kind: 'GraphQLInputObjectType';
name: string;
description: ?string;
astNode: ?InputObjectTypeDefinitionNode;
Expand Down Expand Up @@ -1215,6 +1221,7 @@ export type GraphQLInputFieldMap = {
*
*/
export class GraphQLList<T: GraphQLType> {
kind: 'GraphQLList';
ofType: T;

constructor(type: T): void {
Expand Down Expand Up @@ -1260,6 +1267,7 @@ GraphQLList.prototype.toJSON =
* Note: the enforcement of non-nullability occurs within the executor.
*/
export class GraphQLNonNull<T: GraphQLNullableType> {
kind: 'GraphQLNonNull';
ofType: T;

constructor(type: T): void {
Expand Down
10 changes: 5 additions & 5 deletions src/utilities/astFromValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 = [];
Expand All @@ -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;
}
Expand All @@ -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)
);

Expand All @@ -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);
}

Expand Down
4 changes: 2 additions & 2 deletions src/utilities/buildASTSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/utilities/buildClientSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
25 changes: 10 additions & 15 deletions src/utilities/extendSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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 ]
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -441,10 +436,10 @@ export function extendSchema(
}

function extendFieldType<T: GraphQLType>(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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/utilities/isValidJSValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.` ];
}
Expand Down Expand Up @@ -87,7 +87,7 @@ export function isValidJSValue(
}

invariant(
type instanceof GraphQLScalarType || type instanceof GraphQLEnumType,
type.kind === 'GraphQLScalarType' || type.kind === 'GraphQLEnumType',
'Must be input type'
);

Expand Down
4 changes: 2 additions & 2 deletions src/utilities/isValidLiteralValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.` ];
}
Expand Down Expand Up @@ -105,7 +105,7 @@ export function isValidLiteralValue(
}

invariant(
type instanceof GraphQLScalarType || type instanceof GraphQLEnumType,
type.kind === 'GraphQLScalarType' || type.kind === 'GraphQLEnumType',
'Must be input type'
);

Expand Down
12 changes: 6 additions & 6 deletions src/utilities/schemaPrinter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion src/utilities/typeComparators.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/utilities/valueFromAST.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}
Expand Down Expand Up @@ -147,7 +147,7 @@ export function valueFromAST(
}

invariant(
type instanceof GraphQLScalarType || type instanceof GraphQLEnumType,
type.kind === 'GraphQLScalarType' || type.kind === 'GraphQLEnumType',
'Must be input type'
);

Expand Down