-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathbuildASTSchema.js
510 lines (463 loc) · 15.4 KB
/
buildASTSchema.js
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
*/
import invariant from '../jsutils/invariant';
import keyMap from '../jsutils/keyMap';
import keyValMap from '../jsutils/keyValMap';
import type { ObjMap } from '../jsutils/ObjMap';
import { valueFromAST } from './valueFromAST';
import { assertValidSDL } from '../validation/validate';
import blockStringValue from '../language/blockStringValue';
import { TokenKind } from '../language/lexer';
import { parse } from '../language/parser';
import type { ParseOptions } from '../language/parser';
import type { Source } from '../language/source';
import { getDirectiveValues } from '../execution/values';
import { Kind } from '../language/kinds';
import type {
DocumentNode,
TypeNode,
NamedTypeNode,
SchemaDefinitionNode,
TypeDefinitionNode,
ScalarTypeDefinitionNode,
ObjectTypeDefinitionNode,
FieldDefinitionNode,
InputValueDefinitionNode,
InterfaceTypeDefinitionNode,
UnionTypeDefinitionNode,
EnumTypeDefinitionNode,
EnumValueDefinitionNode,
InputObjectTypeDefinitionNode,
DirectiveDefinitionNode,
StringValueNode,
Location,
} from '../language/ast';
import { isTypeDefinitionNode } from '../language/predicates';
import type { DirectiveLocationEnum } from '../language/directiveLocation';
import type {
GraphQLType,
GraphQLNamedType,
GraphQLFieldConfig,
GraphQLEnumValueConfig,
GraphQLInputField,
} from '../type/definition';
import {
GraphQLScalarType,
GraphQLObjectType,
GraphQLInterfaceType,
GraphQLUnionType,
GraphQLEnumType,
GraphQLInputObjectType,
GraphQLList,
GraphQLNonNull,
} from '../type/definition';
import {
GraphQLDirective,
GraphQLSkipDirective,
GraphQLIncludeDirective,
GraphQLDeprecatedDirective,
} from '../type/directives';
import { introspectionTypes } from '../type/introspection';
import { specifiedScalarTypes } from '../type/scalars';
import { GraphQLSchema } from '../type/schema';
import type { GraphQLSchemaValidationOptions } from '../type/schema';
export type BuildSchemaOptions = {
...GraphQLSchemaValidationOptions,
/**
* Descriptions are defined as preceding string literals, however an older
* experimental version of the SDL supported preceding comments as
* descriptions. Set to true to enable this deprecated behavior.
* This option is provided to ease adoption and will be removed in v16.
*
* Default: false
*/
commentDescriptions?: boolean,
/**
* Set to true to assume the SDL is valid.
*
* Default: false
*/
assumeValidSDL?: boolean,
};
/**
* This takes the ast of a schema document produced by the parse function in
* src/language/parser.js.
*
* If no schema definition is provided, then it will look for types named Query
* and Mutation.
*
* Given that AST it constructs a GraphQLSchema. The resulting schema
* has no resolve methods, so execution will use default resolvers.
*
* Accepts options as a second argument:
*
* - commentDescriptions:
* Provide true to use preceding comments as the description.
*
*/
export function buildASTSchema(
documentAST: DocumentNode,
options?: BuildSchemaOptions,
): GraphQLSchema {
invariant(
documentAST && documentAST.kind === Kind.DOCUMENT,
'Must provide valid Document AST',
);
if (!options || !(options.assumeValid || options.assumeValidSDL)) {
assertValidSDL(documentAST);
}
let schemaDef: ?SchemaDefinitionNode;
const typeDefs: Array<TypeDefinitionNode> = [];
const nodeMap: ObjMap<TypeDefinitionNode> = Object.create(null);
const directiveDefs: Array<DirectiveDefinitionNode> = [];
for (let i = 0; i < documentAST.definitions.length; i++) {
const def = documentAST.definitions[i];
if (def.kind === Kind.SCHEMA_DEFINITION) {
schemaDef = def;
} else if (isTypeDefinitionNode(def)) {
const typeName = def.name.value;
if (nodeMap[typeName]) {
throw new Error(`Type "${typeName}" was defined more than once.`);
}
typeDefs.push(def);
nodeMap[typeName] = def;
} else if (def.kind === Kind.DIRECTIVE_DEFINITION) {
directiveDefs.push(def);
}
}
const operationTypes = schemaDef
? getOperationTypes(schemaDef)
: {
query: nodeMap.Query,
mutation: nodeMap.Mutation,
subscription: nodeMap.Subscription,
};
const definitionBuilder = new ASTDefinitionBuilder(
nodeMap,
options,
typeRef => {
throw new Error(`Type "${typeRef.name.value}" not found in document.`);
},
);
const directives = directiveDefs.map(def =>
definitionBuilder.buildDirective(def),
);
// If specified directives were not explicitly declared, add them.
if (!directives.some(directive => directive.name === 'skip')) {
directives.push(GraphQLSkipDirective);
}
if (!directives.some(directive => directive.name === 'include')) {
directives.push(GraphQLIncludeDirective);
}
if (!directives.some(directive => directive.name === 'deprecated')) {
directives.push(GraphQLDeprecatedDirective);
}
// Note: While this could make early assertions to get the correctly
// typed values below, that would throw immediately while type system
// validation with validateSchema() will produce more actionable results.
return new GraphQLSchema({
query: operationTypes.query
? (definitionBuilder.buildType(operationTypes.query): any)
: null,
mutation: operationTypes.mutation
? (definitionBuilder.buildType(operationTypes.mutation): any)
: null,
subscription: operationTypes.subscription
? (definitionBuilder.buildType(operationTypes.subscription): any)
: null,
types: typeDefs.map(node => definitionBuilder.buildType(node)),
directives,
astNode: schemaDef,
assumeValid: options && options.assumeValid,
allowedLegacyNames: options && options.allowedLegacyNames,
});
function getOperationTypes(schema: SchemaDefinitionNode) {
const opTypes = {};
for (const operationType of schema.operationTypes) {
const typeName = operationType.type.name.value;
const operation = operationType.operation;
if (opTypes[operation]) {
throw new Error(`Must provide only one ${operation} type in schema.`);
}
if (!nodeMap[typeName]) {
throw new Error(
`Specified ${operation} type "${typeName}" not found in document.`,
);
}
opTypes[operation] = operationType.type;
}
return opTypes;
}
}
type TypeDefinitionsMap = ObjMap<TypeDefinitionNode>;
type TypeResolver = (typeRef: NamedTypeNode) => GraphQLNamedType;
export class ASTDefinitionBuilder {
_typeDefinitionsMap: TypeDefinitionsMap;
_options: ?BuildSchemaOptions;
_resolveType: TypeResolver;
_cache: ObjMap<GraphQLNamedType>;
constructor(
typeDefinitionsMap: TypeDefinitionsMap,
options: ?BuildSchemaOptions,
resolveType: TypeResolver,
) {
this._typeDefinitionsMap = typeDefinitionsMap;
this._options = options;
this._resolveType = resolveType;
// Initialize to the GraphQL built in scalars and introspection types.
this._cache = keyMap(
specifiedScalarTypes.concat(introspectionTypes),
type => type.name,
);
}
buildType(node: NamedTypeNode | TypeDefinitionNode): GraphQLNamedType {
const typeName = node.name.value;
if (!this._cache[typeName]) {
if (node.kind === Kind.NAMED_TYPE) {
const defNode = this._typeDefinitionsMap[typeName];
this._cache[typeName] = defNode
? this._makeSchemaDef(defNode)
: this._resolveType(node);
} else {
this._cache[typeName] = this._makeSchemaDef(node);
}
}
return this._cache[typeName];
}
_buildWrappedType(typeNode: TypeNode): GraphQLType {
if (typeNode.kind === Kind.LIST_TYPE) {
return GraphQLList(this._buildWrappedType(typeNode.type));
}
if (typeNode.kind === Kind.NON_NULL_TYPE) {
return GraphQLNonNull(
// Note: GraphQLNonNull constructor validates this type
(this._buildWrappedType(typeNode.type): any),
);
}
return this.buildType(typeNode);
}
buildDirective(directiveNode: DirectiveDefinitionNode): GraphQLDirective {
return new GraphQLDirective({
name: directiveNode.name.value,
description: getDescription(directiveNode, this._options),
locations: directiveNode.locations.map(
node => ((node.value: any): DirectiveLocationEnum),
),
args:
directiveNode.arguments &&
this._makeInputValues(directiveNode.arguments),
astNode: directiveNode,
});
}
buildField(field: FieldDefinitionNode): GraphQLFieldConfig<*, *> {
return {
// Note: While this could make assertions to get the correctly typed
// value, that would throw immediately while type system validation
// with validateSchema() will produce more actionable results.
type: (this._buildWrappedType(field.type): any),
description: getDescription(field, this._options),
args: field.arguments && this._makeInputValues(field.arguments),
deprecationReason: getDeprecationReason(field),
astNode: field,
};
}
buildInputField(value: InputValueDefinitionNode): GraphQLInputField {
// Note: While this could make assertions to get the correctly typed
// value, that would throw immediately while type system validation
// with validateSchema() will produce more actionable results.
const type: any = this._buildWrappedType(value.type);
return {
name: value.name.value,
type,
description: getDescription(value, this._options),
defaultValue: valueFromAST(value.defaultValue, type),
astNode: value,
};
}
buildEnumValue(value: EnumValueDefinitionNode): GraphQLEnumValueConfig {
return {
description: getDescription(value, this._options),
deprecationReason: getDeprecationReason(value),
astNode: value,
};
}
_makeSchemaDef(def: TypeDefinitionNode): GraphQLNamedType {
switch (def.kind) {
case Kind.OBJECT_TYPE_DEFINITION:
return this._makeTypeDef(def);
case Kind.INTERFACE_TYPE_DEFINITION:
return this._makeInterfaceDef(def);
case Kind.ENUM_TYPE_DEFINITION:
return this._makeEnumDef(def);
case Kind.UNION_TYPE_DEFINITION:
return this._makeUnionDef(def);
case Kind.SCALAR_TYPE_DEFINITION:
return this._makeScalarDef(def);
case Kind.INPUT_OBJECT_TYPE_DEFINITION:
return this._makeInputObjectDef(def);
default:
throw new Error(`Type kind "${def.kind}" not supported.`);
}
}
_makeTypeDef(def: ObjectTypeDefinitionNode) {
const interfaces: ?$ReadOnlyArray<NamedTypeNode> = def.interfaces;
return new GraphQLObjectType({
name: def.name.value,
description: getDescription(def, this._options),
fields: () => this._makeFieldDefMap(def),
// Note: While this could make early assertions to get the correctly
// typed values, that would throw immediately while type system
// validation with validateSchema() will produce more actionable results.
interfaces: interfaces
? () => interfaces.map(ref => (this.buildType(ref): any))
: [],
astNode: def,
});
}
_makeFieldDefMap(
def: ObjectTypeDefinitionNode | InterfaceTypeDefinitionNode,
) {
return def.fields
? keyValMap(
def.fields,
field => field.name.value,
field => this.buildField(field),
)
: {};
}
_makeInputValues(values: $ReadOnlyArray<InputValueDefinitionNode>) {
return keyValMap(
values,
value => value.name.value,
value => this.buildInputField(value),
);
}
_makeInterfaceDef(def: InterfaceTypeDefinitionNode) {
return new GraphQLInterfaceType({
name: def.name.value,
description: getDescription(def, this._options),
fields: () => this._makeFieldDefMap(def),
astNode: def,
});
}
_makeEnumDef(def: EnumTypeDefinitionNode) {
return new GraphQLEnumType({
name: def.name.value,
description: getDescription(def, this._options),
values: this._makeValueDefMap(def),
astNode: def,
});
}
_makeValueDefMap(def: EnumTypeDefinitionNode) {
return def.values
? keyValMap(
def.values,
enumValue => enumValue.name.value,
enumValue => this.buildEnumValue(enumValue),
)
: {};
}
_makeUnionDef(def: UnionTypeDefinitionNode) {
const types: ?$ReadOnlyArray<NamedTypeNode> = def.types;
return new GraphQLUnionType({
name: def.name.value,
description: getDescription(def, this._options),
// Note: While this could make assertions to get the correctly typed
// values below, that would throw immediately while type system
// validation with validateSchema() will produce more actionable results.
types: types ? () => types.map(ref => (this.buildType(ref): any)) : [],
astNode: def,
});
}
_makeScalarDef(def: ScalarTypeDefinitionNode) {
return new GraphQLScalarType({
name: def.name.value,
description: getDescription(def, this._options),
// Note: While this could make assertions to get the correctly typed
// values below, that would throw immediately while type system
// validation with validateSchema() will produce more actionable results.
ofType: def.type && (this.buildType(def.type): any),
astNode: def,
serialize: value => value,
});
}
_makeInputObjectDef(def: InputObjectTypeDefinitionNode) {
return new GraphQLInputObjectType({
name: def.name.value,
description: getDescription(def, this._options),
fields: () => (def.fields ? this._makeInputValues(def.fields) : {}),
astNode: def,
});
}
}
/**
* Given a field or enum value node, returns the string value for the
* deprecation reason.
*/
function getDeprecationReason(
node: EnumValueDefinitionNode | FieldDefinitionNode,
): ?string {
const deprecated = getDirectiveValues(GraphQLDeprecatedDirective, node);
return deprecated && (deprecated.reason: any);
}
/**
* Given an ast node, returns its string description.
* @deprecated: provided to ease adoption and will be removed in v16.
*
* Accepts options as a second argument:
*
* - commentDescriptions:
* Provide true to use preceding comments as the description.
*
*/
export function getDescription(
node: { +description?: StringValueNode, +loc?: Location },
options: ?BuildSchemaOptions,
): void | string {
if (node.description) {
return node.description.value;
}
if (options && options.commentDescriptions) {
const rawValue = getLeadingCommentBlock(node);
if (rawValue !== undefined) {
return blockStringValue('\n' + rawValue);
}
}
}
function getLeadingCommentBlock(node): void | string {
const loc = node.loc;
if (!loc) {
return;
}
const comments = [];
let token = loc.startToken.prev;
while (
token &&
token.kind === TokenKind.COMMENT &&
token.next &&
token.prev &&
token.line + 1 === token.next.line &&
token.line !== token.prev.line
) {
const value = String(token.value);
comments.push(value);
token = token.prev;
}
return comments.reverse().join('\n');
}
/**
* A helper function to build a GraphQLSchema directly from a source
* document.
*/
export function buildSchema(
source: string | Source,
options?: BuildSchemaOptions & ParseOptions,
): GraphQLSchema {
return buildASTSchema(parse(source, options), options);
}