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

typeFromAST: use exhaustive switch and remove invariant #3396

Merged
merged 1 commit into from Nov 30, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 11 additions & 18 deletions src/utilities/typeFromAST.ts
@@ -1,6 +1,3 @@
import { inspect } from '../jsutils/inspect';
import { invariant } from '../jsutils/invariant';

import type {
TypeNode,
NamedTypeNode,
Expand Down Expand Up @@ -41,20 +38,16 @@ export function typeFromAST(
schema: GraphQLSchema,
typeNode: TypeNode,
): GraphQLType | undefined {
let innerType;
if (typeNode.kind === Kind.LIST_TYPE) {
innerType = typeFromAST(schema, typeNode.type);
return innerType && new GraphQLList(innerType);
}
if (typeNode.kind === Kind.NON_NULL_TYPE) {
innerType = typeFromAST(schema, typeNode.type);
return innerType && new GraphQLNonNull(innerType);
switch (typeNode.kind) {
case Kind.LIST_TYPE: {
const innerType = typeFromAST(schema, typeNode.type);
return innerType && new GraphQLList(innerType);
}
case Kind.NON_NULL_TYPE: {
const innerType = typeFromAST(schema, typeNode.type);
return innerType && new GraphQLNonNull(innerType);
}
case Kind.NAMED_TYPE:
return schema.getType(typeNode.name.value);
}
// istanbul ignore else (See: 'https://github.com/graphql/graphql-js/issues/2618')
if (typeNode.kind === Kind.NAMED_TYPE) {
return schema.getType(typeNode.name.value);
}

// istanbul ignore next (Not reachable. All possible type nodes have been considered)
invariant(false, 'Unexpected type node: ' + inspect(typeNode));
}