UNPKG

1.29 kBJavaScriptView Raw
1import inspect from '../jsutils/inspect';
2import invariant from '../jsutils/invariant';
3import { Kind } from '../language/kinds';
4import { GraphQLList, GraphQLNonNull } from '../type/definition';
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 && GraphQLList(innerType);
22 }
23
24 if (typeNode.kind === Kind.NON_NULL_TYPE) {
25 innerType = typeFromAST(schema, typeNode.type);
26 return innerType && GraphQLNonNull(innerType);
27 }
28
29 /* istanbul ignore else */
30 if (typeNode.kind === Kind.NAMED_TYPE) {
31 return schema.getType(typeNode.name.value);
32 } // Not reachable. All possible type nodes have been considered.
33
34
35 /* istanbul ignore next */
36 invariant(false, 'Unexpected type node: ' + inspect(typeNode));
37}