Skip to content

Commit

Permalink
typeFromAST: use exhaustive switch and remove invariant (#3396)
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed Nov 30, 2021
1 parent 79d984f commit bc3564a
Showing 1 changed file with 11 additions and 18 deletions.
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));
}

0 comments on commit bc3564a

Please sign in to comment.