-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathextendSchema.js
645 lines (587 loc) · 20.5 KB
/
extendSchema.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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
/**
* 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 objectValues from '../jsutils/objectValues';
import { ASTDefinitionBuilder } from './buildASTSchema';
import { assertValidSDLExtension } from '../validation/validate';
import { GraphQLError } from '../error/GraphQLError';
import { isSchema, GraphQLSchema } from '../type/schema';
import { isIntrospectionType } from '../type/introspection';
import { isSpecifiedScalarType } from '../type/scalars';
import type { GraphQLSchemaValidationOptions } from '../type/schema';
import type {
GraphQLType,
GraphQLNamedType,
GraphQLArgument,
GraphQLFieldConfigArgumentMap,
} from '../type/definition';
import {
isScalarType,
isObjectType,
isInterfaceType,
isUnionType,
isListType,
isNonNullType,
isEnumType,
isInputObjectType,
GraphQLList,
GraphQLNonNull,
GraphQLScalarType,
GraphQLObjectType,
GraphQLInterfaceType,
GraphQLUnionType,
GraphQLEnumType,
GraphQLInputObjectType,
} from '../type/definition';
import { GraphQLDirective } from '../type/directives';
import { Kind } from '../language/kinds';
import type {
DocumentNode,
DirectiveDefinitionNode,
SchemaExtensionNode,
SchemaDefinitionNode,
} from '../language/ast';
import {
isTypeDefinitionNode,
isTypeExtensionNode,
} from '../language/predicates';
type Options = {|
...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,
|};
/**
* Produces a new schema given an existing schema and a document which may
* contain GraphQL type extensions and definitions. The original schema will
* remain unaltered.
*
* Because a schema represents a graph of references, a schema cannot be
* extended without effectively making an entire copy. We do not know until it's
* too late if subgraphs remain unchanged.
*
* This algorithm copies the provided schema, applying extensions while
* producing the copy. The original schema remains unaltered.
*
* Accepts options as a third argument:
*
* - commentDescriptions:
* Provide true to use preceding comments as the description.
*
*/
export function extendSchema(
schema: GraphQLSchema,
documentAST: DocumentNode,
options?: Options,
): GraphQLSchema {
invariant(isSchema(schema), 'Must provide valid GraphQLSchema');
invariant(
documentAST && documentAST.kind === Kind.DOCUMENT,
'Must provide valid Document AST',
);
if (!options || !(options.assumeValid || options.assumeValidSDL)) {
assertValidSDLExtension(documentAST, schema);
}
// Collect the type definitions and extensions found in the document.
const typeDefinitionMap = Object.create(null);
const typeExtensionsMap = Object.create(null);
// New directives and types are separate because a directives and types can
// have the same name. For example, a type named "skip".
const directiveDefinitions: Array<DirectiveDefinitionNode> = [];
let schemaDef: ?SchemaDefinitionNode;
// Schema extensions are collected which may add additional operation types.
const schemaExtensions: Array<SchemaExtensionNode> = [];
for (let i = 0; i < documentAST.definitions.length; i++) {
const def = documentAST.definitions[i];
if (def.kind === Kind.SCHEMA_DEFINITION) {
schemaDef = def;
} else if (def.kind === Kind.SCHEMA_EXTENSION) {
schemaExtensions.push(def);
} else if (isTypeDefinitionNode(def)) {
// Sanity check that none of the defined types conflict with the
// schema's existing types.
const typeName = def.name.value;
if (schema.getType(typeName)) {
throw new GraphQLError(
`Type "${typeName}" already exists in the schema. It cannot also ` +
'be defined in this type definition.',
[def],
);
}
typeDefinitionMap[typeName] = def;
} else if (isTypeExtensionNode(def)) {
// Sanity check that this type extension exists within the
// schema's existing types.
const extendedTypeName = def.name.value;
const existingType = schema.getType(extendedTypeName);
if (!existingType) {
throw new GraphQLError(
`Cannot extend type "${extendedTypeName}" because it does not ` +
'exist in the existing schema.',
[def],
);
}
checkExtensionNode(existingType, def);
const existingTypeExtensions = typeExtensionsMap[extendedTypeName];
typeExtensionsMap[extendedTypeName] = existingTypeExtensions
? existingTypeExtensions.concat([def])
: [def];
} else if (def.kind === Kind.DIRECTIVE_DEFINITION) {
const directiveName = def.name.value;
const existingDirective = schema.getDirective(directiveName);
if (existingDirective) {
throw new GraphQLError(
`Directive "${directiveName}" already exists in the schema. It ` +
'cannot be redefined.',
[def],
);
}
directiveDefinitions.push(def);
}
}
// If this document contains no new types, extensions, or directives then
// return the same unmodified GraphQLSchema instance.
if (
Object.keys(typeExtensionsMap).length === 0 &&
Object.keys(typeDefinitionMap).length === 0 &&
directiveDefinitions.length === 0 &&
schemaExtensions.length === 0 &&
!schemaDef
) {
return schema;
}
const astBuilder = new ASTDefinitionBuilder(
typeDefinitionMap,
options,
typeRef => {
const typeName = typeRef.name.value;
const existingType = schema.getType(typeName);
if (existingType) {
return extendNamedType(existingType);
}
throw new GraphQLError(
`Unknown type: "${typeName}". Ensure that this type exists ` +
'either in the original schema, or is added in a type definition.',
[typeRef],
);
},
);
const extendTypeCache = Object.create(null);
// Get the extended root operation types.
const operationTypes = {
query: extendMaybeNamedType(schema.getQueryType()),
mutation: extendMaybeNamedType(schema.getMutationType()),
subscription: extendMaybeNamedType(schema.getSubscriptionType()),
};
if (schemaDef) {
for (const { operation, type } of schemaDef.operationTypes) {
if (operationTypes[operation]) {
throw new Error(`Must provide only one ${operation} type in schema.`);
}
// 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.
operationTypes[operation] = (astBuilder.buildType(type): any);
}
}
// Then, incorporate schema definition and all schema extensions.
for (const schemaExtension of schemaExtensions) {
if (schemaExtension.operationTypes) {
for (const { operation, type } of schemaExtension.operationTypes) {
if (operationTypes[operation]) {
throw new Error(`Must provide only one ${operation} type in schema.`);
}
// 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.
operationTypes[operation] = (astBuilder.buildType(type): any);
}
}
}
const schemaExtensionASTNodes = schemaExtensions
? schema.extensionASTNodes
? schema.extensionASTNodes.concat(schemaExtensions)
: schemaExtensions
: schema.extensionASTNodes;
const types = [
// Iterate through all types, getting the type definition for each, ensuring
// that any type not directly referenced by a field will get created.
...objectValues(schema.getTypeMap()).map(type => extendNamedType(type)),
// Do the same with new types.
...objectValues(typeDefinitionMap).map(type => astBuilder.buildType(type)),
];
// Support both original legacy names and extended legacy names.
const allowedLegacyNames = schema.__allowedLegacyNames.concat(
(options && options.allowedLegacyNames) || [],
);
// Then produce and return a Schema with these types.
return new GraphQLSchema({
...operationTypes,
types,
directives: getMergedDirectives(),
astNode: schema.astNode,
extensionASTNodes: schemaExtensionASTNodes,
allowedLegacyNames,
});
// Below are functions used for producing this schema that have closed over
// this scope and have access to the schema, cache, and newly defined types.
function getMergedDirectives(): Array<GraphQLDirective> {
const existingDirectives = schema.getDirectives().map(extendDirective);
invariant(existingDirectives, 'schema must have default directives');
return existingDirectives.concat(
directiveDefinitions.map(node => astBuilder.buildDirective(node)),
);
}
function extendMaybeNamedType<T: GraphQLNamedType>(type: ?T): ?T {
return type ? extendNamedType(type) : null;
}
function extendNamedType<T: GraphQLNamedType>(type: T): T {
if (isIntrospectionType(type) || isSpecifiedScalarType(type)) {
// Builtin types are not extended.
return type;
}
const name = type.name;
if (!extendTypeCache[name]) {
if (isScalarType(type)) {
extendTypeCache[name] = extendScalarType(type);
} else if (isObjectType(type)) {
extendTypeCache[name] = extendObjectType(type);
} else if (isInterfaceType(type)) {
extendTypeCache[name] = extendInterfaceType(type);
} else if (isUnionType(type)) {
extendTypeCache[name] = extendUnionType(type);
} else if (isEnumType(type)) {
extendTypeCache[name] = extendEnumType(type);
} else if (isInputObjectType(type)) {
extendTypeCache[name] = extendInputObjectType(type);
}
}
return (extendTypeCache[name]: any);
}
function extendDirective(directive: GraphQLDirective): GraphQLDirective {
return new GraphQLDirective({
name: directive.name,
description: directive.description,
locations: directive.locations,
args: extendArgs(directive.args),
astNode: directive.astNode,
});
}
function extendInputObjectType(
type: GraphQLInputObjectType,
): GraphQLInputObjectType {
const name = type.name;
const extensionASTNodes = typeExtensionsMap[name]
? type.extensionASTNodes
? type.extensionASTNodes.concat(typeExtensionsMap[name])
: typeExtensionsMap[name]
: type.extensionASTNodes;
return new GraphQLInputObjectType({
name,
description: type.description,
fields: () => extendInputFieldMap(type),
astNode: type.astNode,
extensionASTNodes,
});
}
function extendInputFieldMap(type: GraphQLInputObjectType) {
const newFieldMap = Object.create(null);
const oldFieldMap = type.getFields();
for (const fieldName of Object.keys(oldFieldMap)) {
const field = oldFieldMap[fieldName];
newFieldMap[fieldName] = {
description: field.description,
type: extendType(field.type),
defaultValue: field.defaultValue,
astNode: field.astNode,
};
}
// If there are any extensions to the fields, apply those here.
const extensions = typeExtensionsMap[type.name];
if (extensions) {
for (const extension of extensions) {
for (const field of extension.fields) {
const fieldName = field.name.value;
if (oldFieldMap[fieldName]) {
throw new GraphQLError(
`Field "${type.name}.${fieldName}" already exists in the ` +
'schema. It cannot also be defined in this type extension.',
[field],
);
}
newFieldMap[fieldName] = astBuilder.buildInputField(field);
}
}
}
return newFieldMap;
}
function extendEnumType(type: GraphQLEnumType): GraphQLEnumType {
const name = type.name;
const extensionASTNodes = typeExtensionsMap[name]
? type.extensionASTNodes
? type.extensionASTNodes.concat(typeExtensionsMap[name])
: typeExtensionsMap[name]
: type.extensionASTNodes;
return new GraphQLEnumType({
name,
description: type.description,
values: extendValueMap(type),
astNode: type.astNode,
extensionASTNodes,
});
}
function extendValueMap(type: GraphQLEnumType) {
const newValueMap = Object.create(null);
const oldValueMap = keyMap(type.getValues(), value => value.name);
for (const valueName of Object.keys(oldValueMap)) {
const value = oldValueMap[valueName];
newValueMap[valueName] = {
name: value.name,
description: value.description,
value: value.value,
deprecationReason: value.deprecationReason,
astNode: value.astNode,
};
}
// If there are any extensions to the values, apply those here.
const extensions = typeExtensionsMap[type.name];
if (extensions) {
for (const extension of extensions) {
for (const value of extension.values) {
const valueName = value.name.value;
if (oldValueMap[valueName]) {
throw new GraphQLError(
`Enum value "${type.name}.${valueName}" already exists in the ` +
'schema. It cannot also be defined in this type extension.',
[value],
);
}
newValueMap[valueName] = astBuilder.buildEnumValue(value);
}
}
}
return newValueMap;
}
function extendScalarType(type: GraphQLScalarType): GraphQLScalarType {
const name = type.name;
const extensionASTNodes = typeExtensionsMap[name]
? type.extensionASTNodes
? type.extensionASTNodes.concat(typeExtensionsMap[name])
: typeExtensionsMap[name]
: type.extensionASTNodes;
return new GraphQLScalarType({
name,
description: type.description,
astNode: type.astNode,
extensionASTNodes,
serialize: type.serialize,
parseValue: type.parseValue,
parseLiteral: type.parseLiteral,
});
}
function extendObjectType(type: GraphQLObjectType): GraphQLObjectType {
const name = type.name;
const extensionASTNodes = typeExtensionsMap[name]
? type.extensionASTNodes
? type.extensionASTNodes.concat(typeExtensionsMap[name])
: typeExtensionsMap[name]
: type.extensionASTNodes;
return new GraphQLObjectType({
name,
description: type.description,
interfaces: () => extendImplementedInterfaces(type),
fields: () => extendFieldMap(type),
astNode: type.astNode,
extensionASTNodes,
isTypeOf: type.isTypeOf,
});
}
function extendArgs(
args: Array<GraphQLArgument>,
): GraphQLFieldConfigArgumentMap {
return keyValMap(
args,
arg => arg.name,
arg => ({
type: extendType(arg.type),
defaultValue: arg.defaultValue,
description: arg.description,
astNode: arg.astNode,
}),
);
}
function extendInterfaceType(
type: GraphQLInterfaceType,
): GraphQLInterfaceType {
const name = type.name;
const extensionASTNodes = typeExtensionsMap[name]
? type.extensionASTNodes
? type.extensionASTNodes.concat(typeExtensionsMap[name])
: typeExtensionsMap[name]
: type.extensionASTNodes;
return new GraphQLInterfaceType({
name: type.name,
description: type.description,
fields: () => extendFieldMap(type),
astNode: type.astNode,
extensionASTNodes,
resolveType: type.resolveType,
});
}
function extendUnionType(type: GraphQLUnionType): GraphQLUnionType {
const name = type.name;
const extensionASTNodes = typeExtensionsMap[name]
? type.extensionASTNodes
? type.extensionASTNodes.concat(typeExtensionsMap[name])
: typeExtensionsMap[name]
: type.extensionASTNodes;
return new GraphQLUnionType({
name,
description: type.description,
types: () => extendPossibleTypes(type),
astNode: type.astNode,
resolveType: type.resolveType,
extensionASTNodes,
});
}
function extendPossibleTypes(
type: GraphQLUnionType,
): Array<GraphQLObjectType> {
const possibleTypes = type.getTypes().map(extendNamedType);
// If there are any extensions to the union, apply those here.
const extensions = typeExtensionsMap[type.name];
if (extensions) {
for (const extension of extensions) {
for (const namedType of extension.types) {
// 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.
possibleTypes.push((astBuilder.buildType(namedType): any));
}
}
}
return possibleTypes;
}
function extendImplementedInterfaces(
type: GraphQLObjectType,
): Array<GraphQLInterfaceType> {
const interfaces = type.getInterfaces().map(extendNamedType);
// If there are any extensions to the interfaces, apply those here.
const extensions = typeExtensionsMap[type.name];
if (extensions) {
for (const extension of extensions) {
for (const namedType of extension.interfaces) {
// 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.push((astBuilder.buildType(namedType): any));
}
}
}
return interfaces;
}
function extendFieldMap(type: GraphQLObjectType | GraphQLInterfaceType) {
const newFieldMap = Object.create(null);
const oldFieldMap = type.getFields();
for (const fieldName of Object.keys(oldFieldMap)) {
const field = oldFieldMap[fieldName];
newFieldMap[fieldName] = {
description: field.description,
deprecationReason: field.deprecationReason,
type: extendType(field.type),
args: extendArgs(field.args),
astNode: field.astNode,
resolve: field.resolve,
};
}
// If there are any extensions to the fields, apply those here.
const extensions = typeExtensionsMap[type.name];
if (extensions) {
for (const extension of extensions) {
for (const field of extension.fields) {
const fieldName = field.name.value;
if (oldFieldMap[fieldName]) {
throw new GraphQLError(
`Field "${type.name}.${fieldName}" already exists in the ` +
'schema. It cannot also be defined in this type extension.',
[field],
);
}
newFieldMap[fieldName] = astBuilder.buildField(field);
}
}
}
return newFieldMap;
}
function extendType<T: GraphQLType>(typeDef: T): T {
if (isListType(typeDef)) {
return (GraphQLList(extendType(typeDef.ofType)): any);
}
if (isNonNullType(typeDef)) {
return (GraphQLNonNull(extendType(typeDef.ofType)): any);
}
return extendNamedType(typeDef);
}
}
function checkExtensionNode(type, node) {
switch (node.kind) {
case Kind.OBJECT_TYPE_EXTENSION:
if (!isObjectType(type)) {
throw new GraphQLError(
`Cannot extend non-object type "${type.name}".`,
[node],
);
}
break;
case Kind.INTERFACE_TYPE_EXTENSION:
if (!isInterfaceType(type)) {
throw new GraphQLError(
`Cannot extend non-interface type "${type.name}".`,
[node],
);
}
break;
case Kind.ENUM_TYPE_EXTENSION:
if (!isEnumType(type)) {
throw new GraphQLError(`Cannot extend non-enum type "${type.name}".`, [
node,
]);
}
break;
case Kind.UNION_TYPE_EXTENSION:
if (!isUnionType(type)) {
throw new GraphQLError(`Cannot extend non-union type "${type.name}".`, [
node,
]);
}
break;
case Kind.INPUT_OBJECT_TYPE_EXTENSION:
if (!isInputObjectType(type)) {
throw new GraphQLError(
`Cannot extend non-input object type "${type.name}".`,
[node],
);
}
break;
}
}