@apollo/federation-internals 2.0.0-preview.9 → 2.0.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 (78) hide show
  1. package/CHANGELOG.md +34 -0
  2. package/dist/coreSpec.d.ts +2 -1
  3. package/dist/coreSpec.d.ts.map +1 -1
  4. package/dist/coreSpec.js +37 -11
  5. package/dist/coreSpec.js.map +1 -1
  6. package/dist/definitions.d.ts +20 -8
  7. package/dist/definitions.d.ts.map +1 -1
  8. package/dist/definitions.js +102 -62
  9. package/dist/definitions.js.map +1 -1
  10. package/dist/directiveAndTypeSpecification.d.ts.map +1 -1
  11. package/dist/directiveAndTypeSpecification.js +10 -1
  12. package/dist/directiveAndTypeSpecification.js.map +1 -1
  13. package/dist/error.d.ts +10 -0
  14. package/dist/error.d.ts.map +1 -1
  15. package/dist/error.js +29 -9
  16. package/dist/error.js.map +1 -1
  17. package/dist/extractSubgraphsFromSupergraph.js +1 -1
  18. package/dist/extractSubgraphsFromSupergraph.js.map +1 -1
  19. package/dist/federation.d.ts +4 -1
  20. package/dist/federation.d.ts.map +1 -1
  21. package/dist/federation.js +48 -26
  22. package/dist/federation.js.map +1 -1
  23. package/dist/federationSpec.js +1 -1
  24. package/dist/federationSpec.js.map +1 -1
  25. package/dist/genErrorCodeDoc.js +12 -6
  26. package/dist/genErrorCodeDoc.js.map +1 -1
  27. package/dist/inaccessibleSpec.d.ts +9 -4
  28. package/dist/inaccessibleSpec.d.ts.map +1 -1
  29. package/dist/inaccessibleSpec.js +625 -32
  30. package/dist/inaccessibleSpec.js.map +1 -1
  31. package/dist/joinSpec.d.ts +2 -1
  32. package/dist/joinSpec.d.ts.map +1 -1
  33. package/dist/joinSpec.js +3 -0
  34. package/dist/joinSpec.js.map +1 -1
  35. package/dist/operations.d.ts +20 -1
  36. package/dist/operations.d.ts.map +1 -1
  37. package/dist/operations.js +52 -1
  38. package/dist/operations.js.map +1 -1
  39. package/dist/precompute.d.ts.map +1 -1
  40. package/dist/precompute.js +2 -2
  41. package/dist/precompute.js.map +1 -1
  42. package/dist/schemaUpgrader.d.ts +8 -1
  43. package/dist/schemaUpgrader.d.ts.map +1 -1
  44. package/dist/schemaUpgrader.js +56 -6
  45. package/dist/schemaUpgrader.js.map +1 -1
  46. package/dist/supergraphs.d.ts.map +1 -1
  47. package/dist/supergraphs.js +1 -0
  48. package/dist/supergraphs.js.map +1 -1
  49. package/dist/validate.js +13 -7
  50. package/dist/validate.js.map +1 -1
  51. package/dist/values.d.ts +2 -2
  52. package/dist/values.d.ts.map +1 -1
  53. package/dist/values.js +13 -11
  54. package/dist/values.js.map +1 -1
  55. package/package.json +3 -3
  56. package/src/__tests__/coreSpec.test.ts +112 -0
  57. package/src/__tests__/removeInaccessibleElements.test.ts +2252 -177
  58. package/src/__tests__/schemaUpgrader.test.ts +38 -0
  59. package/src/__tests__/subgraphValidation.test.ts +96 -5
  60. package/src/__tests__/values.test.ts +315 -3
  61. package/src/coreSpec.ts +74 -16
  62. package/src/definitions.ts +207 -87
  63. package/src/directiveAndTypeSpecification.ts +18 -1
  64. package/src/error.ts +69 -9
  65. package/src/extractSubgraphsFromSupergraph.ts +1 -1
  66. package/src/federation.ts +86 -26
  67. package/src/federationSpec.ts +2 -2
  68. package/src/genErrorCodeDoc.ts +13 -7
  69. package/src/inaccessibleSpec.ts +978 -56
  70. package/src/joinSpec.ts +5 -1
  71. package/src/operations.ts +68 -2
  72. package/src/precompute.ts +2 -4
  73. package/src/schemaUpgrader.ts +70 -6
  74. package/src/supergraphs.ts +1 -0
  75. package/src/validate.ts +20 -9
  76. package/src/values.ts +39 -12
  77. package/tsconfig.test.tsbuildinfo +1 -1
  78. package/tsconfig.tsbuildinfo +1 -1
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ScalarType = exports.SchemaDefinition = exports.RootType = exports.Schema = 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.runtimeTypesIntersects = exports.possibleRuntimeTypes = exports.isCompositeType = exports.isAbstractType = exports.isNullableType = exports.baseType = 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.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.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 = void 0;
3
+ exports.Schema = 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.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.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 = void 0;
5
5
  const graphql_1 = require("graphql");
6
6
  const coreSpec_1 = require("./coreSpec");
7
7
  const utils_1 = require("./utils");
@@ -21,13 +21,19 @@ const error_2 = require("./error");
21
21
  const validationErrorCode = 'GraphQLValidationFailed';
22
22
  const DEFAULT_VALIDATION_ERROR_MESSAGE = 'The schema is not a valid GraphQL schema.';
23
23
  const ErrGraphQLValidationFailed = (causes, message = DEFAULT_VALIDATION_ERROR_MESSAGE) => (0, core_schema_1.err)(validationErrorCode, {
24
- message,
24
+ message: message + '. Caused by:\n' + causes.map((c) => c.toString()).join('\n\n'),
25
25
  causes
26
26
  });
27
27
  exports.ErrGraphQLValidationFailed = ErrGraphQLValidationFailed;
28
+ 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
+ });
33
+ exports.ErrGraphQLAPISchemaValidationFailed = ErrGraphQLAPISchemaValidationFailed;
28
34
  function errorCauses(e) {
29
35
  if (e instanceof error_1.GraphQLErrorExt) {
30
- if (e.code === validationErrorCode) {
36
+ if (e.code === validationErrorCode || e.code === apiSchemaValidationErrorCode) {
31
37
  return (e.causes);
32
38
  }
33
39
  return [e];
@@ -159,6 +165,19 @@ function isInputType(type) {
159
165
  }
160
166
  }
161
167
  exports.isInputType = isInputType;
168
+ function isTypeOfKind(type, kind) {
169
+ return type.kind === kind;
170
+ }
171
+ exports.isTypeOfKind = isTypeOfKind;
172
+ function filterTypesOfKind(types, kind) {
173
+ return types.reduce((acc, type) => {
174
+ if (isTypeOfKind(type, kind)) {
175
+ acc.push(type);
176
+ }
177
+ return acc;
178
+ }, []);
179
+ }
180
+ exports.filterTypesOfKind = filterTypesOfKind;
162
181
  function baseType(type) {
163
182
  return isWrapperType(type) ? type.baseType() : type;
164
183
  }
@@ -374,36 +393,26 @@ class SchemaElement extends Element {
374
393
  ? this.appliedDirectivesOf(nameOrDefinition)
375
394
  : this.appliedDirectivesOf(nameOrDefinition)).length !== 0;
376
395
  }
377
- applyDirective(nameOrDefOrDirective, args, asFirstDirective = false) {
396
+ applyDirective(nameOrDef, args, asFirstDirective = false) {
378
397
  var _a;
379
- let toAdd;
380
- if (nameOrDefOrDirective instanceof Directive) {
381
- this.checkUpdate(nameOrDefOrDirective);
382
- toAdd = nameOrDefOrDirective;
383
- if (args) {
384
- toAdd.setArguments(args);
398
+ let name;
399
+ if (typeof nameOrDef === 'string') {
400
+ this.checkUpdate();
401
+ const def = (_a = this.schema().directive(nameOrDef)) !== null && _a !== void 0 ? _a : this.schema().blueprint.onMissingDirectiveDefinition(this.schema(), nameOrDef, args);
402
+ if (!def) {
403
+ throw this.schema().blueprint.onGraphQLJSValidationError(this.schema(), new graphql_1.GraphQLError(`Unknown directive "@${nameOrDef}".`));
404
+ }
405
+ if (Array.isArray(def)) {
406
+ throw (0, exports.ErrGraphQLValidationFailed)(def);
385
407
  }
408
+ name = nameOrDef;
386
409
  }
387
410
  else {
388
- let name;
389
- if (typeof nameOrDefOrDirective === 'string') {
390
- this.checkUpdate();
391
- const def = (_a = this.schema().directive(nameOrDefOrDirective)) !== null && _a !== void 0 ? _a : this.schema().blueprint.onMissingDirectiveDefinition(this.schema(), nameOrDefOrDirective, args);
392
- if (!def) {
393
- throw this.schema().blueprint.onGraphQLJSValidationError(this.schema(), new graphql_1.GraphQLError(`Unknown directive "@${nameOrDefOrDirective}".`));
394
- }
395
- if (Array.isArray(def)) {
396
- throw (0, exports.ErrGraphQLValidationFailed)(def);
397
- }
398
- name = nameOrDefOrDirective;
399
- }
400
- else {
401
- this.checkUpdate(nameOrDefOrDirective);
402
- name = nameOrDefOrDirective.name;
403
- }
404
- toAdd = new Directive(name, args !== null && args !== void 0 ? args : Object.create(null));
405
- Element.prototype['setParent'].call(toAdd, this);
411
+ this.checkUpdate(nameOrDef);
412
+ name = nameOrDef.name;
406
413
  }
414
+ const toAdd = new Directive(name, args !== null && args !== void 0 ? args : Object.create(null));
415
+ Element.prototype['setParent'].call(toAdd, this);
407
416
  if (asFirstDirective) {
408
417
  this._appliedDirectives.unshift(toAdd);
409
418
  }
@@ -537,15 +546,15 @@ class BaseNamedType extends NamedSchemaElement {
537
546
  }
538
547
  this.checkRemoval();
539
548
  this.onModification();
540
- this.removeInnerElements();
541
- Schema.prototype['removeTypeInternal'].call(this._parent, this);
542
- this.removeAppliedDirectives();
543
549
  this.sourceAST = undefined;
550
+ this.removeAppliedDirectives();
551
+ this.removeInnerElements();
544
552
  const toReturn = (0, utils_1.setValues)(this._referencers).map(r => {
545
553
  SchemaElement.prototype['removeTypeReferenceInternal'].call(r, this);
546
554
  return r;
547
555
  });
548
556
  this._referencers.clear();
557
+ Schema.prototype['removeTypeInternal'].call(this._parent, this);
549
558
  this._parent = undefined;
550
559
  return toReturn;
551
560
  }
@@ -665,17 +674,21 @@ class CoreFeature {
665
674
  this.purpose = purpose;
666
675
  }
667
676
  isFeatureDefinition(element) {
677
+ const importName = element.kind === 'DirectiveDefinition'
678
+ ? '@' + element.name
679
+ : element.name;
668
680
  return element.name.startsWith(this.nameInSchema + '__')
669
681
  || (element.kind === 'DirectiveDefinition' && element.name === this.nameInSchema)
670
- || !!this.imports.find((i) => { var _a; return element.name === ((_a = i.as) !== null && _a !== void 0 ? _a : i.name); });
682
+ || !!this.imports.find((i) => { var _a; return importName === ((_a = i.as) !== null && _a !== void 0 ? _a : i.name); });
671
683
  }
672
684
  directiveNameInSchema(name) {
673
685
  var _a, _b;
674
- if (name === this.url.name) {
675
- return this.nameInSchema;
676
- }
677
686
  const elementImport = this.imports.find((i) => i.name.charAt(0) === '@' && i.name.slice(1) === name);
678
- return elementImport ? ((_b = (_a = elementImport.as) === null || _a === void 0 ? void 0 : _a.slice(1)) !== null && _b !== void 0 ? _b : name) : this.nameInSchema + '__' + name;
687
+ return elementImport
688
+ ? ((_b = (_a = elementImport.as) === null || _a === void 0 ? void 0 : _a.slice(1)) !== null && _b !== void 0 ? _b : name)
689
+ : (name === this.url.name
690
+ ? this.nameInSchema
691
+ : this.nameInSchema + '__' + name);
679
692
  }
680
693
  typeNameInSchema(name) {
681
694
  var _a;
@@ -849,12 +862,7 @@ class Schema {
849
862
  this.validate();
850
863
  const apiSchema = this.clone();
851
864
  (0, inaccessibleSpec_1.removeInaccessibleElements)(apiSchema);
852
- const coreFeatures = apiSchema.coreFeatures;
853
- if (coreFeatures) {
854
- for (const coreFeature of coreFeatures.allFeatures()) {
855
- (0, coreSpec_1.removeFeatureElements)(apiSchema, coreFeature);
856
- }
857
- }
865
+ (0, coreSpec_1.removeAllCoreFeatures)(apiSchema);
858
866
  (0, utils_1.assert)(!apiSchema.isCoreSchema(), "The API schema shouldn't be a core schema");
859
867
  apiSchema.validate();
860
868
  this.apiSchema = apiSchema;
@@ -871,22 +879,38 @@ class Schema {
871
879
  get schemaDefinition() {
872
880
  return this._schemaDefinition;
873
881
  }
874
- types(kind) {
875
- const allKinds = this._types.values();
876
- return (kind ? allKinds.filter(t => t.kind === kind) : allKinds);
882
+ types() {
883
+ return this._types.values();
884
+ }
885
+ interfaceTypes() {
886
+ return filterTypesOfKind(this.types(), 'InterfaceType');
887
+ }
888
+ objectTypes() {
889
+ return filterTypesOfKind(this.types(), 'ObjectType');
890
+ }
891
+ unionTypes() {
892
+ return filterTypesOfKind(this.types(), 'UnionType');
877
893
  }
878
- builtInTypes(kind, includeShadowed = false) {
894
+ scalarTypes() {
895
+ return filterTypesOfKind(this.types(), 'ScalarType');
896
+ }
897
+ inputTypes() {
898
+ return filterTypesOfKind(this.types(), 'InputObjectType');
899
+ }
900
+ enumTypes() {
901
+ return filterTypesOfKind(this.types(), 'EnumType');
902
+ }
903
+ builtInTypes(includeShadowed = false) {
879
904
  const allBuiltIns = this._builtInTypes.values();
880
- const forKind = (kind ? allBuiltIns.filter(t => t.kind === kind) : allBuiltIns);
881
905
  return includeShadowed
882
- ? forKind
883
- : forKind.filter(t => !this.isShadowedBuiltInType(t));
906
+ ? allBuiltIns
907
+ : allBuiltIns.filter(t => !this.isShadowedBuiltInType(t));
884
908
  }
885
909
  isShadowedBuiltInType(type) {
886
910
  return type.isBuiltIn && this._types.has(type.name);
887
911
  }
888
- allTypes(kind) {
889
- return this.builtInTypes(kind).concat(this.types(kind));
912
+ allTypes() {
913
+ return this.builtInTypes().concat(this.types());
890
914
  }
891
915
  type(name) {
892
916
  const type = this._types.get(name);
@@ -1124,9 +1148,9 @@ class SchemaDefinition extends SchemaElement {
1124
1148
  roots() {
1125
1149
  return this._roots.values();
1126
1150
  }
1127
- applyDirective(nameOrDefOrDirective, args, asFirstDirective = false) {
1151
+ applyDirective(nameOrDef, args, asFirstDirective = false) {
1128
1152
  var _a, _b;
1129
- const applied = super.applyDirective(nameOrDefOrDirective, args, asFirstDirective);
1153
+ const applied = super.applyDirective(nameOrDef, args, asFirstDirective);
1130
1154
  const schema = this.schema();
1131
1155
  const coreFeatures = schema.coreFeatures;
1132
1156
  if ((0, coreSpec_1.isCoreSpecDirectiveApplication)(applied)) {
@@ -1562,7 +1586,7 @@ class EnumType extends BaseNamedType {
1562
1586
  this._values = [];
1563
1587
  }
1564
1588
  get values() {
1565
- return this._values;
1589
+ return Array.from(this._values);
1566
1590
  }
1567
1591
  value(name) {
1568
1592
  return this._values.find(v => v.name === name);
@@ -1805,15 +1829,17 @@ class FieldDefinition extends NamedSchemaElementWithType {
1805
1829
  if (!this._parent) {
1806
1830
  return [];
1807
1831
  }
1832
+ this.checkRemoval();
1808
1833
  this.onModification();
1809
- this.removeAppliedDirectives();
1834
+ this.sourceAST = undefined;
1810
1835
  this.type = undefined;
1811
- this._extension = undefined;
1836
+ this.removeAppliedDirectives();
1812
1837
  for (const arg of this.arguments()) {
1813
1838
  arg.remove();
1814
1839
  }
1815
1840
  FieldBasedType.prototype['removeFieldInternal'].call(this._parent, this);
1816
1841
  this._parent = undefined;
1842
+ this._extension = undefined;
1817
1843
  return [];
1818
1844
  }
1819
1845
  removeRecursive() {
@@ -1865,10 +1891,15 @@ class InputFieldDefinition extends NamedSchemaElementWithType {
1865
1891
  if (!this._parent) {
1866
1892
  return [];
1867
1893
  }
1894
+ this.checkRemoval();
1868
1895
  this.onModification();
1896
+ this.sourceAST = undefined;
1897
+ this.type = undefined;
1898
+ this.defaultValue = undefined;
1899
+ this.removeAppliedDirectives();
1869
1900
  InputObjectType.prototype['removeFieldInternal'].call(this._parent, this);
1870
1901
  this._parent = undefined;
1871
- this.type = undefined;
1902
+ this._extension = undefined;
1872
1903
  return [];
1873
1904
  }
1874
1905
  removeRecursive() {
@@ -1903,7 +1934,12 @@ class ArgumentDefinition extends NamedSchemaElementWithType {
1903
1934
  if (!this._parent) {
1904
1935
  return [];
1905
1936
  }
1937
+ this.checkRemoval();
1906
1938
  this.onModification();
1939
+ this.sourceAST = undefined;
1940
+ this.type = undefined;
1941
+ this.defaultValue = undefined;
1942
+ this.removeAppliedDirectives();
1907
1943
  if (this._parent instanceof FieldDefinition) {
1908
1944
  FieldDefinition.prototype['removeArgumentInternal'].call(this._parent, this.name);
1909
1945
  }
@@ -1911,8 +1947,6 @@ class ArgumentDefinition extends NamedSchemaElementWithType {
1911
1947
  DirectiveDefinition.prototype['removeArgumentInternal'].call(this._parent, this.name);
1912
1948
  }
1913
1949
  this._parent = undefined;
1914
- this.type = undefined;
1915
- this.defaultValue = undefined;
1916
1950
  return [];
1917
1951
  }
1918
1952
  toString() {
@@ -1952,9 +1986,13 @@ class EnumValue extends NamedSchemaElement {
1952
1986
  if (!this._parent) {
1953
1987
  return [];
1954
1988
  }
1989
+ this.checkRemoval();
1955
1990
  this.onModification();
1991
+ this.sourceAST = undefined;
1992
+ this.removeAppliedDirectives();
1956
1993
  EnumType.prototype['removeValueInternal'].call(this._parent, this);
1957
1994
  this._parent = undefined;
1995
+ this._extension = undefined;
1958
1996
  return [];
1959
1997
  }
1960
1998
  removeTypeReference(type) {
@@ -2062,15 +2100,17 @@ class DirectiveDefinition extends NamedSchemaElement {
2062
2100
  if (!this._parent) {
2063
2101
  return [];
2064
2102
  }
2103
+ this.checkRemoval();
2065
2104
  this.onModification();
2066
- Schema.prototype['removeDirectiveInternal'].call(this._parent, this);
2067
- this._parent = undefined;
2105
+ this.sourceAST = undefined;
2068
2106
  (0, utils_1.assert)(this._appliedDirectives.length === 0, "Directive definition should not have directive applied to it");
2069
2107
  for (const arg of this.arguments()) {
2070
2108
  arg.remove();
2071
2109
  }
2072
2110
  const toReturn = (0, utils_1.setValues)(this._referencers);
2073
2111
  this._referencers.clear();
2112
+ Schema.prototype['removeDirectiveInternal'].call(this._parent, this);
2113
+ this._parent = undefined;
2074
2114
  return toReturn;
2075
2115
  }
2076
2116
  removeRecursive() {