@apollo/federation-internals 2.1.0-alpha.2 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. package/CHANGELOG.md +5 -4
  2. package/dist/buildSchema.d.ts.map +1 -1
  3. package/dist/buildSchema.js +14 -4
  4. package/dist/buildSchema.js.map +1 -1
  5. package/dist/coreSpec.d.ts +1 -4
  6. package/dist/coreSpec.d.ts.map +1 -1
  7. package/dist/coreSpec.js +1 -5
  8. package/dist/coreSpec.js.map +1 -1
  9. package/dist/definitions.d.ts +66 -34
  10. package/dist/definitions.d.ts.map +1 -1
  11. package/dist/definitions.js +309 -165
  12. package/dist/definitions.js.map +1 -1
  13. package/dist/error.d.ts +5 -0
  14. package/dist/error.d.ts.map +1 -1
  15. package/dist/error.js +47 -1
  16. package/dist/error.js.map +1 -1
  17. package/dist/extractSubgraphsFromSupergraph.d.ts.map +1 -1
  18. package/dist/extractSubgraphsFromSupergraph.js +135 -5
  19. package/dist/extractSubgraphsFromSupergraph.js.map +1 -1
  20. package/dist/federation.d.ts +3 -2
  21. package/dist/federation.d.ts.map +1 -1
  22. package/dist/federation.js +12 -7
  23. package/dist/federation.js.map +1 -1
  24. package/dist/inaccessibleSpec.js +1 -2
  25. package/dist/inaccessibleSpec.js.map +1 -1
  26. package/dist/operations.d.ts +44 -20
  27. package/dist/operations.d.ts.map +1 -1
  28. package/dist/operations.js +287 -27
  29. package/dist/operations.js.map +1 -1
  30. package/dist/print.d.ts +1 -1
  31. package/dist/print.d.ts.map +1 -1
  32. package/dist/print.js +1 -1
  33. package/dist/print.js.map +1 -1
  34. package/dist/schemaUpgrader.d.ts.map +1 -1
  35. package/dist/schemaUpgrader.js +6 -6
  36. package/dist/schemaUpgrader.js.map +1 -1
  37. package/dist/supergraphs.d.ts +1 -3
  38. package/dist/supergraphs.d.ts.map +1 -1
  39. package/dist/supergraphs.js +9 -22
  40. package/dist/supergraphs.js.map +1 -1
  41. package/dist/utils.d.ts +10 -1
  42. package/dist/utils.d.ts.map +1 -1
  43. package/dist/utils.js +40 -1
  44. package/dist/utils.js.map +1 -1
  45. package/package.json +3 -4
  46. package/src/__tests__/coreSpec.test.ts +1 -1
  47. package/src/__tests__/definitions.test.ts +27 -0
  48. package/src/__tests__/extractSubgraphsFromSupergraph.test.ts +9 -5
  49. package/src/__tests__/operations.test.ts +36 -0
  50. package/src/__tests__/removeInaccessibleElements.test.ts +1 -3
  51. package/src/__tests__/schemaUpgrader.test.ts +0 -1
  52. package/src/__tests__/subgraphValidation.test.ts +1 -2
  53. package/src/buildSchema.ts +20 -7
  54. package/src/coreSpec.ts +2 -7
  55. package/src/definitions.ts +355 -155
  56. package/src/error.ts +62 -0
  57. package/src/extractSubgraphsFromSupergraph.ts +198 -7
  58. package/src/federation.ts +11 -4
  59. package/src/inaccessibleSpec.ts +2 -5
  60. package/src/operations.ts +428 -40
  61. package/src/print.ts +3 -3
  62. package/src/schemaUpgrader.ts +7 -6
  63. package/src/supergraphs.ts +16 -25
  64. package/src/utils.ts +49 -0
  65. package/tsconfig.test.tsbuildinfo +1 -1
  66. package/tsconfig.tsbuildinfo +1 -1
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CoreFeatures = exports.CoreFeature = exports.defaultSchemaBlueprint = exports.SchemaBlueprint = exports.NamedSchemaElementWithType = exports.NamedSchemaElement = exports.SchemaElement = exports.Extension = exports.sourceASTs = exports.DirectiveTargetElement = exports.isLeafType = exports.typeFromAST = exports.typeToAST = exports.executableDirectiveLocations = exports.isConditionalDirective = exports.runtimeTypesIntersects = exports.possibleRuntimeTypes = exports.isCompositeType = exports.isAbstractType = exports.isNullableType = exports.baseType = exports.filterTypesOfKind = exports.isTypeOfKind = exports.isInputType = exports.isOutputType = exports.isInputObjectType = exports.isUnionType = exports.isEnumType = exports.isInterfaceType = exports.isObjectType = exports.isIDType = exports.isBooleanType = exports.isFloatType = exports.isStringType = exports.isIntType = exports.isCustomScalarType = exports.isScalarType = exports.isNonNullType = exports.isListType = exports.isWrapperType = exports.isNamedType = exports.isSchemaRootType = exports.defaultRootName = exports.allSchemaRootKinds = exports.typenameFieldName = exports.printErrors = exports.printGraphQLErrorsOrRethrow = exports.errorCauses = exports.ErrGraphQLAPISchemaValidationFailed = exports.ErrGraphQLValidationFailed = void 0;
4
- exports.newNamedType = exports.variableDefinitionFromAST = exports.variableDefinitionsFromAST = exports.VariableDefinitions = exports.VariableDefinition = exports.variablesInArguments = exports.isVariable = exports.containsVariable = exports.mergeVariables = exports.Variable = exports.directiveApplicationsSubstraction = exports.isDirectiveApplicationsSubset = exports.sameDirectiveApplications = exports.sameDirectiveApplication = exports.Directive = exports.DirectiveDefinition = exports.EnumValue = exports.ArgumentDefinition = exports.InputFieldDefinition = exports.FieldDefinition = exports.NonNullType = exports.ListType = exports.InputObjectType = exports.EnumType = exports.UnionType = exports.UnionMember = exports.InterfaceType = exports.ObjectType = exports.InterfaceImplementation = exports.ScalarType = exports.SchemaDefinition = exports.RootType = exports.Schema = void 0;
3
+ exports.CoreFeatures = exports.CoreFeature = exports.defaultSchemaBlueprint = exports.SchemaBlueprint = exports.NamedSchemaElementWithType = exports.NamedSchemaElement = exports.SchemaElement = exports.Extension = exports.sourceASTs = exports.DirectiveTargetElement = exports.isLeafType = exports.typeFromAST = exports.typeToAST = exports.isTypeSystemDirectiveLocation = exports.typeSystemDirectiveLocations = exports.isExecutableDirectiveLocation = exports.executableDirectiveLocations = exports.isConditionalDirective = exports.runtimeTypesIntersects = exports.possibleRuntimeTypes = exports.isCompositeType = exports.isAbstractType = exports.isNullableType = exports.baseType = exports.filterTypesOfKind = exports.isTypeOfKind = exports.isInputType = exports.isOutputType = exports.isInputObjectType = exports.isUnionType = exports.isEnumType = exports.isInterfaceType = exports.isObjectType = exports.isIDType = exports.isBooleanType = exports.isFloatType = exports.isStringType = exports.isIntType = exports.isCustomScalarType = exports.isScalarType = exports.isNonNullType = exports.isListType = exports.isWrapperType = exports.isNamedType = exports.isSchemaRootType = exports.defaultRootName = exports.allSchemaRootKinds = exports.typenameFieldName = exports.ErrGraphQLAPISchemaValidationFailed = exports.ErrGraphQLValidationFailed = void 0;
4
+ exports.copyDirectiveDefinitionToSchema = exports.newNamedType = exports.variableDefinitionFromAST = exports.variableDefinitionsFromAST = exports.VariableDefinitions = exports.VariableDefinition = exports.variablesInArguments = exports.isVariable = exports.containsVariable = exports.mergeVariables = exports.Variable = exports.directiveApplicationsSubstraction = exports.isDirectiveApplicationsSubset = exports.sameDirectiveApplications = exports.sameDirectiveApplication = exports.Directive = exports.DirectiveDefinition = exports.EnumValue = exports.ArgumentDefinition = exports.InputFieldDefinition = exports.FieldDefinition = exports.NonNullType = exports.ListType = exports.InputObjectType = exports.EnumType = exports.UnionType = exports.UnionMember = exports.InterfaceType = exports.ObjectType = exports.InterfaceImplementation = exports.ScalarType = exports.SchemaDefinition = exports.RootType = exports.Schema = void 0;
5
5
  const graphql_1 = require("graphql");
6
6
  const coreSpec_1 = require("./coreSpec");
7
7
  const utils_1 = require("./utils");
@@ -10,52 +10,19 @@ const inaccessibleSpec_1 = require("./inaccessibleSpec");
10
10
  const print_1 = require("./print");
11
11
  const types_1 = require("./types");
12
12
  const introspection_1 = require("./introspection");
13
- const core_schema_1 = require("@apollo/core-schema");
14
- const error_1 = require("@apollo/core-schema/dist/error");
15
13
  const validate_1 = require("graphql/validation/validate");
16
14
  const specifiedRules_1 = require("graphql/validation/specifiedRules");
17
15
  const validate_2 = require("./validate");
18
16
  const directiveAndTypeSpecification_1 = require("./directiveAndTypeSpecification");
19
17
  const suggestions_1 = require("./suggestions");
20
- const error_2 = require("./error");
18
+ const error_1 = require("./error");
21
19
  const validationErrorCode = 'GraphQLValidationFailed';
22
20
  const DEFAULT_VALIDATION_ERROR_MESSAGE = 'The schema is not a valid GraphQL schema.';
23
- const ErrGraphQLValidationFailed = (causes, message = DEFAULT_VALIDATION_ERROR_MESSAGE) => (0, core_schema_1.err)(validationErrorCode, {
24
- message: message + '. Caused by:\n' + causes.map((c) => c.toString()).join('\n\n'),
25
- causes
26
- });
21
+ const ErrGraphQLValidationFailed = (causes, message = DEFAULT_VALIDATION_ERROR_MESSAGE) => (0, error_1.aggregateError)(validationErrorCode, message, causes);
27
22
  exports.ErrGraphQLValidationFailed = ErrGraphQLValidationFailed;
28
23
  const apiSchemaValidationErrorCode = 'GraphQLAPISchemaValidationFailed';
29
- const ErrGraphQLAPISchemaValidationFailed = (causes) => (0, core_schema_1.err)(apiSchemaValidationErrorCode, {
30
- message: 'The supergraph schema failed to produce a valid API schema',
31
- causes
32
- });
24
+ const ErrGraphQLAPISchemaValidationFailed = (causes) => (0, error_1.aggregateError)(apiSchemaValidationErrorCode, 'The supergraph schema failed to produce a valid API schema', causes);
33
25
  exports.ErrGraphQLAPISchemaValidationFailed = ErrGraphQLAPISchemaValidationFailed;
34
- function errorCauses(e) {
35
- if (e instanceof error_1.GraphQLErrorExt) {
36
- if (e.code === validationErrorCode || e.code === apiSchemaValidationErrorCode) {
37
- return (e.causes);
38
- }
39
- return [e];
40
- }
41
- if (e instanceof graphql_1.GraphQLError) {
42
- return [e];
43
- }
44
- return undefined;
45
- }
46
- exports.errorCauses = errorCauses;
47
- function printGraphQLErrorsOrRethrow(e) {
48
- const causes = errorCauses(e);
49
- if (!causes) {
50
- throw e;
51
- }
52
- return causes.map(e => e.toString()).join('\n\n');
53
- }
54
- exports.printGraphQLErrorsOrRethrow = printGraphQLErrorsOrRethrow;
55
- function printErrors(errors) {
56
- return errors.map(e => e.toString()).join('\n\n');
57
- }
58
- exports.printErrors = printErrors;
59
26
  exports.typenameFieldName = '__typename';
60
27
  exports.allSchemaRootKinds = ['query', 'mutation', 'subscription'];
61
28
  function defaultRootName(rootKind) {
@@ -227,6 +194,29 @@ exports.executableDirectiveLocations = [
227
194
  graphql_1.DirectiveLocation.INLINE_FRAGMENT,
228
195
  graphql_1.DirectiveLocation.VARIABLE_DEFINITION,
229
196
  ];
197
+ const executableDirectiveLocationsSet = new Set(exports.executableDirectiveLocations);
198
+ function isExecutableDirectiveLocation(loc) {
199
+ return executableDirectiveLocationsSet.has(loc);
200
+ }
201
+ exports.isExecutableDirectiveLocation = isExecutableDirectiveLocation;
202
+ exports.typeSystemDirectiveLocations = [
203
+ graphql_1.DirectiveLocation.SCHEMA,
204
+ graphql_1.DirectiveLocation.SCALAR,
205
+ graphql_1.DirectiveLocation.OBJECT,
206
+ graphql_1.DirectiveLocation.FIELD_DEFINITION,
207
+ graphql_1.DirectiveLocation.ARGUMENT_DEFINITION,
208
+ graphql_1.DirectiveLocation.INTERFACE,
209
+ graphql_1.DirectiveLocation.UNION,
210
+ graphql_1.DirectiveLocation.ENUM,
211
+ graphql_1.DirectiveLocation.ENUM_VALUE,
212
+ graphql_1.DirectiveLocation.INPUT_OBJECT,
213
+ graphql_1.DirectiveLocation.INPUT_FIELD_DEFINITION,
214
+ ];
215
+ const typeSystemDirectiveLocationsSet = new Set(exports.typeSystemDirectiveLocations);
216
+ function isTypeSystemDirectiveLocation(loc) {
217
+ return typeSystemDirectiveLocationsSet.has(loc);
218
+ }
219
+ exports.isTypeSystemDirectiveLocation = isTypeSystemDirectiveLocation;
230
220
  function typeToAST(type) {
231
221
  switch (type.kind) {
232
222
  case 'ListType':
@@ -256,7 +246,7 @@ function typeFromAST(schema, node) {
256
246
  default:
257
247
  const type = schema.type(node.name.value);
258
248
  if (!type) {
259
- throw error_2.ERRORS.INVALID_GRAPHQL.err(`Unknown type "${node.name.value}"`, { nodes: node });
249
+ throw error_1.ERRORS.INVALID_GRAPHQL.err(`Unknown type "${node.name.value}"`, { nodes: node });
260
250
  }
261
251
  return type;
262
252
  }
@@ -269,7 +259,6 @@ exports.isLeafType = isLeafType;
269
259
  class DirectiveTargetElement {
270
260
  constructor(_schema) {
271
261
  this._schema = _schema;
272
- this.appliedDirectives = [];
273
262
  }
274
263
  schema() {
275
264
  return this._schema;
@@ -278,6 +267,10 @@ class DirectiveTargetElement {
278
267
  const directiveName = typeof nameOrDefinition === 'string' ? nameOrDefinition : nameOrDefinition.name;
279
268
  return this.appliedDirectives.filter(d => d.name == directiveName);
280
269
  }
270
+ get appliedDirectives() {
271
+ var _a;
272
+ return (_a = this._appliedDirectives) !== null && _a !== void 0 ? _a : [];
273
+ }
281
274
  hasAppliedDirective(nameOrDefinition) {
282
275
  const directiveName = typeof nameOrDefinition === 'string' ? nameOrDefinition : nameOrDefinition.name;
283
276
  return this.appliedDirectives.some(d => d.name == directiveName);
@@ -297,7 +290,12 @@ class DirectiveTargetElement {
297
290
  toAdd = new Directive(defOrDirective.name, args !== null && args !== void 0 ? args : Object.create(null));
298
291
  }
299
292
  Element.prototype['setParent'].call(toAdd, this);
300
- this.appliedDirectives.push(toAdd);
293
+ if (this._appliedDirectives) {
294
+ this._appliedDirectives.push(toAdd);
295
+ }
296
+ else {
297
+ this._appliedDirectives = [toAdd];
298
+ }
301
299
  return toAdd;
302
300
  }
303
301
  appliedDirectivesToDirectiveNodes() {
@@ -379,16 +377,36 @@ class Extension {
379
377
  }
380
378
  exports.Extension = Extension;
381
379
  class SchemaElement extends Element {
382
- constructor() {
383
- super(...arguments);
384
- this._appliedDirectives = [];
380
+ addUnappliedDirective({ nameOrDef, args, extension, directive }) {
381
+ const toAdd = {
382
+ nameOrDef,
383
+ args: args !== null && args !== void 0 ? args : {},
384
+ extension,
385
+ directive,
386
+ };
387
+ if (this._unappliedDirectives) {
388
+ this._unappliedDirectives.push(toAdd);
389
+ }
390
+ else {
391
+ this._unappliedDirectives = [toAdd];
392
+ }
393
+ }
394
+ processUnappliedDirectives() {
395
+ var _a;
396
+ for (const { nameOrDef, args, extension, directive } of (_a = this._unappliedDirectives) !== null && _a !== void 0 ? _a : []) {
397
+ const d = this.applyDirective(nameOrDef, args);
398
+ d.setOfExtension(extension);
399
+ d.sourceAST = directive;
400
+ }
401
+ this._unappliedDirectives = undefined;
385
402
  }
386
403
  get appliedDirectives() {
387
- return this._appliedDirectives;
404
+ var _a;
405
+ return (_a = this._appliedDirectives) !== null && _a !== void 0 ? _a : [];
388
406
  }
389
407
  appliedDirectivesOf(nameOrDefinition) {
390
408
  const directiveName = typeof nameOrDefinition === 'string' ? nameOrDefinition : nameOrDefinition.name;
391
- return this._appliedDirectives.filter(d => d.name == directiveName);
409
+ return this.appliedDirectives.filter(d => d.name == directiveName);
392
410
  }
393
411
  hasAppliedDirective(nameOrDefinition) {
394
412
  return (typeof nameOrDefinition === 'string'
@@ -402,7 +420,7 @@ class SchemaElement extends Element {
402
420
  this.checkUpdate();
403
421
  const def = (_a = this.schema().directive(nameOrDef)) !== null && _a !== void 0 ? _a : this.schema().blueprint.onMissingDirectiveDefinition(this.schema(), nameOrDef, args);
404
422
  if (!def) {
405
- throw this.schema().blueprint.onGraphQLJSValidationError(this.schema(), error_2.ERRORS.INVALID_GRAPHQL.err(`Unknown directive "@${nameOrDef}".`));
423
+ throw this.schema().blueprint.onGraphQLJSValidationError(this.schema(), error_1.ERRORS.INVALID_GRAPHQL.err(`Unknown directive "@${nameOrDef}".`));
406
424
  }
407
425
  if (Array.isArray(def)) {
408
426
  throw (0, exports.ErrGraphQLValidationFailed)(def);
@@ -415,17 +433,25 @@ class SchemaElement extends Element {
415
433
  }
416
434
  const toAdd = new Directive(name, args !== null && args !== void 0 ? args : Object.create(null));
417
435
  Element.prototype['setParent'].call(toAdd, this);
418
- if (asFirstDirective) {
419
- this._appliedDirectives.unshift(toAdd);
436
+ if (this._appliedDirectives) {
437
+ if (asFirstDirective) {
438
+ this._appliedDirectives.unshift(toAdd);
439
+ }
440
+ else {
441
+ this._appliedDirectives.push(toAdd);
442
+ }
420
443
  }
421
444
  else {
422
- this._appliedDirectives.push(toAdd);
445
+ this._appliedDirectives = [toAdd];
423
446
  }
424
447
  DirectiveDefinition.prototype['addReferencer'].call(toAdd.definition, toAdd);
425
448
  this.onModification();
426
449
  return toAdd;
427
450
  }
428
451
  removeAppliedDirectives() {
452
+ if (!this._appliedDirectives) {
453
+ return;
454
+ }
429
455
  const applied = this._appliedDirectives.concat();
430
456
  applied.forEach(d => d.remove());
431
457
  }
@@ -474,15 +500,22 @@ class BaseNamedType extends NamedSchemaElement {
474
500
  constructor(name, isBuiltIn = false) {
475
501
  super(name);
476
502
  this.isBuiltIn = isBuiltIn;
477
- this._referencers = new Set();
478
- this._extensions = new Set();
479
503
  this.preserveEmptyDefinition = false;
480
504
  }
481
505
  addReferencer(referencer) {
482
- this._referencers.add(referencer);
506
+ if (this._referencers) {
507
+ if (!this._referencers.includes(referencer)) {
508
+ this._referencers.push(referencer);
509
+ }
510
+ }
511
+ else {
512
+ this._referencers = [referencer];
513
+ }
483
514
  }
484
515
  removeReferencer(referencer) {
485
- this._referencers.delete(referencer);
516
+ if (this._referencers) {
517
+ (0, utils_1.removeArrayElement)(referencer, this._referencers);
518
+ }
486
519
  }
487
520
  get coordinate() {
488
521
  return this.name;
@@ -490,28 +523,38 @@ class BaseNamedType extends NamedSchemaElement {
490
523
  *allChildElements() {
491
524
  }
492
525
  extensions() {
493
- return this._extensions;
526
+ var _a;
527
+ return (_a = this._extensions) !== null && _a !== void 0 ? _a : [];
528
+ }
529
+ hasExtension(extension) {
530
+ var _a, _b;
531
+ return (_b = (_a = this._extensions) === null || _a === void 0 ? void 0 : _a.includes(extension)) !== null && _b !== void 0 ? _b : false;
494
532
  }
495
533
  newExtension() {
496
534
  return this.addExtension(new Extension());
497
535
  }
498
536
  addExtension(extension) {
499
537
  this.checkUpdate();
500
- if (this._extensions.has(extension)) {
538
+ if (this.hasExtension(extension)) {
501
539
  return extension;
502
540
  }
503
541
  (0, utils_1.assert)(!extension.extendedElement, () => `Cannot add extension to type ${this}: it is already added to another type`);
504
- this._extensions.add(extension);
542
+ if (this._extensions) {
543
+ this._extensions.push(extension);
544
+ }
545
+ else {
546
+ this._extensions = [extension];
547
+ }
505
548
  Extension.prototype['setExtendedElement'].call(extension, this);
506
549
  this.onModification();
507
550
  return extension;
508
551
  }
509
552
  removeExtensions() {
510
- if (this._extensions.size === 0) {
553
+ if (!this._extensions) {
511
554
  return;
512
555
  }
513
- this._extensions.clear();
514
- for (const directive of this._appliedDirectives) {
556
+ this._extensions = undefined;
557
+ for (const directive of this.appliedDirectives) {
515
558
  directive.removeOfExtension();
516
559
  }
517
560
  this.removeInnerElementsExtensions();
@@ -520,11 +563,11 @@ class BaseNamedType extends NamedSchemaElement {
520
563
  return (0, introspection_1.isIntrospectionName)(this.name);
521
564
  }
522
565
  hasExtensionElements() {
523
- return this._extensions.size > 0;
566
+ return !!this._extensions;
524
567
  }
525
568
  hasNonExtensionElements() {
526
569
  return this.preserveEmptyDefinition
527
- || this._appliedDirectives.some(d => d.ofExtension() === undefined)
570
+ || this.appliedDirectives.some(d => d.ofExtension() === undefined)
528
571
  || this.hasNonExtensionInnerElements();
529
572
  }
530
573
  isElementBuiltIn() {
@@ -538,6 +581,7 @@ class BaseNamedType extends NamedSchemaElement {
538
581
  this.onModification();
539
582
  }
540
583
  remove() {
584
+ var _a, _b;
541
585
  if (!this._parent) {
542
586
  return [];
543
587
  }
@@ -546,11 +590,11 @@ class BaseNamedType extends NamedSchemaElement {
546
590
  this.sourceAST = undefined;
547
591
  this.removeAppliedDirectives();
548
592
  this.removeInnerElements();
549
- const toReturn = (0, utils_1.setValues)(this._referencers).map(r => {
593
+ const toReturn = (_b = (_a = this._referencers) === null || _a === void 0 ? void 0 : _a.map(r => {
550
594
  SchemaElement.prototype['removeTypeReferenceInternal'].call(r, this);
551
595
  return r;
552
- });
553
- this._referencers.clear();
596
+ })) !== null && _b !== void 0 ? _b : [];
597
+ this._referencers = undefined;
554
598
  Schema.prototype['removeTypeInternal'].call(this._parent, this);
555
599
  this._parent = undefined;
556
600
  return toReturn;
@@ -559,10 +603,11 @@ class BaseNamedType extends NamedSchemaElement {
559
603
  this.remove().forEach(ref => this.removeReferenceRecursive(ref));
560
604
  }
561
605
  referencers() {
562
- return (0, utils_1.setValues)(this._referencers);
606
+ var _a;
607
+ return (_a = this._referencers) !== null && _a !== void 0 ? _a : [];
563
608
  }
564
609
  isReferenced() {
565
- return this._referencers.size > 0;
610
+ return !!this._referencers;
566
611
  }
567
612
  toString() {
568
613
  return this.name;
@@ -603,7 +648,7 @@ class BaseExtensionMember extends Element {
603
648
  setOfExtension(extension) {
604
649
  var _a;
605
650
  this.checkUpdate();
606
- (0, utils_1.assert)(!extension || ((_a = this._parent) === null || _a === void 0 ? void 0 : _a.extensions().has(extension)), () => `Cannot set object as part of the provided extension: it is not an extension of parent ${this.parent}`);
651
+ (0, utils_1.assert)(!extension || ((_a = this._parent) === null || _a === void 0 ? void 0 : _a.hasExtension(extension)), () => `Cannot set object as part of the provided extension: it is not an extension of parent ${this.parent}`);
607
652
  this._extension = extension;
608
653
  }
609
654
  remove() {
@@ -648,12 +693,15 @@ class SchemaBlueprint {
648
693
  return this.onUnknownDirectiveValidationError(schema, name, error);
649
694
  }
650
695
  else {
651
- return (0, error_2.withModifiedErrorMessage)(error, `${error.message}${(0, suggestions_1.didYouMean)(suggestions.map((s) => '@' + s))}`);
696
+ return (0, error_1.withModifiedErrorMessage)(error, `${error.message}${(0, suggestions_1.didYouMean)(suggestions.map((s) => '@' + s))}`);
652
697
  }
653
698
  }
654
699
  onUnknownDirectiveValidationError(_schema, _unknownDirectiveName, error) {
655
700
  return error;
656
701
  }
702
+ applyDirectivesAfterParsing() {
703
+ return false;
704
+ }
657
705
  }
658
706
  exports.SchemaBlueprint = SchemaBlueprint;
659
707
  exports.defaultSchemaBlueprint = new SchemaBlueprint();
@@ -697,7 +745,7 @@ class CoreFeatures {
697
745
  this.add(coreItself);
698
746
  const coreDef = (0, coreSpec_1.findCoreSpecVersion)(coreItself.url);
699
747
  if (!coreDef) {
700
- throw error_2.ERRORS.UNKNOWN_LINK_VERSION.err(`Schema uses unknown version ${coreItself.url.version} of the ${coreItself.url.name} spec`);
748
+ throw error_1.ERRORS.UNKNOWN_LINK_VERSION.err(`Schema uses unknown version ${coreItself.url.version} of the ${coreItself.url.name} spec`);
701
749
  }
702
750
  this.coreDefinition = coreDef;
703
751
  }
@@ -724,7 +772,7 @@ class CoreFeatures {
724
772
  const url = this.coreDefinition.extractFeatureUrl(args);
725
773
  const existing = this.byIdentity.get(url.identity);
726
774
  if (existing) {
727
- throw error_2.ERRORS.INVALID_LINK_DIRECTIVE_USAGE.err(`Duplicate inclusion of feature ${url.identity}`);
775
+ throw error_1.ERRORS.INVALID_LINK_DIRECTIVE_USAGE.err(`Duplicate inclusion of feature ${url.identity}`);
728
776
  }
729
777
  const imports = (0, coreSpec_1.extractCoreFeatureImports)(url, typedDirective);
730
778
  const feature = new CoreFeature(url, (_b = args.as) !== null && _b !== void 0 ? _b : url.name, directive, imports, args.for);
@@ -798,11 +846,35 @@ const graphQLBuiltInDirectivesSpecifications = [
798
846
  locations: [graphql_1.DirectiveLocation.SCALAR],
799
847
  argumentFct: (schema) => ({ args: [{ name: 'url', type: new NonNullType(schema.stringType()) }], errors: [] })
800
848
  }),
849
+ (0, directiveAndTypeSpecification_1.createDirectiveSpecification)({
850
+ name: 'defer',
851
+ locations: [graphql_1.DirectiveLocation.FRAGMENT_SPREAD, graphql_1.DirectiveLocation.INLINE_FRAGMENT],
852
+ argumentFct: (schema) => ({
853
+ args: [
854
+ { name: 'label', type: schema.stringType() },
855
+ { name: 'if', type: new NonNullType(schema.booleanType()), defaultValue: true },
856
+ ],
857
+ errors: [],
858
+ })
859
+ }),
860
+ (0, directiveAndTypeSpecification_1.createDirectiveSpecification)({
861
+ name: 'stream',
862
+ locations: [graphql_1.DirectiveLocation.FIELD],
863
+ argumentFct: (schema) => ({
864
+ args: [
865
+ { name: 'label', type: schema.stringType() },
866
+ { name: 'initialCount', type: schema.intType(), defaultValue: 0 },
867
+ { name: 'if', type: new NonNullType(schema.booleanType()), defaultValue: true },
868
+ ],
869
+ errors: [],
870
+ })
871
+ }),
801
872
  ];
802
873
  const coordinateRegexp = /^@?[_A-Za-z][_0-9A-Za-z]*(\.[_A-Za-z][_0-9A-Za-z]*)?(\([_A-Za-z][_0-9A-Za-z]*:\))?$/;
803
874
  class Schema {
804
- constructor(blueprint = exports.defaultSchemaBlueprint) {
875
+ constructor(blueprint = exports.defaultSchemaBlueprint, config = {}) {
805
876
  this.blueprint = blueprint;
877
+ this.config = config;
806
878
  this._builtInTypes = new utils_1.MapWithCachedArrays();
807
879
  this._types = new utils_1.MapWithCachedArrays();
808
880
  this._builtInDirectives = new utils_1.MapWithCachedArrays();
@@ -848,9 +920,6 @@ class Schema {
848
920
  this.apiSchema = undefined;
849
921
  }
850
922
  }
851
- forceSetCachedDocument(document) {
852
- this.cachedDocument = document;
853
- }
854
923
  isCoreSchema() {
855
924
  return this.coreFeatures !== undefined;
856
925
  }
@@ -858,8 +927,14 @@ class Schema {
858
927
  return this._coreFeatures;
859
928
  }
860
929
  toAST() {
930
+ var _a;
861
931
  if (!this.cachedDocument) {
862
- this.forceSetCachedDocument((0, graphql_1.parse)((0, print_1.printSchema)(this), { noLocation: true }));
932
+ const ast = (0, graphql_1.parse)((0, print_1.printSchema)(this), { noLocation: true });
933
+ const shouldCache = (_a = this.config.cacheAST) !== null && _a !== void 0 ? _a : false;
934
+ if (!shouldCache) {
935
+ return ast;
936
+ }
937
+ this.cachedDocument = ast;
863
938
  }
864
939
  return this.cachedDocument;
865
940
  }
@@ -892,9 +967,18 @@ class Schema {
892
967
  }
893
968
  return nodes;
894
969
  }
895
- toGraphQLJSSchema() {
970
+ toGraphQLJSSchema(config) {
971
+ var _a, _b;
972
+ const includeDefer = (_a = config === null || config === void 0 ? void 0 : config.includeDefer) !== null && _a !== void 0 ? _a : false;
973
+ const includeStream = (_b = config === null || config === void 0 ? void 0 : config.includeStream) !== null && _b !== void 0 ? _b : false;
896
974
  let ast = this.toAST();
897
975
  const additionalNodes = this.emptyASTDefinitionsForExtensionsWithoutDefinition();
976
+ if (includeDefer) {
977
+ additionalNodes.push(this.deferDirective().toAST());
978
+ }
979
+ if (includeStream) {
980
+ additionalNodes.push(this.streamDirective().toAST());
981
+ }
898
982
  if (additionalNodes.length > 0) {
899
983
  ast = {
900
984
  kind: graphql_1.Kind.DOCUMENT,
@@ -1061,8 +1145,10 @@ class Schema {
1061
1145
  return definition;
1062
1146
  }
1063
1147
  invalidate() {
1148
+ if (this.isValidated) {
1149
+ this.blueprint.onInvalidation(this);
1150
+ }
1064
1151
  this.isValidated = false;
1065
- this.blueprint.onInvalidation(this);
1066
1152
  }
1067
1153
  validate() {
1068
1154
  if (this.isValidated) {
@@ -1091,26 +1177,32 @@ class Schema {
1091
1177
  }
1092
1178
  return cloned;
1093
1179
  }
1094
- getBuiltInDirective(schema, name) {
1095
- const directive = schema.directive(name);
1180
+ getBuiltInDirective(name) {
1181
+ const directive = this.directive(name);
1096
1182
  (0, utils_1.assert)(directive, `The provided schema has not be built with the ${name} directive built-in`);
1097
1183
  return directive;
1098
1184
  }
1099
- includeDirective(schema) {
1100
- return this.getBuiltInDirective(schema, 'include');
1185
+ includeDirective() {
1186
+ return this.getBuiltInDirective('include');
1101
1187
  }
1102
- skipDirective(schema) {
1103
- return this.getBuiltInDirective(schema, 'skip');
1188
+ skipDirective() {
1189
+ return this.getBuiltInDirective('skip');
1104
1190
  }
1105
- deprecatedDirective(schema) {
1106
- return this.getBuiltInDirective(schema, 'deprecated');
1191
+ deprecatedDirective() {
1192
+ return this.getBuiltInDirective('deprecated');
1107
1193
  }
1108
- specifiedByDirective(schema) {
1109
- return this.getBuiltInDirective(schema, 'specifiedBy');
1194
+ specifiedByDirective() {
1195
+ return this.getBuiltInDirective('specifiedBy');
1196
+ }
1197
+ deferDirective() {
1198
+ return this.getBuiltInDirective('defer');
1199
+ }
1200
+ streamDirective() {
1201
+ return this.getBuiltInDirective('stream');
1110
1202
  }
1111
1203
  elementByCoordinate(coordinate) {
1112
1204
  if (!coordinate.match(coordinateRegexp)) {
1113
- throw error_2.ERRORS.INVALID_GRAPHQL.err(`Invalid argument "${coordinate}: it is not a syntactically valid graphQL coordinate."`);
1205
+ throw error_1.ERRORS.INVALID_GRAPHQL.err(`Invalid argument "${coordinate}: it is not a syntactically valid graphQL coordinate."`);
1114
1206
  }
1115
1207
  const argStartIdx = coordinate.indexOf('(');
1116
1208
  const start = argStartIdx < 0 ? coordinate : coordinate.slice(0, argStartIdx);
@@ -1121,7 +1213,7 @@ class Schema {
1121
1213
  const isDirective = typeOrDirectiveName.startsWith('@');
1122
1214
  if (isDirective) {
1123
1215
  if (fieldOrEnumName) {
1124
- throw error_2.ERRORS.INVALID_GRAPHQL.err(`Invalid argument "${coordinate}: it is not a syntactically valid graphQL coordinate."`);
1216
+ throw error_1.ERRORS.INVALID_GRAPHQL.err(`Invalid argument "${coordinate}: it is not a syntactically valid graphQL coordinate."`);
1125
1217
  }
1126
1218
  const directive = this.directive(typeOrDirectiveName.slice(1));
1127
1219
  return argName ? directive === null || directive === void 0 ? void 0 : directive.argument(argName) : directive;
@@ -1138,16 +1230,16 @@ class Schema {
1138
1230
  return argName ? field === null || field === void 0 ? void 0 : field.argument(argName) : field;
1139
1231
  case 'InputObjectType':
1140
1232
  if (argName) {
1141
- throw error_2.ERRORS.INVALID_GRAPHQL.err(`Invalid argument "${coordinate}: it is not a syntactically valid graphQL coordinate."`);
1233
+ throw error_1.ERRORS.INVALID_GRAPHQL.err(`Invalid argument "${coordinate}: it is not a syntactically valid graphQL coordinate."`);
1142
1234
  }
1143
1235
  return type.field(fieldOrEnumName);
1144
1236
  case 'EnumType':
1145
1237
  if (argName) {
1146
- throw error_2.ERRORS.INVALID_GRAPHQL.err(`Invalid argument "${coordinate}: it is not a syntactically valid graphQL coordinate."`);
1238
+ throw error_1.ERRORS.INVALID_GRAPHQL.err(`Invalid argument "${coordinate}: it is not a syntactically valid graphQL coordinate."`);
1147
1239
  }
1148
1240
  return type.value(fieldOrEnumName);
1149
1241
  default:
1150
- throw error_2.ERRORS.INVALID_GRAPHQL.err(`Invalid argument "${coordinate}: it is not a syntactically valid graphQL coordinate."`);
1242
+ throw error_1.ERRORS.INVALID_GRAPHQL.err(`Invalid argument "${coordinate}: it is not a syntactically valid graphQL coordinate."`);
1151
1243
  }
1152
1244
  }
1153
1245
  }
@@ -1172,7 +1264,6 @@ class SchemaDefinition extends SchemaElement {
1172
1264
  super(...arguments);
1173
1265
  this.kind = 'SchemaDefinition';
1174
1266
  this._roots = new utils_1.MapWithCachedArrays();
1175
- this._extensions = new Set();
1176
1267
  this.preserveEmptyDefinition = false;
1177
1268
  }
1178
1269
  roots() {
@@ -1185,7 +1276,7 @@ class SchemaDefinition extends SchemaElement {
1185
1276
  const coreFeatures = schema.coreFeatures;
1186
1277
  if ((0, coreSpec_1.isCoreSpecDirectiveApplication)(applied)) {
1187
1278
  if (coreFeatures) {
1188
- throw error_2.ERRORS.INVALID_LINK_DIRECTIVE_USAGE.err(`Invalid duplicate application of @core/@link`);
1279
+ throw error_1.ERRORS.INVALID_LINK_DIRECTIVE_USAGE.err(`Invalid duplicate application of @core/@link`);
1189
1280
  }
1190
1281
  const schemaDirective = applied;
1191
1282
  const args = schemaDirective.arguments();
@@ -1216,10 +1307,10 @@ class SchemaDefinition extends SchemaElement {
1216
1307
  this.checkUpdate();
1217
1308
  const obj = this.schema().type(nameOrType);
1218
1309
  if (!obj) {
1219
- throw error_2.ERRORS.INVALID_GRAPHQL.err(`Cannot set schema ${rootKind} root to unknown type ${nameOrType}`);
1310
+ throw error_1.ERRORS.INVALID_GRAPHQL.err(`Cannot set schema ${rootKind} root to unknown type ${nameOrType}`);
1220
1311
  }
1221
1312
  else if (obj.kind != 'ObjectType') {
1222
- throw error_2.ERRORS.INVALID_GRAPHQL.err(`${defaultRootName(rootKind)} root type must be an Object type${rootKind === 'query' ? '' : ' if provided'}, it cannot be set to ${nameOrType} (an ${obj.kind}).`);
1313
+ throw error_1.ERRORS.INVALID_GRAPHQL.err(`${defaultRootName(rootKind)} root type must be an Object type${rootKind === 'query' ? '' : ' if provided'}, it cannot be set to ${nameOrType} (an ${obj.kind}).`);
1223
1314
  }
1224
1315
  toSet = new RootType(rootKind, obj);
1225
1316
  }
@@ -1238,28 +1329,38 @@ class SchemaDefinition extends SchemaElement {
1238
1329
  return toSet;
1239
1330
  }
1240
1331
  extensions() {
1241
- return this._extensions;
1332
+ var _a;
1333
+ return (_a = this._extensions) !== null && _a !== void 0 ? _a : [];
1334
+ }
1335
+ hasExtension(extension) {
1336
+ var _a, _b;
1337
+ return (_b = (_a = this._extensions) === null || _a === void 0 ? void 0 : _a.includes(extension)) !== null && _b !== void 0 ? _b : false;
1242
1338
  }
1243
1339
  newExtension() {
1244
1340
  return this.addExtension(new Extension());
1245
1341
  }
1246
1342
  addExtension(extension) {
1247
1343
  this.checkUpdate();
1248
- if (this._extensions.has(extension)) {
1344
+ if (this.hasExtension(extension)) {
1249
1345
  return extension;
1250
1346
  }
1251
1347
  (0, utils_1.assert)(!extension.extendedElement, 'Cannot add extension to this schema: extension is already added to another schema');
1252
- this._extensions.add(extension);
1348
+ if (this._extensions) {
1349
+ this._extensions.push(extension);
1350
+ }
1351
+ else {
1352
+ this._extensions = [extension];
1353
+ }
1253
1354
  Extension.prototype['setExtendedElement'].call(extension, this);
1254
1355
  this.onModification();
1255
1356
  return extension;
1256
1357
  }
1257
1358
  hasExtensionElements() {
1258
- return this._extensions.size > 0;
1359
+ return !!this._extensions;
1259
1360
  }
1260
1361
  hasNonExtensionElements() {
1261
1362
  return this.preserveEmptyDefinition
1262
- || this._appliedDirectives.some((d) => d.ofExtension() === undefined)
1363
+ || this.appliedDirectives.some((d) => d.ofExtension() === undefined)
1263
1364
  || this.roots().some((r) => r.ofExtension() === undefined);
1264
1365
  }
1265
1366
  removeRootType(rootType) {
@@ -1315,7 +1416,6 @@ exports.InterfaceImplementation = InterfaceImplementation;
1315
1416
  class FieldBasedType extends BaseNamedType {
1316
1417
  constructor() {
1317
1418
  super(...arguments);
1318
- this._interfaceImplementations = new utils_1.MapWithCachedArrays();
1319
1419
  this._fields = new utils_1.MapWithCachedArrays();
1320
1420
  }
1321
1421
  onAttached() {
@@ -1328,18 +1428,21 @@ class FieldBasedType extends BaseNamedType {
1328
1428
  this._cachedNonBuiltInFields = undefined;
1329
1429
  }
1330
1430
  interfaceImplementations() {
1331
- return this._interfaceImplementations.values();
1431
+ var _a, _b;
1432
+ return (_b = (_a = this._interfaceImplementations) === null || _a === void 0 ? void 0 : _a.values()) !== null && _b !== void 0 ? _b : [];
1332
1433
  }
1333
1434
  interfaceImplementation(type) {
1334
- return this._interfaceImplementations.get(typeof type === 'string' ? type : type.name);
1435
+ return this._interfaceImplementations ? this._interfaceImplementations.get(typeof type === 'string' ? type : type.name) : undefined;
1335
1436
  }
1336
1437
  interfaces() {
1337
1438
  return this.interfaceImplementations().map(impl => impl.interface);
1338
1439
  }
1339
1440
  implementsInterface(type) {
1340
- return this._interfaceImplementations.has(typeof type === 'string' ? type : type.name);
1441
+ var _a, _b;
1442
+ return (_b = (_a = this._interfaceImplementations) === null || _a === void 0 ? void 0 : _a.has(typeof type === 'string' ? type : type.name)) !== null && _b !== void 0 ? _b : false;
1341
1443
  }
1342
1444
  addImplementedInterface(nameOrItfOrItfImpl) {
1445
+ var _a;
1343
1446
  let toAdd;
1344
1447
  if (nameOrItfOrItfImpl instanceof InterfaceImplementation) {
1345
1448
  this.checkUpdate(nameOrItfOrItfImpl);
@@ -1351,10 +1454,10 @@ class FieldBasedType extends BaseNamedType {
1351
1454
  this.checkUpdate();
1352
1455
  const maybeItf = this.schema().type(nameOrItfOrItfImpl);
1353
1456
  if (!maybeItf) {
1354
- throw error_2.ERRORS.INVALID_GRAPHQL.err(`Cannot implement unknown type ${nameOrItfOrItfImpl}`);
1457
+ throw error_1.ERRORS.INVALID_GRAPHQL.err(`Cannot implement unknown type ${nameOrItfOrItfImpl}`);
1355
1458
  }
1356
1459
  else if (maybeItf.kind != 'InterfaceType') {
1357
- throw error_2.ERRORS.INVALID_GRAPHQL.err(`Cannot implement non-interface type ${nameOrItfOrItfImpl} (of type ${maybeItf.kind})`);
1460
+ throw error_1.ERRORS.INVALID_GRAPHQL.err(`Cannot implement non-interface type ${nameOrItfOrItfImpl} (of type ${maybeItf.kind})`);
1358
1461
  }
1359
1462
  itf = maybeItf;
1360
1463
  }
@@ -1363,8 +1466,11 @@ class FieldBasedType extends BaseNamedType {
1363
1466
  }
1364
1467
  toAdd = new InterfaceImplementation(itf);
1365
1468
  }
1366
- const existing = this._interfaceImplementations.get(toAdd.interface.name);
1469
+ const existing = (_a = this._interfaceImplementations) === null || _a === void 0 ? void 0 : _a.get(toAdd.interface.name);
1367
1470
  if (!existing) {
1471
+ if (!this._interfaceImplementations) {
1472
+ this._interfaceImplementations = new utils_1.MapWithCachedArrays();
1473
+ }
1368
1474
  this._interfaceImplementations.set(toAdd.interface.name, toAdd);
1369
1475
  addReferenceToType(this, toAdd.interface);
1370
1476
  Element.prototype['setParent'].call(toAdd, this);
@@ -1407,10 +1513,10 @@ class FieldBasedType extends BaseNamedType {
1407
1513
  toAdd = nameOrField;
1408
1514
  }
1409
1515
  if (this.field(toAdd.name)) {
1410
- throw error_2.ERRORS.INVALID_GRAPHQL.err(`Field ${toAdd.name} already exists on ${this}`);
1516
+ throw error_1.ERRORS.INVALID_GRAPHQL.err(`Field ${toAdd.name} already exists on ${this}`);
1411
1517
  }
1412
1518
  if (type && !isOutputType(type)) {
1413
- throw error_2.ERRORS.INVALID_GRAPHQL.err(`Invalid input type ${type} for field ${toAdd.name}: object and interface field types should be output types.`);
1519
+ throw error_1.ERRORS.INVALID_GRAPHQL.err(`Invalid input type ${type} for field ${toAdd.name}: object and interface field types should be output types.`);
1414
1520
  }
1415
1521
  this._fields.set(toAdd.name, toAdd);
1416
1522
  this._cachedNonBuiltInFields = undefined;
@@ -1428,11 +1534,13 @@ class FieldBasedType extends BaseNamedType {
1428
1534
  }
1429
1535
  }
1430
1536
  removeInterfaceImplementation(itf) {
1431
- this._interfaceImplementations.delete(itf.name);
1537
+ var _a;
1538
+ (_a = this._interfaceImplementations) === null || _a === void 0 ? void 0 : _a.delete(itf.name);
1432
1539
  removeReferenceToType(this, itf);
1433
1540
  }
1434
1541
  removeTypeReference(type) {
1435
- this._interfaceImplementations.delete(type.name);
1542
+ var _a;
1543
+ (_a = this._interfaceImplementations) === null || _a === void 0 ? void 0 : _a.delete(type.name);
1436
1544
  }
1437
1545
  removeInnerElements() {
1438
1546
  for (const interfaceImpl of this.interfaceImplementations()) {
@@ -1492,7 +1600,7 @@ class InterfaceType extends FieldBasedType {
1492
1600
  this.astDefinitionKind = graphql_1.Kind.INTERFACE_TYPE_DEFINITION;
1493
1601
  }
1494
1602
  allImplementations() {
1495
- return (0, utils_1.setValues)(this._referencers).filter(ref => ref.kind === 'ObjectType' || ref.kind === 'InterfaceType');
1603
+ return this.referencers().filter(ref => ref.kind === 'ObjectType' || ref.kind === 'InterfaceType');
1496
1604
  }
1497
1605
  possibleRuntimeTypes() {
1498
1606
  return this.allImplementations().filter(impl => impl.kind === 'ObjectType');
@@ -1556,10 +1664,10 @@ class UnionType extends BaseNamedType {
1556
1664
  this.checkUpdate();
1557
1665
  const maybeObj = this.schema().type(nameOrTypeOrMember);
1558
1666
  if (!maybeObj) {
1559
- throw error_2.ERRORS.INVALID_GRAPHQL.err(`Cannot add unknown type ${nameOrTypeOrMember} as member of union type ${this.name}`);
1667
+ throw error_1.ERRORS.INVALID_GRAPHQL.err(`Cannot add unknown type ${nameOrTypeOrMember} as member of union type ${this.name}`);
1560
1668
  }
1561
1669
  else if (maybeObj.kind != 'ObjectType') {
1562
- throw error_2.ERRORS.INVALID_GRAPHQL.err(`Cannot add non-object type ${nameOrTypeOrMember} (of type ${maybeObj.kind}) as member of union type ${this.name}`);
1670
+ throw error_1.ERRORS.INVALID_GRAPHQL.err(`Cannot add non-object type ${nameOrTypeOrMember} (of type ${maybeObj.kind}) as member of union type ${this.name}`);
1563
1671
  }
1564
1672
  obj = maybeObj;
1565
1673
  }
@@ -1657,13 +1765,10 @@ class EnumType extends BaseNamedType {
1657
1765
  (0, utils_1.assert)(false, `Eum type ${this} can't reference other types; shouldn't be asked to remove reference to ${type}`);
1658
1766
  }
1659
1767
  removeValueInternal(value) {
1660
- const index = this._values.indexOf(value);
1661
- if (index >= 0) {
1662
- this._values.splice(index, 1);
1663
- }
1768
+ (0, utils_1.removeArrayElement)(value, this._values);
1664
1769
  }
1665
1770
  removeInnerElements() {
1666
- const values = Array.from(this._values);
1771
+ const values = this.values;
1667
1772
  for (const value of values) {
1668
1773
  value.remove();
1669
1774
  }
@@ -1699,10 +1804,10 @@ class InputObjectType extends BaseNamedType {
1699
1804
  const toAdd = typeof nameOrField === 'string' ? new InputFieldDefinition(nameOrField) : nameOrField;
1700
1805
  this.checkUpdate(toAdd);
1701
1806
  if (this.field(toAdd.name)) {
1702
- throw error_2.ERRORS.INVALID_GRAPHQL.err(`Field ${toAdd.name} already exists on ${this}`);
1807
+ throw error_1.ERRORS.INVALID_GRAPHQL.err(`Field ${toAdd.name} already exists on ${this}`);
1703
1808
  }
1704
1809
  if (type && !isInputType(type)) {
1705
- throw error_2.ERRORS.INVALID_GRAPHQL.err(`Invalid output type ${type} for field ${toAdd.name}: input field types should be input types.`);
1810
+ throw error_1.ERRORS.INVALID_GRAPHQL.err(`Invalid output type ${type} for field ${toAdd.name}: input field types should be input types.`);
1706
1811
  }
1707
1812
  this._fields.set(toAdd.name, toAdd);
1708
1813
  this._cachedFieldsArray = undefined;
@@ -1790,7 +1895,6 @@ class FieldDefinition extends NamedSchemaElementWithType {
1790
1895
  super(name);
1791
1896
  this.isBuiltIn = isBuiltIn;
1792
1897
  this.kind = 'FieldDefinition';
1793
- this._args = new utils_1.MapWithCachedArrays();
1794
1898
  }
1795
1899
  isElementBuiltIn() {
1796
1900
  return this.isBuiltIn;
@@ -1800,13 +1904,15 @@ class FieldDefinition extends NamedSchemaElementWithType {
1800
1904
  return `${parent == undefined ? '<detached>' : parent.coordinate}.${this.name}`;
1801
1905
  }
1802
1906
  hasArguments() {
1803
- return this._args.size > 0;
1907
+ return !!this._args && this._args.size > 0;
1804
1908
  }
1805
1909
  arguments() {
1806
- return this._args.values();
1910
+ var _a, _b;
1911
+ return (_b = (_a = this._args) === null || _a === void 0 ? void 0 : _a.values()) !== null && _b !== void 0 ? _b : [];
1807
1912
  }
1808
1913
  argument(name) {
1809
- return this._args.get(name);
1914
+ var _a;
1915
+ return (_a = this._args) === null || _a === void 0 ? void 0 : _a.get(name);
1810
1916
  }
1811
1917
  addArgument(nameOrArg, type, defaultValue) {
1812
1918
  let toAdd;
@@ -1822,15 +1928,18 @@ class FieldDefinition extends NamedSchemaElementWithType {
1822
1928
  const existing = this.argument(toAdd.name);
1823
1929
  if (existing) {
1824
1930
  if (type && existing.type && !(0, types_1.sameType)(type, existing.type)) {
1825
- throw error_2.ERRORS.INVALID_GRAPHQL.err(`Argument ${toAdd.name} already exists on field ${this.name} with a different type (${existing.type})`);
1931
+ throw error_1.ERRORS.INVALID_GRAPHQL.err(`Argument ${toAdd.name} already exists on field ${this.name} with a different type (${existing.type})`);
1826
1932
  }
1827
1933
  if (defaultValue && (!existing.defaultValue || !(0, values_1.valueEquals)(defaultValue, existing.defaultValue))) {
1828
- throw error_2.ERRORS.INVALID_GRAPHQL.err(`Argument ${toAdd.name} already exists on field ${this.name} with a different default value (${(0, values_1.valueToString)(existing.defaultValue)})`);
1934
+ throw error_1.ERRORS.INVALID_GRAPHQL.err(`Argument ${toAdd.name} already exists on field ${this.name} with a different default value (${(0, values_1.valueToString)(existing.defaultValue)})`);
1829
1935
  }
1830
1936
  return existing;
1831
1937
  }
1832
1938
  if (type && !isInputType(type)) {
1833
- throw error_2.ERRORS.INVALID_GRAPHQL.err(`Invalid output type ${type} for argument ${toAdd.name} of ${this}: arguments should be input types.`);
1939
+ throw error_1.ERRORS.INVALID_GRAPHQL.err(`Invalid output type ${type} for argument ${toAdd.name} of ${this}: arguments should be input types.`);
1940
+ }
1941
+ if (!this._args) {
1942
+ this._args = new utils_1.MapWithCachedArrays();
1834
1943
  }
1835
1944
  this._args.set(toAdd.name, toAdd);
1836
1945
  Element.prototype['setParent'].call(toAdd, this);
@@ -1849,7 +1958,7 @@ class FieldDefinition extends NamedSchemaElementWithType {
1849
1958
  setOfExtension(extension) {
1850
1959
  var _a;
1851
1960
  this.checkUpdate();
1852
- (0, utils_1.assert)(!extension || ((_a = this._parent) === null || _a === void 0 ? void 0 : _a.extensions().has(extension)), () => `Cannot mark field ${this.name} as part of the provided extension: it is not an extension of field parent type ${this.parent}`);
1961
+ (0, utils_1.assert)(!extension || ((_a = this._parent) === null || _a === void 0 ? void 0 : _a.hasExtension(extension)), () => `Cannot mark field ${this.name} as part of the provided extension: it is not an extension of field parent type ${this.parent}`);
1853
1962
  this._extension = extension;
1854
1963
  this.onModification();
1855
1964
  }
@@ -1860,7 +1969,9 @@ class FieldDefinition extends NamedSchemaElementWithType {
1860
1969
  return introspection_1.introspectionFieldNames.includes(this.name);
1861
1970
  }
1862
1971
  removeArgumentInternal(name) {
1863
- this._args.delete(name);
1972
+ if (this._args) {
1973
+ this._args.delete(name);
1974
+ }
1864
1975
  }
1865
1976
  removeParent() {
1866
1977
  this._parent = undefined;
@@ -1893,9 +2004,9 @@ class FieldDefinition extends NamedSchemaElementWithType {
1893
2004
  }
1894
2005
  }
1895
2006
  toString() {
1896
- const args = this._args.size == 0
1897
- ? ""
1898
- : '(' + this.arguments().map(arg => arg.toString()).join(', ') + ')';
2007
+ const args = this.hasArguments()
2008
+ ? '(' + this.arguments().map(arg => arg.toString()).join(', ') + ')'
2009
+ : "";
1899
2010
  return `${this.name}${args}: ${this.type}`;
1900
2011
  }
1901
2012
  }
@@ -1921,7 +2032,7 @@ class InputFieldDefinition extends NamedSchemaElementWithType {
1921
2032
  setOfExtension(extension) {
1922
2033
  var _a;
1923
2034
  this.checkUpdate();
1924
- (0, utils_1.assert)(!extension || ((_a = this._parent) === null || _a === void 0 ? void 0 : _a.extensions().has(extension)), () => `Cannot mark field ${this.name} as part of the provided extension: it is not an extension of field parent type ${this.parent}`);
2035
+ (0, utils_1.assert)(!extension || ((_a = this._parent) === null || _a === void 0 ? void 0 : _a.hasExtension(extension)), () => `Cannot mark field ${this.name} as part of the provided extension: it is not an extension of field parent type ${this.parent}`);
1925
2036
  this._extension = extension;
1926
2037
  this.onModification();
1927
2038
  }
@@ -2014,7 +2125,7 @@ class EnumValue extends NamedSchemaElement {
2014
2125
  setOfExtension(extension) {
2015
2126
  var _a;
2016
2127
  this.checkUpdate();
2017
- (0, utils_1.assert)(!extension || ((_a = this._parent) === null || _a === void 0 ? void 0 : _a.extensions().has(extension)), () => `Cannot mark field ${this.name} as part of the provided extension: it is not an extension of enum value parent type ${this.parent}`);
2128
+ (0, utils_1.assert)(!extension || ((_a = this._parent) === null || _a === void 0 ? void 0 : _a.hasExtension(extension)), () => `Cannot mark field ${this.name} as part of the provided extension: it is not an extension of enum value parent type ${this.parent}`);
2018
2129
  this._extension = extension;
2019
2130
  this.onModification();
2020
2131
  }
@@ -2047,19 +2158,19 @@ class DirectiveDefinition extends NamedSchemaElement {
2047
2158
  super(name);
2048
2159
  this.isBuiltIn = isBuiltIn;
2049
2160
  this.kind = 'DirectiveDefinition';
2050
- this._args = new utils_1.MapWithCachedArrays();
2051
2161
  this.repeatable = false;
2052
2162
  this._locations = [];
2053
- this._referencers = new Set();
2054
2163
  }
2055
2164
  get coordinate() {
2056
2165
  return `@${this.name}`;
2057
2166
  }
2058
2167
  arguments() {
2059
- return this._args.values();
2168
+ var _a, _b;
2169
+ return (_b = (_a = this._args) === null || _a === void 0 ? void 0 : _a.values()) !== null && _b !== void 0 ? _b : [];
2060
2170
  }
2061
2171
  argument(name) {
2062
- return this._args.get(name);
2172
+ var _a;
2173
+ return (_a = this._args) === null || _a === void 0 ? void 0 : _a.get(name);
2063
2174
  }
2064
2175
  addArgument(nameOrArg, type, defaultValue) {
2065
2176
  let toAdd;
@@ -2073,7 +2184,10 @@ class DirectiveDefinition extends NamedSchemaElement {
2073
2184
  toAdd = nameOrArg;
2074
2185
  }
2075
2186
  if (this.argument(toAdd.name)) {
2076
- throw error_2.ERRORS.INVALID_GRAPHQL.err(`Argument ${toAdd.name} already exists on field ${this.name}`);
2187
+ throw error_1.ERRORS.INVALID_GRAPHQL.err(`Argument ${toAdd.name} already exists on field ${this.name}`);
2188
+ }
2189
+ if (!this._args) {
2190
+ this._args = new utils_1.MapWithCachedArrays();
2077
2191
  }
2078
2192
  this._args.set(toAdd.name, toAdd);
2079
2193
  Element.prototype['setParent'].call(toAdd, this);
@@ -2084,7 +2198,8 @@ class DirectiveDefinition extends NamedSchemaElement {
2084
2198
  return toAdd;
2085
2199
  }
2086
2200
  removeArgumentInternal(name) {
2087
- this._args.delete(name);
2201
+ var _a;
2202
+ (_a = this._args) === null || _a === void 0 ? void 0 : _a.delete(name);
2088
2203
  }
2089
2204
  get locations() {
2090
2205
  return this._locations;
@@ -2111,43 +2226,56 @@ class DirectiveDefinition extends NamedSchemaElement {
2111
2226
  removeLocations(...locations) {
2112
2227
  let modified = false;
2113
2228
  for (const location of locations) {
2114
- const index = this._locations.indexOf(location);
2115
- if (index >= 0) {
2116
- this._locations.splice(index, 1);
2117
- modified = true;
2118
- }
2229
+ modified || (modified = (0, utils_1.removeArrayElement)(location, this._locations));
2119
2230
  }
2120
2231
  if (modified) {
2121
2232
  this.onModification();
2122
2233
  }
2123
2234
  return this;
2124
2235
  }
2236
+ hasExecutableLocations() {
2237
+ return this.locations.some((loc) => isExecutableDirectiveLocation(loc));
2238
+ }
2239
+ hasTypeSystemLocations() {
2240
+ return this.locations.some((loc) => isTypeSystemDirectiveLocation(loc));
2241
+ }
2125
2242
  applications() {
2126
- return (0, utils_1.setValues)(this._referencers);
2243
+ var _a;
2244
+ return (_a = this._referencers) !== null && _a !== void 0 ? _a : [];
2127
2245
  }
2128
2246
  addReferencer(referencer) {
2129
2247
  (0, utils_1.assert)(referencer, 'Referencer should exists');
2130
- this._referencers.add(referencer);
2248
+ if (this._referencers) {
2249
+ if (!this._referencers.includes(referencer)) {
2250
+ this._referencers.push(referencer);
2251
+ }
2252
+ }
2253
+ else {
2254
+ this._referencers = [referencer];
2255
+ }
2131
2256
  }
2132
2257
  removeReferencer(referencer) {
2133
- this._referencers.delete(referencer);
2258
+ if (this._referencers) {
2259
+ (0, utils_1.removeArrayElement)(referencer, this._referencers);
2260
+ }
2134
2261
  }
2135
2262
  removeTypeReference(type) {
2136
2263
  (0, utils_1.assert)(false, `Directive definition ${this} can't reference other types (it's arguments can); shouldn't be asked to remove reference to ${type}`);
2137
2264
  }
2138
2265
  remove() {
2266
+ var _a;
2139
2267
  if (!this._parent) {
2140
2268
  return [];
2141
2269
  }
2142
2270
  this.checkRemoval();
2143
2271
  this.onModification();
2144
2272
  this.sourceAST = undefined;
2145
- (0, utils_1.assert)(this._appliedDirectives.length === 0, "Directive definition should not have directive applied to it");
2273
+ (0, utils_1.assert)(!this._appliedDirectives || this._appliedDirectives.length === 0, "Directive definition should not have directive applied to it");
2146
2274
  for (const arg of this.arguments()) {
2147
2275
  arg.remove();
2148
2276
  }
2149
- const toReturn = (0, utils_1.setValues)(this._referencers);
2150
- this._referencers.clear();
2277
+ const toReturn = (_a = this._referencers) !== null && _a !== void 0 ? _a : [];
2278
+ this._referencers = undefined;
2151
2279
  Schema.prototype['removeDirectiveInternal'].call(this._parent, this);
2152
2280
  this._parent = undefined;
2153
2281
  return toReturn;
@@ -2155,6 +2283,10 @@ class DirectiveDefinition extends NamedSchemaElement {
2155
2283
  removeRecursive() {
2156
2284
  this.remove().forEach(ref => ref.remove());
2157
2285
  }
2286
+ toAST() {
2287
+ const doc = (0, graphql_1.parse)((0, print_1.printDirectiveDefinition)(this));
2288
+ return doc.definitions[0];
2289
+ }
2158
2290
  toString() {
2159
2291
  return `@${this.name}`;
2160
2292
  }
@@ -2231,7 +2363,7 @@ class Directive extends Element {
2231
2363
  if (extension) {
2232
2364
  const parent = this.parent;
2233
2365
  (0, utils_1.assert)(parent instanceof SchemaDefinition || parent instanceof BaseNamedType, 'Can only mark directive parts of extensions when directly apply to type or schema definition.');
2234
- (0, utils_1.assert)(parent.extensions().has(extension), () => `Cannot mark directive ${this.name} as part of the provided extension: it is not an extension of parent ${parent}`);
2366
+ (0, utils_1.assert)(parent.hasExtension(extension), () => `Cannot mark directive ${this.name} as part of the provided extension: it is not an extension of parent ${parent}`);
2235
2367
  }
2236
2368
  this._extension = extension;
2237
2369
  this.onModification();
@@ -2281,9 +2413,8 @@ class Directive extends Element {
2281
2413
  DirectiveDefinition.prototype['removeReferencer'].call(definition, this);
2282
2414
  }
2283
2415
  const parentDirectives = this._parent.appliedDirectives;
2284
- const index = parentDirectives.indexOf(this);
2285
- (0, utils_1.assert)(index >= 0, () => `Directive ${this} lists ${this._parent} as parent, but that parent doesn't list it as applied directive`);
2286
- parentDirectives.splice(index, 1);
2416
+ const removed = (0, utils_1.removeArrayElement)(this, parentDirectives);
2417
+ (0, utils_1.assert)(removed, () => `Directive ${this} lists ${this._parent} as parent, but that parent doesn't list it as applied directive`);
2287
2418
  this._parent = undefined;
2288
2419
  this._extension = undefined;
2289
2420
  return true;
@@ -2456,7 +2587,7 @@ function variableDefinitionsFromAST(schema, definitionNodes) {
2456
2587
  for (const definitionNode of definitionNodes) {
2457
2588
  if (!definitions.add(variableDefinitionFromAST(schema, definitionNode))) {
2458
2589
  const name = definitionNode.variable.name.value;
2459
- throw error_2.ERRORS.INVALID_GRAPHQL.err(`Duplicate definition for variable ${name}`, { nodes: definitionNodes.filter(n => n.variable.name.value === name) });
2590
+ throw error_1.ERRORS.INVALID_GRAPHQL.err(`Duplicate definition for variable ${name}`, { nodes: definitionNodes.filter(n => n.variable.name.value === name) });
2460
2591
  }
2461
2592
  }
2462
2593
  return definitions;
@@ -2466,7 +2597,7 @@ function variableDefinitionFromAST(schema, definitionNode) {
2466
2597
  const variable = new Variable(definitionNode.variable.name.value);
2467
2598
  const type = typeFromAST(schema, definitionNode.type);
2468
2599
  if (!isInputType(type)) {
2469
- throw error_2.ERRORS.INVALID_GRAPHQL.err(`Invalid type "${type}" for variable $${variable}: not an input type`, { nodes: definitionNode.type });
2600
+ throw error_1.ERRORS.INVALID_GRAPHQL.err(`Invalid type "${type}" for variable $${variable}: not an input type`, { nodes: definitionNode.type });
2470
2601
  }
2471
2602
  const def = new VariableDefinition(schema, variable, type, definitionNode.defaultValue ? (0, values_1.valueFromAST)(definitionNode.defaultValue, type) : undefined);
2472
2603
  return def;
@@ -2535,6 +2666,10 @@ function* directivesToCopy(source, dest) {
2535
2666
  }
2536
2667
  yield* source.directives();
2537
2668
  }
2669
+ function copyDirectiveDefinitionToSchema({ definition, schema, copyDirectiveApplicationsInArguments = true, locationFilter, }) {
2670
+ copyDirectiveDefinitionInner(definition, schema.addDirectiveDefinition(definition.name), copyDirectiveApplicationsInArguments, locationFilter);
2671
+ }
2672
+ exports.copyDirectiveDefinitionToSchema = copyDirectiveDefinitionToSchema;
2538
2673
  function copy(source, dest) {
2539
2674
  for (const type of typesToCopy(source, dest)) {
2540
2675
  dest.addType(newNamedType(type.kind, type.name));
@@ -2664,21 +2799,30 @@ function copyWrapperTypeOrTypeRef(source, destParent) {
2664
2799
  return destParent.type(source.name);
2665
2800
  }
2666
2801
  }
2667
- function copyArgumentDefinitionInner(source, dest) {
2802
+ function copyArgumentDefinitionInner(source, dest, copyDirectiveApplications = true) {
2668
2803
  const type = copyWrapperTypeOrTypeRef(source.type, dest.schema());
2669
2804
  dest.type = type;
2670
2805
  dest.defaultValue = source.defaultValue;
2671
- copyAppliedDirectives(source, dest);
2806
+ if (copyDirectiveApplications) {
2807
+ copyAppliedDirectives(source, dest);
2808
+ }
2672
2809
  dest.description = source.description;
2673
2810
  dest.sourceAST = source.sourceAST;
2674
2811
  }
2675
- function copyDirectiveDefinitionInner(source, dest) {
2812
+ function copyDirectiveDefinitionInner(source, dest, copyDirectiveApplicationsInArguments = true, locationFilter) {
2813
+ let locations = source.locations;
2814
+ if (locationFilter) {
2815
+ locations = locations.filter((loc) => locationFilter(loc));
2816
+ }
2817
+ if (locations.length === 0) {
2818
+ return;
2819
+ }
2676
2820
  for (const arg of source.arguments()) {
2677
2821
  const type = copyWrapperTypeOrTypeRef(arg.type, dest.schema());
2678
- copyArgumentDefinitionInner(arg, dest.addArgument(arg.name, type));
2822
+ copyArgumentDefinitionInner(arg, dest.addArgument(arg.name, type), copyDirectiveApplicationsInArguments);
2679
2823
  }
2680
2824
  dest.repeatable = source.repeatable;
2681
- dest.addLocations(...source.locations);
2825
+ dest.addLocations(...locations);
2682
2826
  dest.sourceAST = source.sourceAST;
2683
2827
  dest.description = source.description;
2684
2828
  }