graphql
Version:
A Query Language and Runtime which can target any service.
32 lines (26 loc) • 1.31 kB
JavaScript
import { inspect } from '../jsutils/inspect.mjs';
import { invariant } from '../jsutils/invariant.mjs';
import { Kind } from '../language/kinds.mjs';
import { GraphQLList, GraphQLNonNull } from '../type/definition.mjs';
/**
* Given a Schema and an AST node describing a type, return a GraphQLType
* definition which applies to that type. For example, if provided the parsed
* AST node for `[User]`, a GraphQLList instance will be returned, containing
* the type called "User" found in the schema. If a type called "User" is not
* found in the schema, then undefined will be returned.
*/
export function typeFromAST(schema, typeNode) {
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);
} // 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)
false || invariant(false, 'Unexpected type node: ' + inspect(typeNode));
}