-
Notifications
You must be signed in to change notification settings - Fork 2k
/
typeFromAST.ts
53 lines (50 loc) · 1.64 KB
/
typeFromAST.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import type {
TypeNode,
NamedTypeNode,
ListTypeNode,
NonNullTypeNode,
} from '../language/ast';
import { Kind } from '../language/kinds';
import type { GraphQLSchema } from '../type/schema';
import type { GraphQLType, GraphQLNamedType } from '../type/definition';
import { GraphQLList, GraphQLNonNull } from '../type/definition';
/**
* 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: GraphQLSchema,
typeNode: NamedTypeNode,
): GraphQLNamedType | undefined;
export function typeFromAST(
schema: GraphQLSchema,
typeNode: ListTypeNode,
): GraphQLList<any> | undefined;
export function typeFromAST(
schema: GraphQLSchema,
typeNode: NonNullTypeNode,
): GraphQLNonNull<any> | undefined;
export function typeFromAST(
schema: GraphQLSchema,
typeNode: TypeNode,
): GraphQLType | undefined;
export function typeFromAST(
schema: GraphQLSchema,
typeNode: TypeNode,
): GraphQLType | undefined {
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);
}
}