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

Replace instanceof checks with their respective predicates #1518

Merged
merged 3 commits into from
Sep 9, 2019
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
12 changes: 6 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@
- `apollo`
- <First `apollo` related entry goes here>
- `apollo-codegen-core`
- <First `apollo-codegen-core` related entry goes here>
- Replace instanceof checks with their respective predicates [#1518](https://github.com/apollographql/apollo-tooling/pull/1518)
- `apollo-codegen-flow`
- <First `apollo-codegen-flow` related entry goes here>
- Replace instanceof checks with their respective predicates [#1518](https://github.com/apollographql/apollo-tooling/pull/1518)
- `apollo-codegen-scala`
- <First `apollo-codegen-scala` related entry goes here>
- Replace instanceof checks with their respective predicates [#1518](https://github.com/apollographql/apollo-tooling/pull/1518)
- `apollo-codegen-swift`
- <First `apollo-codegen-swift` related entry goes here>
- Replace instanceof checks with their respective predicates [#1518](https://github.com/apollographql/apollo-tooling/pull/1518)
- `apollo-codegen-typescript`
- <First `apollo-codegen-typescript` related entry goes here>
- Replace instanceof checks with their respective predicates [#1518](https://github.com/apollographql/apollo-tooling/pull/1518)
- `apollo-env`
- <First `apollo-env` related entry goes here>
- `apollo-graphql`
- <First `apollo-graphql` related entry goes here>
- `apollo-language-server`
- <First `apollo-language-server` related entry goes here>
- Replace instanceof checks with their respective predicates [#1518](https://github.com/apollographql/apollo-tooling/pull/1518)
- `apollo-tools`
- <First `apollo-tools` related entry goes here>
- `vscode-apollo`
Expand Down
16 changes: 8 additions & 8 deletions packages/apollo-codegen-core/src/compiler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import {
isCompositeType,
GraphQLOutputType,
GraphQLInputType,
GraphQLScalarType,
GraphQLEnumType,
GraphQLInputObjectType,
GraphQLObjectType,
GraphQLError,
GraphQLSchema,
Expand All @@ -22,7 +19,10 @@ import {
SelectionNode,
isSpecifiedScalarType,
NonNullTypeNode,
GraphQLNonNull
GraphQLNonNull,
isEnumType,
isInputObjectType,
isScalarType
} from "graphql";

import {
Expand Down Expand Up @@ -202,13 +202,13 @@ class Compiler {
if (this.typesUsedSet.has(type)) return;

if (
type instanceof GraphQLEnumType ||
type instanceof GraphQLInputObjectType ||
(type instanceof GraphQLScalarType && !isSpecifiedScalarType(type))
isEnumType(type) ||
isInputObjectType(type) ||
(isScalarType(type) && !isSpecifiedScalarType(type))
) {
this.typesUsedSet.add(type);
}
if (type instanceof GraphQLInputObjectType) {
if (isInputObjectType(type)) {
for (const field of Object.values(type.getFields())) {
this.addTypeUsed(getNamedType(field.type));
}
Expand Down
11 changes: 7 additions & 4 deletions packages/apollo-codegen-core/src/serializeToJSON.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import {
GraphQLType,
GraphQLScalarType,
GraphQLEnumType,
GraphQLInputObjectType
GraphQLInputObjectType,
isEnumType,
isInputObjectType,
isScalarType
} from "graphql";

import { LegacyCompilerContext } from "./compiler/legacyIR";
Expand Down Expand Up @@ -34,11 +37,11 @@ export function serializeAST(ast: any, space?: string) {
}

function serializeType(type: GraphQLType) {
if (type instanceof GraphQLEnumType) {
if (isEnumType(type)) {
return serializeEnumType(type);
} else if (type instanceof GraphQLInputObjectType) {
} else if (isInputObjectType(type)) {
return serializeInputObjectType(type);
} else if (type instanceof GraphQLScalarType) {
} else if (isScalarType(type)) {
return serializeScalarType(type);
} else {
throw new Error(`Unexpected GraphQL type: ${type}`);
Expand Down
21 changes: 9 additions & 12 deletions packages/apollo-codegen-core/src/utilities/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import {
TypeMetaFieldDef,
TypeNameMetaFieldDef,
GraphQLCompositeType,
GraphQLObjectType,
GraphQLInterfaceType,
GraphQLUnionType,
GraphQLEnumValue,
GraphQLError,
GraphQLSchema,
Expand All @@ -24,7 +21,10 @@ import {
DocumentNode,
DirectiveNode,
isListType,
isNonNullType
isNonNullType,
isObjectType,
isInterfaceType,
isUnionType
} from "graphql";

declare module "graphql/utilities/buildASTSchema" {
Expand Down Expand Up @@ -162,7 +162,7 @@ export function isTypeProperSuperTypeOf(
) {
return (
isEqualType(maybeSuperType, subType) ||
(subType instanceof GraphQLObjectType &&
(isObjectType(subType) &&
(isAbstractType(maybeSuperType) &&
schema.isPossibleType(maybeSuperType, subType)))
);
Expand Down Expand Up @@ -226,16 +226,13 @@ export function getFieldDef(
}
if (
name === TypeNameMetaFieldDef.name &&
(parentType instanceof GraphQLObjectType ||
parentType instanceof GraphQLInterfaceType ||
parentType instanceof GraphQLUnionType)
(isObjectType(parentType) ||
isInterfaceType(parentType) ||
isUnionType(parentType))
) {
return TypeNameMetaFieldDef;
}
if (
parentType instanceof GraphQLObjectType ||
parentType instanceof GraphQLInterfaceType
) {
if (isObjectType(parentType) || isInterfaceType(parentType)) {
return parentType.getFields()[name];
}

Expand Down
25 changes: 12 additions & 13 deletions packages/apollo-codegen-flow/src/codeGeneration.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import * as t from "@babel/types";
import { stripIndent } from "common-tags";
import { GraphQLEnumType, GraphQLInputObjectType } from "graphql";
import {
GraphQLEnumType,
GraphQLInputObjectType,
isEnumType,
isInputObjectType
} from "graphql";

import {
CompilerContext,
Expand Down Expand Up @@ -42,19 +47,13 @@ function printEnumsAndInputObjects(
//==============================================================
`);

context.typesUsed
.filter(type => type instanceof GraphQLEnumType)
.forEach(enumType => {
generator.typeAliasForEnumType(enumType as GraphQLEnumType);
});
context.typesUsed.filter(isEnumType).forEach(enumType => {
generator.typeAliasForEnumType(enumType);
});

context.typesUsed
.filter(type => type instanceof GraphQLInputObjectType)
.forEach(inputObjectType => {
generator.typeAliasForInputObjectType(
inputObjectType as GraphQLInputObjectType
);
});
context.typesUsed.filter(isInputObjectType).forEach(inputObjectType => {
generator.typeAliasForInputObjectType(inputObjectType);
});

generator.printer.enqueue(stripIndent`
//==============================================================
Expand Down
6 changes: 3 additions & 3 deletions packages/apollo-codegen-flow/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import {
GraphQLFloat,
GraphQLInt,
GraphQLID,
GraphQLScalarType,
GraphQLString,
GraphQLType,
isListType,
isNonNullType
isNonNullType,
isScalarType
} from "graphql";

import * as t from "@babel/types";
Expand Down Expand Up @@ -40,7 +40,7 @@ export function createTypeAnnotationFromGraphQLTypeFunction(
typeAnnotationFromGraphQLType(type.ofType, typeName)
])
);
} else if (type instanceof GraphQLScalarType) {
} else if (isScalarType(type)) {
const builtIn = builtInScalarMap[typeName || type.name];
if (builtIn != null) {
return builtIn;
Expand Down
14 changes: 7 additions & 7 deletions packages/apollo-codegen-scala/src/codeGeneration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import {
getNamedType,
isCompositeType,
GraphQLEnumType,
GraphQLNonNull,
GraphQLInputObjectType
GraphQLInputObjectType,
isNonNullType,
isEnumType,
isInputObjectType
} from "graphql";

import { isTypeProperSuperTypeOf } from "apollo-codegen-core/lib/utilities/graphql";
Expand Down Expand Up @@ -157,9 +159,7 @@ export function classDeclarationForOperation(
undefined,
true
);
const isOptional = !(
type instanceof GraphQLNonNull || type instanceof GraphQLNonNull
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lol what even was this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shadaj this is way back in history but can you recall if this was intentional or possibly a mistake?
Re: 58f07c4#diff-508d6e2cd0e11941b1548a2ea48a909dR139

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a mistake! I really hope there's no situation in which double-checking a type is actually needed 😄

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shadaj Was removing the second check the mistake, or was that correct? Like this was originally

const isOptional = !(type instanceof GraphQLNonNull || type.ofType instanceof GraphQLNonNull);

was the ofType incorrectly removed, or was that fine to remove?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was fine to remove the extra check, I believe that ofType was eliminated with changes to the IR format so only the top level type needed to be checked.

);
const isOptional = !isNonNullType(type);
return { name, propertyName, type, typeName, isOptional };
});

Expand Down Expand Up @@ -382,9 +382,9 @@ export function typeDeclarationForGraphQLType(
generator: CodeGenerator<LegacyCompilerContext, any>,
type: GraphQLType
) {
if (type instanceof GraphQLEnumType) {
if (isEnumType(type)) {
enumerationDeclaration(generator, type);
} else if (type instanceof GraphQLInputObjectType) {
} else if (isInputObjectType(type)) {
traitDeclarationForInputObjectType(generator, type);
}
}
Expand Down
14 changes: 7 additions & 7 deletions packages/apollo-codegen-scala/src/naming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import { escapeIdentifierIfNeeded, Property } from "./language";
import { typeNameFromGraphQLType } from "./types";

import {
GraphQLList,
GraphQLNonNull,
getNamedType,
isCompositeType
isCompositeType,
isNonNullType,
isListType
} from "graphql";
import {
LegacyCompilerContext,
Expand Down Expand Up @@ -53,8 +53,8 @@ export function propertyFromInputField(
const propertyName = escapeIdentifierIfNeeded(unescapedPropertyName);

const type = field.type;
const isList = type instanceof GraphQLList || type instanceof GraphQLList;
const isOptional = !(type instanceof GraphQLNonNull);
const isList = isListType(type);
const isOptional = !isNonNullType(type);
const bareType = getNamedType(type);

const bareTypeName = isCompositeType(bareType)
Expand Down Expand Up @@ -94,8 +94,8 @@ export function propertyFromLegacyField(
const propertyName = escapeIdentifierIfNeeded(name);

const type = field.type;
const isList = type instanceof GraphQLList || type instanceof GraphQLList;
const isOptional = field.isConditional || !(type instanceof GraphQLNonNull);
const isList = isListType(type);
const isOptional = field.isConditional || !isNonNullType(type);
const bareType = getNamedType(type);

const bareTypeName = isCompositeType(bareType)
Expand Down
9 changes: 5 additions & 4 deletions packages/apollo-codegen-scala/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import {
GraphQLBoolean,
GraphQLID,
GraphQLScalarType,
GraphQLEnumType,
isAbstractType,
isNonNullType,
isListType
isListType,
isScalarType,
isEnumType
} from "graphql";
import { LegacyCompilerContext } from "apollo-codegen-core/lib/compiler/legacyIR";
import { GraphQLType } from "graphql";
Expand Down Expand Up @@ -76,9 +77,9 @@ export function typeNameFromGraphQLType(
) +
"]";
}
} else if (type instanceof GraphQLScalarType) {
} else if (isScalarType(type)) {
typeName = typeNameForScalarType(context, type);
} else if (type instanceof GraphQLEnumType) {
} else if (isEnumType(type)) {
typeName = "String";
} else {
typeName = bareTypeName || type.name;
Expand Down
8 changes: 5 additions & 3 deletions packages/apollo-codegen-swift/src/codeGeneration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import {
GraphQLEnumType,
GraphQLInputObjectType,
isNonNullType,
isListType
isListType,
isEnumType,
isInputObjectType
} from "graphql";

import {
Expand Down Expand Up @@ -965,9 +967,9 @@ export class SwiftAPIGenerator extends SwiftGenerator<CompilerContext> {
type: GraphQLType,
outputIndividualFiles: boolean
) {
if (type instanceof GraphQLEnumType) {
if (isEnumType(type)) {
this.enumerationDeclaration(type);
} else if (type instanceof GraphQLInputObjectType) {
} else if (isInputObjectType(type)) {
this.structDeclarationForInputObjectType(type, outputIndividualFiles);
}
}
Expand Down
16 changes: 8 additions & 8 deletions packages/apollo-codegen-swift/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import {
GraphQLFloat,
GraphQLBoolean,
GraphQLID,
GraphQLNonNull,
GraphQLScalarType,
GraphQLEnumType,
isCompositeType,
getNamedType,
GraphQLInputField,
isNonNullType,
isListType
isListType,
isScalarType,
isEnumType
} from "graphql";

import { camelCase, pascalCase } from "change-case";
Expand Down Expand Up @@ -66,7 +66,7 @@ export class Helpers {
"[" +
this.typeNameFromGraphQLType(type.ofType, unmodifiedTypeName) +
"]";
} else if (type instanceof GraphQLScalarType) {
} else if (isScalarType(type)) {
typeName = this.typeNameForScalarType(type);
} else {
typeName = unmodifiedTypeName || type.name;
Expand All @@ -89,9 +89,9 @@ export class Helpers {
return `.nonNull(${this.fieldTypeEnum(type.ofType, structName)})`;
} else if (isListType(type)) {
return `.list(${this.fieldTypeEnum(type.ofType, structName)})`;
} else if (type instanceof GraphQLScalarType) {
} else if (isScalarType(type)) {
return `.scalar(${this.typeNameForScalarType(type)}.self)`;
} else if (type instanceof GraphQLEnumType) {
} else if (isEnumType(type)) {
return `.scalar(${type.name}.self)`;
} else if (isCompositeType(type)) {
return `.object(${structName}.selections)`;
Expand Down Expand Up @@ -151,7 +151,7 @@ export class Helpers {
type = type.ofType;
}

const isOptional = !(type instanceof GraphQLNonNull);
const isOptional = !isNonNullType(type);

const unmodifiedType = getNamedType(field.type);

Expand Down Expand Up @@ -200,7 +200,7 @@ export class Helpers {
return Object.assign({}, field, {
propertyName: camelCase(field.name),
typeName: this.typeNameFromGraphQLType(field.type),
isOptional: !(field.type instanceof GraphQLNonNull)
isOptional: !isNonNullType(field.type)
});
}

Expand Down
Loading