@apollo/federation-internals 2.1.0-alpha.4 → 2.1.2-alpha.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 (60) hide show
  1. package/CHANGELOG.md +1 -10
  2. package/dist/buildSchema.d.ts.map +1 -1
  3. package/dist/buildSchema.js +1 -1
  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 +26 -29
  10. package/dist/definitions.d.ts.map +1 -1
  11. package/dist/definitions.js +202 -158
  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 +126 -5
  19. package/dist/extractSubgraphsFromSupergraph.js.map +1 -1
  20. package/dist/federation.d.ts +2 -2
  21. package/dist/federation.d.ts.map +1 -1
  22. package/dist/federation.js +6 -6
  23. package/dist/federation.js.map +1 -1
  24. package/dist/operations.d.ts +10 -4
  25. package/dist/operations.d.ts.map +1 -1
  26. package/dist/operations.js +42 -39
  27. package/dist/operations.js.map +1 -1
  28. package/dist/print.js.map +1 -1
  29. package/dist/schemaUpgrader.d.ts.map +1 -1
  30. package/dist/schemaUpgrader.js +4 -4
  31. package/dist/schemaUpgrader.js.map +1 -1
  32. package/dist/supergraphs.d.ts +1 -3
  33. package/dist/supergraphs.d.ts.map +1 -1
  34. package/dist/supergraphs.js +9 -22
  35. package/dist/supergraphs.js.map +1 -1
  36. package/dist/utils.d.ts +2 -1
  37. package/dist/utils.d.ts.map +1 -1
  38. package/dist/utils.js +12 -1
  39. package/dist/utils.js.map +1 -1
  40. package/package.json +2 -3
  41. package/src/__tests__/coreSpec.test.ts +1 -1
  42. package/src/__tests__/definitions.test.ts +13 -4
  43. package/src/__tests__/extractSubgraphsFromSupergraph.test.ts +9 -5
  44. package/src/__tests__/operations.test.ts +123 -2
  45. package/src/__tests__/removeInaccessibleElements.test.ts +1 -3
  46. package/src/__tests__/schemaUpgrader.test.ts +0 -1
  47. package/src/__tests__/subgraphValidation.test.ts +1 -2
  48. package/src/buildSchema.ts +1 -2
  49. package/src/coreSpec.ts +2 -7
  50. package/src/definitions.ts +173 -148
  51. package/src/error.ts +62 -0
  52. package/src/extractSubgraphsFromSupergraph.ts +178 -7
  53. package/src/federation.ts +4 -3
  54. package/src/operations.ts +79 -50
  55. package/src/print.ts +2 -2
  56. package/src/schemaUpgrader.ts +5 -4
  57. package/src/supergraphs.ts +16 -25
  58. package/src/utils.ts +15 -0
  59. package/tsconfig.test.tsbuildinfo +1 -1
  60. package/tsconfig.tsbuildinfo +1 -1
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- 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.printErrors = exports.printGraphQLErrorsOrRethrow = exports.errorCauses = 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 = exports.CoreFeatures = exports.CoreFeature = exports.defaultSchemaBlueprint = 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) {
@@ -279,7 +246,7 @@ function typeFromAST(schema, node) {
279
246
  default:
280
247
  const type = schema.type(node.name.value);
281
248
  if (!type) {
282
- 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 });
283
250
  }
284
251
  return type;
285
252
  }
@@ -292,7 +259,6 @@ exports.isLeafType = isLeafType;
292
259
  class DirectiveTargetElement {
293
260
  constructor(_schema) {
294
261
  this._schema = _schema;
295
- this.appliedDirectives = [];
296
262
  }
297
263
  schema() {
298
264
  return this._schema;
@@ -301,6 +267,10 @@ class DirectiveTargetElement {
301
267
  const directiveName = typeof nameOrDefinition === 'string' ? nameOrDefinition : nameOrDefinition.name;
302
268
  return this.appliedDirectives.filter(d => d.name == directiveName);
303
269
  }
270
+ get appliedDirectives() {
271
+ var _a;
272
+ return (_a = this._appliedDirectives) !== null && _a !== void 0 ? _a : [];
273
+ }
304
274
  hasAppliedDirective(nameOrDefinition) {
305
275
  const directiveName = typeof nameOrDefinition === 'string' ? nameOrDefinition : nameOrDefinition.name;
306
276
  return this.appliedDirectives.some(d => d.name == directiveName);
@@ -320,7 +290,12 @@ class DirectiveTargetElement {
320
290
  toAdd = new Directive(defOrDirective.name, args !== null && args !== void 0 ? args : Object.create(null));
321
291
  }
322
292
  Element.prototype['setParent'].call(toAdd, this);
323
- this.appliedDirectives.push(toAdd);
293
+ if (this._appliedDirectives) {
294
+ this._appliedDirectives.push(toAdd);
295
+ }
296
+ else {
297
+ this._appliedDirectives = [toAdd];
298
+ }
324
299
  return toAdd;
325
300
  }
326
301
  appliedDirectivesToDirectiveNodes() {
@@ -402,33 +377,36 @@ class Extension {
402
377
  }
403
378
  exports.Extension = Extension;
404
379
  class SchemaElement extends Element {
405
- constructor() {
406
- super(...arguments);
407
- this._appliedDirectives = [];
408
- this._unappliedDirectives = [];
409
- }
410
380
  addUnappliedDirective({ nameOrDef, args, extension, directive }) {
411
- this._unappliedDirectives.push({
381
+ const toAdd = {
412
382
  nameOrDef,
413
383
  args: args !== null && args !== void 0 ? args : {},
414
384
  extension,
415
385
  directive,
416
- });
386
+ };
387
+ if (this._unappliedDirectives) {
388
+ this._unappliedDirectives.push(toAdd);
389
+ }
390
+ else {
391
+ this._unappliedDirectives = [toAdd];
392
+ }
417
393
  }
418
394
  processUnappliedDirectives() {
419
- for (const { nameOrDef, args, extension, directive } of this._unappliedDirectives) {
395
+ var _a;
396
+ for (const { nameOrDef, args, extension, directive } of (_a = this._unappliedDirectives) !== null && _a !== void 0 ? _a : []) {
420
397
  const d = this.applyDirective(nameOrDef, args);
421
398
  d.setOfExtension(extension);
422
399
  d.sourceAST = directive;
423
400
  }
424
- this._unappliedDirectives = [];
401
+ this._unappliedDirectives = undefined;
425
402
  }
426
403
  get appliedDirectives() {
427
- return this._appliedDirectives;
404
+ var _a;
405
+ return (_a = this._appliedDirectives) !== null && _a !== void 0 ? _a : [];
428
406
  }
429
407
  appliedDirectivesOf(nameOrDefinition) {
430
408
  const directiveName = typeof nameOrDefinition === 'string' ? nameOrDefinition : nameOrDefinition.name;
431
- return this._appliedDirectives.filter(d => d.name == directiveName);
409
+ return this.appliedDirectives.filter(d => d.name == directiveName);
432
410
  }
433
411
  hasAppliedDirective(nameOrDefinition) {
434
412
  return (typeof nameOrDefinition === 'string'
@@ -442,7 +420,7 @@ class SchemaElement extends Element {
442
420
  this.checkUpdate();
443
421
  const def = (_a = this.schema().directive(nameOrDef)) !== null && _a !== void 0 ? _a : this.schema().blueprint.onMissingDirectiveDefinition(this.schema(), nameOrDef, args);
444
422
  if (!def) {
445
- 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}".`));
446
424
  }
447
425
  if (Array.isArray(def)) {
448
426
  throw (0, exports.ErrGraphQLValidationFailed)(def);
@@ -455,17 +433,25 @@ class SchemaElement extends Element {
455
433
  }
456
434
  const toAdd = new Directive(name, args !== null && args !== void 0 ? args : Object.create(null));
457
435
  Element.prototype['setParent'].call(toAdd, this);
458
- if (asFirstDirective) {
459
- 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
+ }
460
443
  }
461
444
  else {
462
- this._appliedDirectives.push(toAdd);
445
+ this._appliedDirectives = [toAdd];
463
446
  }
464
447
  DirectiveDefinition.prototype['addReferencer'].call(toAdd.definition, toAdd);
465
448
  this.onModification();
466
449
  return toAdd;
467
450
  }
468
451
  removeAppliedDirectives() {
452
+ if (!this._appliedDirectives) {
453
+ return;
454
+ }
469
455
  const applied = this._appliedDirectives.concat();
470
456
  applied.forEach(d => d.remove());
471
457
  }
@@ -514,15 +500,22 @@ class BaseNamedType extends NamedSchemaElement {
514
500
  constructor(name, isBuiltIn = false) {
515
501
  super(name);
516
502
  this.isBuiltIn = isBuiltIn;
517
- this._referencers = new Set();
518
- this._extensions = new Set();
519
503
  this.preserveEmptyDefinition = false;
520
504
  }
521
505
  addReferencer(referencer) {
522
- 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
+ }
523
514
  }
524
515
  removeReferencer(referencer) {
525
- this._referencers.delete(referencer);
516
+ if (this._referencers) {
517
+ (0, utils_1.removeArrayElement)(referencer, this._referencers);
518
+ }
526
519
  }
527
520
  get coordinate() {
528
521
  return this.name;
@@ -530,28 +523,38 @@ class BaseNamedType extends NamedSchemaElement {
530
523
  *allChildElements() {
531
524
  }
532
525
  extensions() {
533
- 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;
534
532
  }
535
533
  newExtension() {
536
534
  return this.addExtension(new Extension());
537
535
  }
538
536
  addExtension(extension) {
539
537
  this.checkUpdate();
540
- if (this._extensions.has(extension)) {
538
+ if (this.hasExtension(extension)) {
541
539
  return extension;
542
540
  }
543
541
  (0, utils_1.assert)(!extension.extendedElement, () => `Cannot add extension to type ${this}: it is already added to another type`);
544
- this._extensions.add(extension);
542
+ if (this._extensions) {
543
+ this._extensions.push(extension);
544
+ }
545
+ else {
546
+ this._extensions = [extension];
547
+ }
545
548
  Extension.prototype['setExtendedElement'].call(extension, this);
546
549
  this.onModification();
547
550
  return extension;
548
551
  }
549
552
  removeExtensions() {
550
- if (this._extensions.size === 0) {
553
+ if (!this._extensions) {
551
554
  return;
552
555
  }
553
- this._extensions.clear();
554
- for (const directive of this._appliedDirectives) {
556
+ this._extensions = undefined;
557
+ for (const directive of this.appliedDirectives) {
555
558
  directive.removeOfExtension();
556
559
  }
557
560
  this.removeInnerElementsExtensions();
@@ -560,11 +563,11 @@ class BaseNamedType extends NamedSchemaElement {
560
563
  return (0, introspection_1.isIntrospectionName)(this.name);
561
564
  }
562
565
  hasExtensionElements() {
563
- return this._extensions.size > 0;
566
+ return !!this._extensions;
564
567
  }
565
568
  hasNonExtensionElements() {
566
569
  return this.preserveEmptyDefinition
567
- || this._appliedDirectives.some(d => d.ofExtension() === undefined)
570
+ || this.appliedDirectives.some(d => d.ofExtension() === undefined)
568
571
  || this.hasNonExtensionInnerElements();
569
572
  }
570
573
  isElementBuiltIn() {
@@ -578,6 +581,7 @@ class BaseNamedType extends NamedSchemaElement {
578
581
  this.onModification();
579
582
  }
580
583
  remove() {
584
+ var _a, _b;
581
585
  if (!this._parent) {
582
586
  return [];
583
587
  }
@@ -586,11 +590,11 @@ class BaseNamedType extends NamedSchemaElement {
586
590
  this.sourceAST = undefined;
587
591
  this.removeAppliedDirectives();
588
592
  this.removeInnerElements();
589
- 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 => {
590
594
  SchemaElement.prototype['removeTypeReferenceInternal'].call(r, this);
591
595
  return r;
592
- });
593
- this._referencers.clear();
596
+ })) !== null && _b !== void 0 ? _b : [];
597
+ this._referencers = undefined;
594
598
  Schema.prototype['removeTypeInternal'].call(this._parent, this);
595
599
  this._parent = undefined;
596
600
  return toReturn;
@@ -599,10 +603,11 @@ class BaseNamedType extends NamedSchemaElement {
599
603
  this.remove().forEach(ref => this.removeReferenceRecursive(ref));
600
604
  }
601
605
  referencers() {
602
- return (0, utils_1.setValues)(this._referencers);
606
+ var _a;
607
+ return (_a = this._referencers) !== null && _a !== void 0 ? _a : [];
603
608
  }
604
609
  isReferenced() {
605
- return this._referencers.size > 0;
610
+ return !!this._referencers;
606
611
  }
607
612
  toString() {
608
613
  return this.name;
@@ -643,7 +648,7 @@ class BaseExtensionMember extends Element {
643
648
  setOfExtension(extension) {
644
649
  var _a;
645
650
  this.checkUpdate();
646
- (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}`);
647
652
  this._extension = extension;
648
653
  }
649
654
  remove() {
@@ -688,7 +693,7 @@ class SchemaBlueprint {
688
693
  return this.onUnknownDirectiveValidationError(schema, name, error);
689
694
  }
690
695
  else {
691
- 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))}`);
692
697
  }
693
698
  }
694
699
  onUnknownDirectiveValidationError(_schema, _unknownDirectiveName, error) {
@@ -740,7 +745,7 @@ class CoreFeatures {
740
745
  this.add(coreItself);
741
746
  const coreDef = (0, coreSpec_1.findCoreSpecVersion)(coreItself.url);
742
747
  if (!coreDef) {
743
- 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`);
744
749
  }
745
750
  this.coreDefinition = coreDef;
746
751
  }
@@ -767,7 +772,7 @@ class CoreFeatures {
767
772
  const url = this.coreDefinition.extractFeatureUrl(args);
768
773
  const existing = this.byIdentity.get(url.identity);
769
774
  if (existing) {
770
- 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}`);
771
776
  }
772
777
  const imports = (0, coreSpec_1.extractCoreFeatureImports)(url, typedDirective);
773
778
  const feature = new CoreFeature(url, (_b = args.as) !== null && _b !== void 0 ? _b : url.name, directive, imports, args.for);
@@ -847,7 +852,7 @@ const graphQLBuiltInDirectivesSpecifications = [
847
852
  argumentFct: (schema) => ({
848
853
  args: [
849
854
  { name: 'label', type: schema.stringType() },
850
- { name: 'if', type: schema.booleanType() },
855
+ { name: 'if', type: new NonNullType(schema.booleanType()), defaultValue: true },
851
856
  ],
852
857
  errors: [],
853
858
  })
@@ -859,7 +864,7 @@ const graphQLBuiltInDirectivesSpecifications = [
859
864
  args: [
860
865
  { name: 'label', type: schema.stringType() },
861
866
  { name: 'initialCount', type: schema.intType(), defaultValue: 0 },
862
- { name: 'if', type: schema.booleanType() },
867
+ { name: 'if', type: new NonNullType(schema.booleanType()), defaultValue: true },
863
868
  ],
864
869
  errors: [],
865
870
  })
@@ -867,8 +872,9 @@ const graphQLBuiltInDirectivesSpecifications = [
867
872
  ];
868
873
  const coordinateRegexp = /^@?[_A-Za-z][_0-9A-Za-z]*(\.[_A-Za-z][_0-9A-Za-z]*)?(\([_A-Za-z][_0-9A-Za-z]*:\))?$/;
869
874
  class Schema {
870
- constructor(blueprint = exports.defaultSchemaBlueprint) {
875
+ constructor(blueprint = exports.defaultSchemaBlueprint, config = {}) {
871
876
  this.blueprint = blueprint;
877
+ this.config = config;
872
878
  this._builtInTypes = new utils_1.MapWithCachedArrays();
873
879
  this._types = new utils_1.MapWithCachedArrays();
874
880
  this._builtInDirectives = new utils_1.MapWithCachedArrays();
@@ -914,9 +920,6 @@ class Schema {
914
920
  this.apiSchema = undefined;
915
921
  }
916
922
  }
917
- forceSetCachedDocument(document) {
918
- this.cachedDocument = document;
919
- }
920
923
  isCoreSchema() {
921
924
  return this.coreFeatures !== undefined;
922
925
  }
@@ -924,8 +927,14 @@ class Schema {
924
927
  return this._coreFeatures;
925
928
  }
926
929
  toAST() {
930
+ var _a;
927
931
  if (!this.cachedDocument) {
928
- 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;
929
938
  }
930
939
  return this.cachedDocument;
931
940
  }
@@ -959,13 +968,17 @@ class Schema {
959
968
  return nodes;
960
969
  }
961
970
  toGraphQLJSSchema(config) {
962
- var _a;
971
+ var _a, _b;
963
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;
964
974
  let ast = this.toAST();
965
975
  const additionalNodes = this.emptyASTDefinitionsForExtensionsWithoutDefinition();
966
976
  if (includeDefer) {
967
977
  additionalNodes.push(this.deferDirective().toAST());
968
978
  }
979
+ if (includeStream) {
980
+ additionalNodes.push(this.streamDirective().toAST());
981
+ }
969
982
  if (additionalNodes.length > 0) {
970
983
  ast = {
971
984
  kind: graphql_1.Kind.DOCUMENT,
@@ -1132,8 +1145,10 @@ class Schema {
1132
1145
  return definition;
1133
1146
  }
1134
1147
  invalidate() {
1148
+ if (this.isValidated) {
1149
+ this.blueprint.onInvalidation(this);
1150
+ }
1135
1151
  this.isValidated = false;
1136
- this.blueprint.onInvalidation(this);
1137
1152
  }
1138
1153
  validate() {
1139
1154
  if (this.isValidated) {
@@ -1187,7 +1202,7 @@ class Schema {
1187
1202
  }
1188
1203
  elementByCoordinate(coordinate) {
1189
1204
  if (!coordinate.match(coordinateRegexp)) {
1190
- 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."`);
1191
1206
  }
1192
1207
  const argStartIdx = coordinate.indexOf('(');
1193
1208
  const start = argStartIdx < 0 ? coordinate : coordinate.slice(0, argStartIdx);
@@ -1198,7 +1213,7 @@ class Schema {
1198
1213
  const isDirective = typeOrDirectiveName.startsWith('@');
1199
1214
  if (isDirective) {
1200
1215
  if (fieldOrEnumName) {
1201
- 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."`);
1202
1217
  }
1203
1218
  const directive = this.directive(typeOrDirectiveName.slice(1));
1204
1219
  return argName ? directive === null || directive === void 0 ? void 0 : directive.argument(argName) : directive;
@@ -1215,16 +1230,16 @@ class Schema {
1215
1230
  return argName ? field === null || field === void 0 ? void 0 : field.argument(argName) : field;
1216
1231
  case 'InputObjectType':
1217
1232
  if (argName) {
1218
- 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."`);
1219
1234
  }
1220
1235
  return type.field(fieldOrEnumName);
1221
1236
  case 'EnumType':
1222
1237
  if (argName) {
1223
- 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."`);
1224
1239
  }
1225
1240
  return type.value(fieldOrEnumName);
1226
1241
  default:
1227
- 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."`);
1228
1243
  }
1229
1244
  }
1230
1245
  }
@@ -1249,7 +1264,6 @@ class SchemaDefinition extends SchemaElement {
1249
1264
  super(...arguments);
1250
1265
  this.kind = 'SchemaDefinition';
1251
1266
  this._roots = new utils_1.MapWithCachedArrays();
1252
- this._extensions = new Set();
1253
1267
  this.preserveEmptyDefinition = false;
1254
1268
  }
1255
1269
  roots() {
@@ -1262,7 +1276,7 @@ class SchemaDefinition extends SchemaElement {
1262
1276
  const coreFeatures = schema.coreFeatures;
1263
1277
  if ((0, coreSpec_1.isCoreSpecDirectiveApplication)(applied)) {
1264
1278
  if (coreFeatures) {
1265
- 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`);
1266
1280
  }
1267
1281
  const schemaDirective = applied;
1268
1282
  const args = schemaDirective.arguments();
@@ -1293,10 +1307,10 @@ class SchemaDefinition extends SchemaElement {
1293
1307
  this.checkUpdate();
1294
1308
  const obj = this.schema().type(nameOrType);
1295
1309
  if (!obj) {
1296
- 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}`);
1297
1311
  }
1298
1312
  else if (obj.kind != 'ObjectType') {
1299
- 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}).`);
1300
1314
  }
1301
1315
  toSet = new RootType(rootKind, obj);
1302
1316
  }
@@ -1315,28 +1329,38 @@ class SchemaDefinition extends SchemaElement {
1315
1329
  return toSet;
1316
1330
  }
1317
1331
  extensions() {
1318
- 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;
1319
1338
  }
1320
1339
  newExtension() {
1321
1340
  return this.addExtension(new Extension());
1322
1341
  }
1323
1342
  addExtension(extension) {
1324
1343
  this.checkUpdate();
1325
- if (this._extensions.has(extension)) {
1344
+ if (this.hasExtension(extension)) {
1326
1345
  return extension;
1327
1346
  }
1328
1347
  (0, utils_1.assert)(!extension.extendedElement, 'Cannot add extension to this schema: extension is already added to another schema');
1329
- this._extensions.add(extension);
1348
+ if (this._extensions) {
1349
+ this._extensions.push(extension);
1350
+ }
1351
+ else {
1352
+ this._extensions = [extension];
1353
+ }
1330
1354
  Extension.prototype['setExtendedElement'].call(extension, this);
1331
1355
  this.onModification();
1332
1356
  return extension;
1333
1357
  }
1334
1358
  hasExtensionElements() {
1335
- return this._extensions.size > 0;
1359
+ return !!this._extensions;
1336
1360
  }
1337
1361
  hasNonExtensionElements() {
1338
1362
  return this.preserveEmptyDefinition
1339
- || this._appliedDirectives.some((d) => d.ofExtension() === undefined)
1363
+ || this.appliedDirectives.some((d) => d.ofExtension() === undefined)
1340
1364
  || this.roots().some((r) => r.ofExtension() === undefined);
1341
1365
  }
1342
1366
  removeRootType(rootType) {
@@ -1392,7 +1416,6 @@ exports.InterfaceImplementation = InterfaceImplementation;
1392
1416
  class FieldBasedType extends BaseNamedType {
1393
1417
  constructor() {
1394
1418
  super(...arguments);
1395
- this._interfaceImplementations = new utils_1.MapWithCachedArrays();
1396
1419
  this._fields = new utils_1.MapWithCachedArrays();
1397
1420
  }
1398
1421
  onAttached() {
@@ -1405,18 +1428,21 @@ class FieldBasedType extends BaseNamedType {
1405
1428
  this._cachedNonBuiltInFields = undefined;
1406
1429
  }
1407
1430
  interfaceImplementations() {
1408
- 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 : [];
1409
1433
  }
1410
1434
  interfaceImplementation(type) {
1411
- return this._interfaceImplementations.get(typeof type === 'string' ? type : type.name);
1435
+ return this._interfaceImplementations ? this._interfaceImplementations.get(typeof type === 'string' ? type : type.name) : undefined;
1412
1436
  }
1413
1437
  interfaces() {
1414
1438
  return this.interfaceImplementations().map(impl => impl.interface);
1415
1439
  }
1416
1440
  implementsInterface(type) {
1417
- 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;
1418
1443
  }
1419
1444
  addImplementedInterface(nameOrItfOrItfImpl) {
1445
+ var _a;
1420
1446
  let toAdd;
1421
1447
  if (nameOrItfOrItfImpl instanceof InterfaceImplementation) {
1422
1448
  this.checkUpdate(nameOrItfOrItfImpl);
@@ -1428,10 +1454,10 @@ class FieldBasedType extends BaseNamedType {
1428
1454
  this.checkUpdate();
1429
1455
  const maybeItf = this.schema().type(nameOrItfOrItfImpl);
1430
1456
  if (!maybeItf) {
1431
- 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}`);
1432
1458
  }
1433
1459
  else if (maybeItf.kind != 'InterfaceType') {
1434
- 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})`);
1435
1461
  }
1436
1462
  itf = maybeItf;
1437
1463
  }
@@ -1440,8 +1466,11 @@ class FieldBasedType extends BaseNamedType {
1440
1466
  }
1441
1467
  toAdd = new InterfaceImplementation(itf);
1442
1468
  }
1443
- 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);
1444
1470
  if (!existing) {
1471
+ if (!this._interfaceImplementations) {
1472
+ this._interfaceImplementations = new utils_1.MapWithCachedArrays();
1473
+ }
1445
1474
  this._interfaceImplementations.set(toAdd.interface.name, toAdd);
1446
1475
  addReferenceToType(this, toAdd.interface);
1447
1476
  Element.prototype['setParent'].call(toAdd, this);
@@ -1484,10 +1513,10 @@ class FieldBasedType extends BaseNamedType {
1484
1513
  toAdd = nameOrField;
1485
1514
  }
1486
1515
  if (this.field(toAdd.name)) {
1487
- 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}`);
1488
1517
  }
1489
1518
  if (type && !isOutputType(type)) {
1490
- 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.`);
1491
1520
  }
1492
1521
  this._fields.set(toAdd.name, toAdd);
1493
1522
  this._cachedNonBuiltInFields = undefined;
@@ -1505,11 +1534,13 @@ class FieldBasedType extends BaseNamedType {
1505
1534
  }
1506
1535
  }
1507
1536
  removeInterfaceImplementation(itf) {
1508
- this._interfaceImplementations.delete(itf.name);
1537
+ var _a;
1538
+ (_a = this._interfaceImplementations) === null || _a === void 0 ? void 0 : _a.delete(itf.name);
1509
1539
  removeReferenceToType(this, itf);
1510
1540
  }
1511
1541
  removeTypeReference(type) {
1512
- this._interfaceImplementations.delete(type.name);
1542
+ var _a;
1543
+ (_a = this._interfaceImplementations) === null || _a === void 0 ? void 0 : _a.delete(type.name);
1513
1544
  }
1514
1545
  removeInnerElements() {
1515
1546
  for (const interfaceImpl of this.interfaceImplementations()) {
@@ -1569,7 +1600,7 @@ class InterfaceType extends FieldBasedType {
1569
1600
  this.astDefinitionKind = graphql_1.Kind.INTERFACE_TYPE_DEFINITION;
1570
1601
  }
1571
1602
  allImplementations() {
1572
- 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');
1573
1604
  }
1574
1605
  possibleRuntimeTypes() {
1575
1606
  return this.allImplementations().filter(impl => impl.kind === 'ObjectType');
@@ -1633,10 +1664,10 @@ class UnionType extends BaseNamedType {
1633
1664
  this.checkUpdate();
1634
1665
  const maybeObj = this.schema().type(nameOrTypeOrMember);
1635
1666
  if (!maybeObj) {
1636
- 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}`);
1637
1668
  }
1638
1669
  else if (maybeObj.kind != 'ObjectType') {
1639
- 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}`);
1640
1671
  }
1641
1672
  obj = maybeObj;
1642
1673
  }
@@ -1734,13 +1765,10 @@ class EnumType extends BaseNamedType {
1734
1765
  (0, utils_1.assert)(false, `Eum type ${this} can't reference other types; shouldn't be asked to remove reference to ${type}`);
1735
1766
  }
1736
1767
  removeValueInternal(value) {
1737
- const index = this._values.indexOf(value);
1738
- if (index >= 0) {
1739
- this._values.splice(index, 1);
1740
- }
1768
+ (0, utils_1.removeArrayElement)(value, this._values);
1741
1769
  }
1742
1770
  removeInnerElements() {
1743
- const values = Array.from(this._values);
1771
+ const values = this.values;
1744
1772
  for (const value of values) {
1745
1773
  value.remove();
1746
1774
  }
@@ -1776,10 +1804,10 @@ class InputObjectType extends BaseNamedType {
1776
1804
  const toAdd = typeof nameOrField === 'string' ? new InputFieldDefinition(nameOrField) : nameOrField;
1777
1805
  this.checkUpdate(toAdd);
1778
1806
  if (this.field(toAdd.name)) {
1779
- 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}`);
1780
1808
  }
1781
1809
  if (type && !isInputType(type)) {
1782
- 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.`);
1783
1811
  }
1784
1812
  this._fields.set(toAdd.name, toAdd);
1785
1813
  this._cachedFieldsArray = undefined;
@@ -1867,7 +1895,6 @@ class FieldDefinition extends NamedSchemaElementWithType {
1867
1895
  super(name);
1868
1896
  this.isBuiltIn = isBuiltIn;
1869
1897
  this.kind = 'FieldDefinition';
1870
- this._args = new utils_1.MapWithCachedArrays();
1871
1898
  }
1872
1899
  isElementBuiltIn() {
1873
1900
  return this.isBuiltIn;
@@ -1877,13 +1904,15 @@ class FieldDefinition extends NamedSchemaElementWithType {
1877
1904
  return `${parent == undefined ? '<detached>' : parent.coordinate}.${this.name}`;
1878
1905
  }
1879
1906
  hasArguments() {
1880
- return this._args.size > 0;
1907
+ return !!this._args && this._args.size > 0;
1881
1908
  }
1882
1909
  arguments() {
1883
- 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 : [];
1884
1912
  }
1885
1913
  argument(name) {
1886
- return this._args.get(name);
1914
+ var _a;
1915
+ return (_a = this._args) === null || _a === void 0 ? void 0 : _a.get(name);
1887
1916
  }
1888
1917
  addArgument(nameOrArg, type, defaultValue) {
1889
1918
  let toAdd;
@@ -1899,15 +1928,18 @@ class FieldDefinition extends NamedSchemaElementWithType {
1899
1928
  const existing = this.argument(toAdd.name);
1900
1929
  if (existing) {
1901
1930
  if (type && existing.type && !(0, types_1.sameType)(type, existing.type)) {
1902
- 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})`);
1903
1932
  }
1904
1933
  if (defaultValue && (!existing.defaultValue || !(0, values_1.valueEquals)(defaultValue, existing.defaultValue))) {
1905
- 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)})`);
1906
1935
  }
1907
1936
  return existing;
1908
1937
  }
1909
1938
  if (type && !isInputType(type)) {
1910
- 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();
1911
1943
  }
1912
1944
  this._args.set(toAdd.name, toAdd);
1913
1945
  Element.prototype['setParent'].call(toAdd, this);
@@ -1926,7 +1958,7 @@ class FieldDefinition extends NamedSchemaElementWithType {
1926
1958
  setOfExtension(extension) {
1927
1959
  var _a;
1928
1960
  this.checkUpdate();
1929
- (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}`);
1930
1962
  this._extension = extension;
1931
1963
  this.onModification();
1932
1964
  }
@@ -1937,7 +1969,9 @@ class FieldDefinition extends NamedSchemaElementWithType {
1937
1969
  return introspection_1.introspectionFieldNames.includes(this.name);
1938
1970
  }
1939
1971
  removeArgumentInternal(name) {
1940
- this._args.delete(name);
1972
+ if (this._args) {
1973
+ this._args.delete(name);
1974
+ }
1941
1975
  }
1942
1976
  removeParent() {
1943
1977
  this._parent = undefined;
@@ -1970,9 +2004,9 @@ class FieldDefinition extends NamedSchemaElementWithType {
1970
2004
  }
1971
2005
  }
1972
2006
  toString() {
1973
- const args = this._args.size == 0
1974
- ? ""
1975
- : '(' + this.arguments().map(arg => arg.toString()).join(', ') + ')';
2007
+ const args = this.hasArguments()
2008
+ ? '(' + this.arguments().map(arg => arg.toString()).join(', ') + ')'
2009
+ : "";
1976
2010
  return `${this.name}${args}: ${this.type}`;
1977
2011
  }
1978
2012
  }
@@ -1998,7 +2032,7 @@ class InputFieldDefinition extends NamedSchemaElementWithType {
1998
2032
  setOfExtension(extension) {
1999
2033
  var _a;
2000
2034
  this.checkUpdate();
2001
- (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}`);
2002
2036
  this._extension = extension;
2003
2037
  this.onModification();
2004
2038
  }
@@ -2091,7 +2125,7 @@ class EnumValue extends NamedSchemaElement {
2091
2125
  setOfExtension(extension) {
2092
2126
  var _a;
2093
2127
  this.checkUpdate();
2094
- (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}`);
2095
2129
  this._extension = extension;
2096
2130
  this.onModification();
2097
2131
  }
@@ -2124,19 +2158,19 @@ class DirectiveDefinition extends NamedSchemaElement {
2124
2158
  super(name);
2125
2159
  this.isBuiltIn = isBuiltIn;
2126
2160
  this.kind = 'DirectiveDefinition';
2127
- this._args = new utils_1.MapWithCachedArrays();
2128
2161
  this.repeatable = false;
2129
2162
  this._locations = [];
2130
- this._referencers = new Set();
2131
2163
  }
2132
2164
  get coordinate() {
2133
2165
  return `@${this.name}`;
2134
2166
  }
2135
2167
  arguments() {
2136
- 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 : [];
2137
2170
  }
2138
2171
  argument(name) {
2139
- return this._args.get(name);
2172
+ var _a;
2173
+ return (_a = this._args) === null || _a === void 0 ? void 0 : _a.get(name);
2140
2174
  }
2141
2175
  addArgument(nameOrArg, type, defaultValue) {
2142
2176
  let toAdd;
@@ -2150,7 +2184,10 @@ class DirectiveDefinition extends NamedSchemaElement {
2150
2184
  toAdd = nameOrArg;
2151
2185
  }
2152
2186
  if (this.argument(toAdd.name)) {
2153
- 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();
2154
2191
  }
2155
2192
  this._args.set(toAdd.name, toAdd);
2156
2193
  Element.prototype['setParent'].call(toAdd, this);
@@ -2161,7 +2198,8 @@ class DirectiveDefinition extends NamedSchemaElement {
2161
2198
  return toAdd;
2162
2199
  }
2163
2200
  removeArgumentInternal(name) {
2164
- this._args.delete(name);
2201
+ var _a;
2202
+ (_a = this._args) === null || _a === void 0 ? void 0 : _a.delete(name);
2165
2203
  }
2166
2204
  get locations() {
2167
2205
  return this._locations;
@@ -2188,11 +2226,7 @@ class DirectiveDefinition extends NamedSchemaElement {
2188
2226
  removeLocations(...locations) {
2189
2227
  let modified = false;
2190
2228
  for (const location of locations) {
2191
- const index = this._locations.indexOf(location);
2192
- if (index >= 0) {
2193
- this._locations.splice(index, 1);
2194
- modified = true;
2195
- }
2229
+ modified || (modified = (0, utils_1.removeArrayElement)(location, this._locations));
2196
2230
  }
2197
2231
  if (modified) {
2198
2232
  this.onModification();
@@ -2206,31 +2240,42 @@ class DirectiveDefinition extends NamedSchemaElement {
2206
2240
  return this.locations.some((loc) => isTypeSystemDirectiveLocation(loc));
2207
2241
  }
2208
2242
  applications() {
2209
- return (0, utils_1.setValues)(this._referencers);
2243
+ var _a;
2244
+ return (_a = this._referencers) !== null && _a !== void 0 ? _a : [];
2210
2245
  }
2211
2246
  addReferencer(referencer) {
2212
2247
  (0, utils_1.assert)(referencer, 'Referencer should exists');
2213
- 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
+ }
2214
2256
  }
2215
2257
  removeReferencer(referencer) {
2216
- this._referencers.delete(referencer);
2258
+ if (this._referencers) {
2259
+ (0, utils_1.removeArrayElement)(referencer, this._referencers);
2260
+ }
2217
2261
  }
2218
2262
  removeTypeReference(type) {
2219
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}`);
2220
2264
  }
2221
2265
  remove() {
2266
+ var _a;
2222
2267
  if (!this._parent) {
2223
2268
  return [];
2224
2269
  }
2225
2270
  this.checkRemoval();
2226
2271
  this.onModification();
2227
2272
  this.sourceAST = undefined;
2228
- (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");
2229
2274
  for (const arg of this.arguments()) {
2230
2275
  arg.remove();
2231
2276
  }
2232
- const toReturn = (0, utils_1.setValues)(this._referencers);
2233
- this._referencers.clear();
2277
+ const toReturn = (_a = this._referencers) !== null && _a !== void 0 ? _a : [];
2278
+ this._referencers = undefined;
2234
2279
  Schema.prototype['removeDirectiveInternal'].call(this._parent, this);
2235
2280
  this._parent = undefined;
2236
2281
  return toReturn;
@@ -2318,7 +2363,7 @@ class Directive extends Element {
2318
2363
  if (extension) {
2319
2364
  const parent = this.parent;
2320
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.');
2321
- (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}`);
2322
2367
  }
2323
2368
  this._extension = extension;
2324
2369
  this.onModification();
@@ -2368,9 +2413,8 @@ class Directive extends Element {
2368
2413
  DirectiveDefinition.prototype['removeReferencer'].call(definition, this);
2369
2414
  }
2370
2415
  const parentDirectives = this._parent.appliedDirectives;
2371
- const index = parentDirectives.indexOf(this);
2372
- (0, utils_1.assert)(index >= 0, () => `Directive ${this} lists ${this._parent} as parent, but that parent doesn't list it as applied directive`);
2373
- 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`);
2374
2418
  this._parent = undefined;
2375
2419
  this._extension = undefined;
2376
2420
  return true;
@@ -2543,7 +2587,7 @@ function variableDefinitionsFromAST(schema, definitionNodes) {
2543
2587
  for (const definitionNode of definitionNodes) {
2544
2588
  if (!definitions.add(variableDefinitionFromAST(schema, definitionNode))) {
2545
2589
  const name = definitionNode.variable.name.value;
2546
- 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) });
2547
2591
  }
2548
2592
  }
2549
2593
  return definitions;
@@ -2553,7 +2597,7 @@ function variableDefinitionFromAST(schema, definitionNode) {
2553
2597
  const variable = new Variable(definitionNode.variable.name.value);
2554
2598
  const type = typeFromAST(schema, definitionNode.type);
2555
2599
  if (!isInputType(type)) {
2556
- 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 });
2557
2601
  }
2558
2602
  const def = new VariableDefinition(schema, variable, type, definitionNode.defaultValue ? (0, values_1.valueFromAST)(definitionNode.defaultValue, type) : undefined);
2559
2603
  return def;