UNPKG

1.37 kBJavaScriptView Raw
1import inspect from "../jsutils/inspect.mjs";
2import invariant from "../jsutils/invariant.mjs";
3import { Kind } from "../language/kinds.mjs";
4import { GraphQLList, GraphQLNonNull } from "../type/definition.mjs";
5/**
6 * Given a Schema and an AST node describing a type, return a GraphQLType
7 * definition which applies to that type. For example, if provided the parsed
8 * AST node for `[User]`, a GraphQLList instance will be returned, containing
9 * the type called "User" found in the schema. If a type called "User" is not
10 * found in the schema, then undefined will be returned.
11 */
12
13/* eslint-disable no-redeclare */
14
15export function typeFromAST(schema, typeNode) {
16 /* eslint-enable no-redeclare */
17 var innerType;
18
19 if (typeNode.kind === Kind.LIST_TYPE) {
20 innerType = typeFromAST(schema, typeNode.type);
21 return innerType && new GraphQLList(innerType);
22 }
23
24 if (typeNode.kind === Kind.NON_NULL_TYPE) {
25 innerType = typeFromAST(schema, typeNode.type);
26 return innerType && new GraphQLNonNull(innerType);
27 } // istanbul ignore else (See: 'https://github.com/graphql/graphql-js/issues/2618')
28
29
30 if (typeNode.kind === Kind.NAMED_TYPE) {
31 return schema.getType(typeNode.name.value);
32 } // istanbul ignore next (Not reachable. All possible type nodes have been considered)
33
34
35 false || invariant(0, 'Unexpected type node: ' + inspect(typeNode));
36}