@apollo/federation-internals 2.0.2-alpha.1 → 2.0.3

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 (45) hide show
  1. package/CHANGELOG.md +6 -27
  2. package/dist/buildSchema.d.ts.map +1 -1
  3. package/dist/buildSchema.js +76 -49
  4. package/dist/buildSchema.js.map +1 -1
  5. package/dist/definitions.d.ts +19 -3
  6. package/dist/definitions.d.ts.map +1 -1
  7. package/dist/definitions.js +110 -10
  8. package/dist/definitions.js.map +1 -1
  9. package/dist/directiveAndTypeSpecification.js +1 -1
  10. package/dist/directiveAndTypeSpecification.js.map +1 -1
  11. package/dist/federation.d.ts +1 -0
  12. package/dist/federation.d.ts.map +1 -1
  13. package/dist/federation.js +20 -0
  14. package/dist/federation.js.map +1 -1
  15. package/dist/graphQLJSSchemaToAST.d.ts +8 -0
  16. package/dist/graphQLJSSchemaToAST.d.ts.map +1 -0
  17. package/dist/graphQLJSSchemaToAST.js +96 -0
  18. package/dist/graphQLJSSchemaToAST.js.map +1 -0
  19. package/dist/index.d.ts +1 -0
  20. package/dist/index.d.ts.map +1 -1
  21. package/dist/index.js +1 -0
  22. package/dist/index.js.map +1 -1
  23. package/dist/operations.d.ts +2 -1
  24. package/dist/operations.d.ts.map +1 -1
  25. package/dist/operations.js +29 -18
  26. package/dist/operations.js.map +1 -1
  27. package/dist/print.d.ts +2 -1
  28. package/dist/print.d.ts.map +1 -1
  29. package/dist/print.js +23 -15
  30. package/dist/print.js.map +1 -1
  31. package/package.json +2 -2
  32. package/src/__tests__/definitions.test.ts +138 -1
  33. package/src/__tests__/graphQLJSSchemaToAST.test.ts +156 -0
  34. package/src/__tests__/schemaUpgrader.test.ts +0 -2
  35. package/src/__tests__/subgraphValidation.test.ts +58 -0
  36. package/src/buildSchema.ts +138 -47
  37. package/src/definitions.ts +146 -16
  38. package/src/directiveAndTypeSpecification.ts +1 -1
  39. package/src/federation.ts +40 -0
  40. package/src/graphQLJSSchemaToAST.ts +138 -0
  41. package/src/index.ts +1 -0
  42. package/src/operations.ts +44 -18
  43. package/src/print.ts +30 -15
  44. package/tsconfig.test.tsbuildinfo +1 -1
  45. package/tsconfig.tsbuildinfo +1 -1
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
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;
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;
5
5
  const graphql_1 = require("graphql");
6
6
  const coreSpec_1 = require("./coreSpec");
7
7
  const utils_1 = require("./utils");
@@ -213,6 +213,10 @@ function runtimeTypesIntersects(t1, t2) {
213
213
  return false;
214
214
  }
215
215
  exports.runtimeTypesIntersects = runtimeTypesIntersects;
216
+ function isConditionalDirective(directive) {
217
+ return ['include', 'skip'].includes(directive.name);
218
+ }
219
+ exports.isConditionalDirective = isConditionalDirective;
216
220
  exports.executableDirectiveLocations = [
217
221
  graphql_1.DirectiveLocation.QUERY,
218
222
  graphql_1.DirectiveLocation.MUTATION,
@@ -480,6 +484,7 @@ class BaseNamedType extends NamedSchemaElement {
480
484
  this.isBuiltIn = isBuiltIn;
481
485
  this._referencers = new Set();
482
486
  this._extensions = new Set();
487
+ this.preserveEmptyDefinition = false;
483
488
  }
484
489
  addReferencer(referencer) {
485
490
  this._referencers.add(referencer);
@@ -528,7 +533,9 @@ class BaseNamedType extends NamedSchemaElement {
528
533
  return this._extensions.size > 0;
529
534
  }
530
535
  hasNonExtensionElements() {
531
- return this._appliedDirectives.some(d => d.ofExtension() === undefined) || this.hasNonExtensionInnerElements();
536
+ return this.preserveEmptyDefinition
537
+ || this._appliedDirectives.some(d => d.ofExtension() === undefined)
538
+ || this.hasNonExtensionInnerElements();
532
539
  }
533
540
  isElementBuiltIn() {
534
541
  return this.isBuiltIn;
@@ -745,7 +752,7 @@ class CoreFeatures {
745
752
  this.byIdentity.set(feature.url.identity, feature);
746
753
  }
747
754
  sourceFeature(element) {
748
- const isDirective = element instanceof DirectiveDefinition;
755
+ const isDirective = element instanceof DirectiveDefinition || element instanceof Directive;
749
756
  const splitted = element.name.split('__');
750
757
  if (splitted.length > 1) {
751
758
  return this.byAlias.get(splitted[0]);
@@ -869,12 +876,53 @@ class Schema {
869
876
  }
870
877
  return this.apiSchema;
871
878
  }
872
- toGraphQLJSSchema(isSubgraph = false) {
873
- if (!isSubgraph) {
874
- return (0, graphql_1.buildASTSchema)(this.toAST());
879
+ emptyASTDefinitionsForExtensionsWithoutDefinition() {
880
+ const nodes = [];
881
+ if (this.schemaDefinition.hasExtensionElements() && !this.schemaDefinition.hasNonExtensionElements()) {
882
+ const node = { kind: graphql_1.Kind.SCHEMA_DEFINITION, operationTypes: [] };
883
+ nodes.push(node);
884
+ }
885
+ for (const type of this.types()) {
886
+ if (type.hasExtensionElements() && !type.hasNonExtensionElements()) {
887
+ const node = {
888
+ kind: type.astDefinitionKind,
889
+ name: { kind: graphql_1.Kind.NAME, value: type.name },
890
+ };
891
+ nodes.push(node);
892
+ }
875
893
  }
876
- const ast = (0, graphql_1.parse)((0, print_1.printSchema)(this, { ...print_1.defaultPrintOptions, mergeTypesAndExtensions: true }), { noLocation: true });
877
- return (0, graphql_1.buildASTSchema)(ast);
894
+ return nodes;
895
+ }
896
+ toGraphQLJSSchema() {
897
+ let ast = this.toAST();
898
+ const additionalNodes = this.emptyASTDefinitionsForExtensionsWithoutDefinition();
899
+ if (additionalNodes.length > 0) {
900
+ ast = {
901
+ kind: graphql_1.Kind.DOCUMENT,
902
+ definitions: ast.definitions.concat(additionalNodes),
903
+ };
904
+ }
905
+ const graphQLSchema = (0, graphql_1.buildASTSchema)(ast);
906
+ if (additionalNodes.length > 0) {
907
+ for (const node of additionalNodes) {
908
+ switch (node.kind) {
909
+ case graphql_1.Kind.SCHEMA_DEFINITION:
910
+ graphQLSchema.astNode = undefined;
911
+ break;
912
+ case graphql_1.Kind.SCALAR_TYPE_DEFINITION:
913
+ case graphql_1.Kind.OBJECT_TYPE_DEFINITION:
914
+ case graphql_1.Kind.INTERFACE_TYPE_DEFINITION:
915
+ case graphql_1.Kind.ENUM_TYPE_DEFINITION:
916
+ case graphql_1.Kind.UNION_TYPE_DEFINITION:
917
+ case graphql_1.Kind.INPUT_OBJECT_TYPE_DEFINITION:
918
+ const type = graphQLSchema.getType(node.name.value);
919
+ if (type) {
920
+ type.astNode = undefined;
921
+ }
922
+ }
923
+ }
924
+ }
925
+ return graphQLSchema;
878
926
  }
879
927
  get schemaDefinition() {
880
928
  return this._schemaDefinition;
@@ -1144,6 +1192,7 @@ class SchemaDefinition extends SchemaElement {
1144
1192
  this.kind = 'SchemaDefinition';
1145
1193
  this._roots = new utils_1.MapWithCachedArrays();
1146
1194
  this._extensions = new Set();
1195
+ this.preserveEmptyDefinition = false;
1147
1196
  }
1148
1197
  roots() {
1149
1198
  return this._roots.values();
@@ -1226,6 +1275,14 @@ class SchemaDefinition extends SchemaElement {
1226
1275
  this.onModification();
1227
1276
  return extension;
1228
1277
  }
1278
+ hasExtensionElements() {
1279
+ return this._extensions.size > 0;
1280
+ }
1281
+ hasNonExtensionElements() {
1282
+ return this.preserveEmptyDefinition
1283
+ || this._appliedDirectives.some((d) => d.ofExtension() === undefined)
1284
+ || this.roots().some((r) => r.ofExtension() === undefined);
1285
+ }
1229
1286
  removeRootType(rootType) {
1230
1287
  this._roots.delete(rootType.rootKind);
1231
1288
  removeReferenceToType(this, rootType.type);
@@ -1246,6 +1303,7 @@ class ScalarType extends BaseNamedType {
1246
1303
  constructor() {
1247
1304
  super(...arguments);
1248
1305
  this.kind = 'ScalarType';
1306
+ this.astDefinitionKind = graphql_1.Kind.SCALAR_TYPE_DEFINITION;
1249
1307
  }
1250
1308
  removeTypeReference(type) {
1251
1309
  (0, utils_1.assert)(false, `Scalar type ${this} can't reference other types; shouldn't be asked to remove reference to ${type}`);
@@ -1423,6 +1481,7 @@ class ObjectType extends FieldBasedType {
1423
1481
  constructor() {
1424
1482
  super(...arguments);
1425
1483
  this.kind = 'ObjectType';
1484
+ this.astDefinitionKind = graphql_1.Kind.OBJECT_TYPE_DEFINITION;
1426
1485
  }
1427
1486
  isRootType() {
1428
1487
  const schema = this.schema();
@@ -1451,6 +1510,7 @@ class InterfaceType extends FieldBasedType {
1451
1510
  constructor() {
1452
1511
  super(...arguments);
1453
1512
  this.kind = 'InterfaceType';
1513
+ this.astDefinitionKind = graphql_1.Kind.INTERFACE_TYPE_DEFINITION;
1454
1514
  }
1455
1515
  allImplementations() {
1456
1516
  return (0, utils_1.setValues)(this._referencers).filter(ref => ref.kind === 'ObjectType' || ref.kind === 'InterfaceType');
@@ -1483,6 +1543,7 @@ class UnionType extends BaseNamedType {
1483
1543
  constructor() {
1484
1544
  super(...arguments);
1485
1545
  this.kind = 'UnionType';
1546
+ this.astDefinitionKind = graphql_1.Kind.UNION_TYPE_DEFINITION;
1486
1547
  this._members = new utils_1.MapWithCachedArrays();
1487
1548
  }
1488
1549
  onAttached() {
@@ -1583,6 +1644,7 @@ class EnumType extends BaseNamedType {
1583
1644
  constructor() {
1584
1645
  super(...arguments);
1585
1646
  this.kind = 'EnumType';
1647
+ this.astDefinitionKind = graphql_1.Kind.ENUM_TYPE_DEFINITION;
1586
1648
  this._values = [];
1587
1649
  }
1588
1650
  get values() {
@@ -1622,7 +1684,10 @@ class EnumType extends BaseNamedType {
1622
1684
  }
1623
1685
  }
1624
1686
  removeInnerElements() {
1625
- this._values.splice(0, this._values.length);
1687
+ const values = Array.from(this._values);
1688
+ for (const value of values) {
1689
+ value.remove();
1690
+ }
1626
1691
  }
1627
1692
  hasNonExtensionInnerElements() {
1628
1693
  return this._values.some(v => v.ofExtension() === undefined);
@@ -1639,6 +1704,7 @@ class InputObjectType extends BaseNamedType {
1639
1704
  constructor() {
1640
1705
  super(...arguments);
1641
1706
  this.kind = 'InputObjectType';
1707
+ this.astDefinitionKind = graphql_1.Kind.INPUT_OBJECT_TYPE_DEFINITION;
1642
1708
  this._fields = new Map();
1643
1709
  }
1644
1710
  fields() {
@@ -2264,6 +2330,38 @@ class Directive extends Element {
2264
2330
  }
2265
2331
  }
2266
2332
  exports.Directive = Directive;
2333
+ function sameDirectiveApplication(application1, application2) {
2334
+ return application1.name === application2.name && (0, values_1.argumentsEquals)(application1.arguments(), application2.arguments());
2335
+ }
2336
+ exports.sameDirectiveApplication = sameDirectiveApplication;
2337
+ function sameDirectiveApplications(applications1, applications2) {
2338
+ if (applications1.length !== applications2.length) {
2339
+ return false;
2340
+ }
2341
+ for (const directive1 of applications1) {
2342
+ if (!applications2.some(directive2 => sameDirectiveApplication(directive1, directive2))) {
2343
+ return false;
2344
+ }
2345
+ }
2346
+ return true;
2347
+ }
2348
+ exports.sameDirectiveApplications = sameDirectiveApplications;
2349
+ function isDirectiveApplicationsSubset(applications, maybeSubset) {
2350
+ if (maybeSubset.length > applications.length) {
2351
+ return false;
2352
+ }
2353
+ for (const directive1 of maybeSubset) {
2354
+ if (!applications.some(directive2 => sameDirectiveApplication(directive1, directive2))) {
2355
+ return false;
2356
+ }
2357
+ }
2358
+ return true;
2359
+ }
2360
+ exports.isDirectiveApplicationsSubset = isDirectiveApplicationsSubset;
2361
+ function directiveApplicationsSubstraction(baseApplications, toRemove) {
2362
+ return baseApplications.filter((application) => !toRemove.some((other) => sameDirectiveApplication(application, other)));
2363
+ }
2364
+ exports.directiveApplicationsSubstraction = directiveApplicationsSubstraction;
2267
2365
  class Variable {
2268
2366
  constructor(name) {
2269
2367
  this.name = name;
@@ -2503,6 +2601,7 @@ function copyOfExtension(extensionsMap, source, dest) {
2503
2601
  }
2504
2602
  }
2505
2603
  function copySchemaDefinitionInner(source, dest) {
2604
+ dest.preserveEmptyDefinition = source.preserveEmptyDefinition;
2506
2605
  const extensionsMap = copyExtensions(source, dest);
2507
2606
  for (const rootType of source.roots()) {
2508
2607
  copyOfExtension(extensionsMap, rootType, dest.setRoot(rootType.rootKind, rootType.type.name));
@@ -2514,6 +2613,7 @@ function copySchemaDefinitionInner(source, dest) {
2514
2613
  dest.sourceAST = source.sourceAST;
2515
2614
  }
2516
2615
  function copyNamedTypeInner(source, dest) {
2616
+ dest.preserveEmptyDefinition = source.preserveEmptyDefinition;
2517
2617
  const extensionsMap = copyExtensions(source, dest);
2518
2618
  for (const directive of source.appliedDirectives) {
2519
2619
  copyOfExtension(extensionsMap, directive, copyAppliedDirective(directive, dest));