This repository has been archived by the owner on Apr 15, 2020. It is now read-only.
forked from ardatan/graphql-tools
/
typeFromAST.ts
248 lines (231 loc) · 6.85 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import {
DefinitionNode,
EnumTypeDefinitionNode,
FieldDefinitionNode,
GraphQLEnumType,
GraphQLInputObjectType,
GraphQLInputType,
GraphQLInterfaceType,
GraphQLList,
GraphQLNamedType,
GraphQLNonNull,
GraphQLObjectType,
GraphQLScalarType,
GraphQLType,
GraphQLUnionType,
InputObjectTypeDefinitionNode,
InputValueDefinitionNode,
InterfaceTypeDefinitionNode,
Kind,
ObjectTypeDefinitionNode,
ScalarTypeDefinitionNode,
TypeNode,
UnionTypeDefinitionNode,
getDescription,
GraphQLString,
GraphQLDirective,
DirectiveDefinitionNode,
DirectiveLocationEnum,
DirectiveLocation,
GraphQLFieldConfig,
StringValueNode,
} from 'graphql';
import resolveFromParentType from './resolveFromParentTypename';
const backcompatOptions = { commentDescriptions: true };
export type GetType = (
name: string,
// this is a hack
type: 'object' | 'interface' | 'input',
) => GraphQLObjectType | GraphQLInputObjectType | GraphQLInterfaceType;
export default function typeFromAST(
node: DefinitionNode,
): GraphQLNamedType | GraphQLDirective | null {
switch (node.kind) {
case Kind.OBJECT_TYPE_DEFINITION:
return makeObjectType(node);
case Kind.INTERFACE_TYPE_DEFINITION:
return makeInterfaceType(node);
case Kind.ENUM_TYPE_DEFINITION:
return makeEnumType(node);
case Kind.UNION_TYPE_DEFINITION:
return makeUnionType(node);
case Kind.SCALAR_TYPE_DEFINITION:
return makeScalarType(node);
case Kind.INPUT_OBJECT_TYPE_DEFINITION:
return makeInputObjectType(node);
case Kind.DIRECTIVE_DEFINITION:
return makeDirective(node);
default:
return null;
}
}
function makeObjectType(
node: ObjectTypeDefinitionNode,
): GraphQLObjectType {
return new GraphQLObjectType({
name: node.name.value,
fields: () => makeFields(node.fields),
interfaces: () =>
node.interfaces.map(
iface => createNamedStub(iface.name.value, 'interface') as GraphQLInterfaceType,
),
description: getDescription(node, backcompatOptions),
});
}
function makeInterfaceType(
node: InterfaceTypeDefinitionNode,
): GraphQLInterfaceType {
return new GraphQLInterfaceType({
name: node.name.value,
fields: () => makeFields(node.fields),
description: getDescription(node, backcompatOptions),
resolveType: (parent, context, info) =>
resolveFromParentType(parent, info.schema),
});
}
function makeEnumType(
node: EnumTypeDefinitionNode,
): GraphQLEnumType {
const values = {};
node.values.forEach(value => {
values[value.name.value] = {
description: getDescription(value, backcompatOptions),
};
});
return new GraphQLEnumType({
name: node.name.value,
values,
description: getDescription(node, backcompatOptions),
});
}
function makeUnionType(
node: UnionTypeDefinitionNode,
): GraphQLUnionType {
return new GraphQLUnionType({
name: node.name.value,
types: () =>
node.types.map(
type => resolveType(type, 'object') as GraphQLObjectType,
),
description: getDescription(node, backcompatOptions),
resolveType: (parent, context, info) =>
resolveFromParentType(parent, info.schema),
});
}
function makeScalarType(
node: ScalarTypeDefinitionNode,
): GraphQLScalarType {
return new GraphQLScalarType({
name: node.name.value,
description: getDescription(node, backcompatOptions),
serialize: () => null,
// Note: validation calls the parse functions to determine if a
// literal value is correct. Returning null would cause use of custom
// scalars to always fail validation. Returning false causes them to
// always pass validation.
parseValue: () => false,
parseLiteral: () => false,
});
}
function makeInputObjectType(
node: InputObjectTypeDefinitionNode,
): GraphQLInputObjectType {
return new GraphQLInputObjectType({
name: node.name.value,
fields: () => makeValues(node.fields),
description: getDescription(node, backcompatOptions),
});
}
function makeFields(
nodes: ReadonlyArray<FieldDefinitionNode>,
): Record<string, GraphQLFieldConfig<any, any>> {
const result: Record<string, GraphQLFieldConfig<any, any>> = {};
nodes.forEach((node) => {
const deprecatedDirective = node.directives.find(
(directive) =>
directive && directive.name && directive.name.value === 'deprecated',
);
const deprecatedArgument =
deprecatedDirective &&
deprecatedDirective.arguments &&
deprecatedDirective.arguments.find(
(arg) => arg && arg.name && arg.name.value === 'reason',
);
const deprecationReason =
deprecatedArgument &&
deprecatedArgument.value &&
(deprecatedArgument.value as StringValueNode).value;
result[node.name.value] = {
type: resolveType(node.type, 'object') as GraphQLObjectType,
args: makeValues(node.arguments),
description: getDescription(node, backcompatOptions),
deprecationReason,
};
});
return result;
}
function makeValues(nodes: ReadonlyArray<InputValueDefinitionNode>) {
const result = {};
nodes.forEach(node => {
const type = resolveType(node.type, 'input') as GraphQLInputType;
result[node.name.value] = {
type,
defaultValue: node.defaultValue,
description: getDescription(node, backcompatOptions),
};
});
return result;
}
function resolveType(
node: TypeNode,
type: 'object' | 'interface' | 'input',
): GraphQLType {
switch (node.kind) {
case Kind.LIST_TYPE:
return new GraphQLList(resolveType(node.type, type));
case Kind.NON_NULL_TYPE:
return new GraphQLNonNull(resolveType(node.type, type));
default:
return createNamedStub(node.name.value, type);
}
}
function createNamedStub(
name: string,
type: 'object' | 'interface' | 'input'
): GraphQLObjectType | GraphQLInputObjectType | GraphQLInterfaceType {
let constructor: any;
if (type === 'object') {
constructor = GraphQLObjectType;
} else if (type === 'interface') {
constructor = GraphQLInterfaceType;
} else {
constructor = GraphQLInputObjectType;
}
return new constructor({
name,
fields: {
__fake: {
type: GraphQLString,
},
},
});
}
export function isStub(type: GraphQLObjectType | GraphQLInputObjectType | GraphQLInterfaceType): boolean {
const fields = type.getFields();
const fieldNames = Object.keys(fields);
return fieldNames.length === 1 && fields[fieldNames[0]].name === '__fake';
}
function makeDirective(node: DirectiveDefinitionNode): GraphQLDirective {
const locations: Array<DirectiveLocationEnum> = [];
node.locations.forEach(location => {
if (<DirectiveLocationEnum>location.value in DirectiveLocation) {
locations.push(<DirectiveLocationEnum>location.value);
}
});
return new GraphQLDirective({
name: node.name.value,
description: node.description ? node.description.value : null,
args: makeValues(node.arguments),
locations,
});
}