@graphql-tools/merge 6.2.9-alpha-3e5e5d87.0 → 6.2.11

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.
package/index.esm.js CHANGED
@@ -1,5 +1,5 @@
1
- import { mergeDeep, compareNodes, isNotEqual, getDocumentNodeFromSchema, createSchemaDefinition, asArray, getResolversFromSchema, SchemaDirectiveVisitor } from '@graphql-tools/utils';
2
- import { getDescription, visit, print, Source, Kind, isSchema, parse, isScalarType, isSpecifiedScalarType, isIntrospectionType, isObjectType, isInterfaceType, isInputObjectType, isUnionType, isEnumType, buildSchema, buildASTSchema } from 'graphql';
1
+ import { mergeDeep, compareNodes, isNotEqual, getDocumentNodeFromSchema, asArray, getResolversFromSchema, SchemaDirectiveVisitor } from '@graphql-tools/utils';
2
+ import { getDescription, visit, print, Source, Kind, isSchema, parse, isDefinitionNode, isScalarType, isSpecifiedScalarType, isIntrospectionType, isObjectType, isInterfaceType, isInputObjectType, isUnionType, isEnumType, buildSchema, buildASTSchema } from 'graphql';
3
3
  import { addResolversToSchema, addErrorLoggingToSchema } from '@graphql-tools/schema';
4
4
 
5
5
  /**
@@ -412,55 +412,13 @@ function isStringTypes(types) {
412
412
  function isSourceTypes(types) {
413
413
  return types instanceof Source;
414
414
  }
415
- function isGraphQLType(definition) {
416
- return definition.kind === 'ObjectTypeDefinition';
417
- }
418
- function isGraphQLTypeExtension(definition) {
419
- return definition.kind === 'ObjectTypeExtension';
420
- }
421
- function isGraphQLEnum(definition) {
422
- return definition.kind === 'EnumTypeDefinition';
423
- }
424
- function isGraphQLEnumExtension(definition) {
425
- return definition.kind === 'EnumTypeExtension';
426
- }
427
- function isGraphQLUnion(definition) {
428
- return definition.kind === 'UnionTypeDefinition';
429
- }
430
- function isGraphQLUnionExtension(definition) {
431
- return definition.kind === 'UnionTypeExtension';
432
- }
433
- function isGraphQLScalar(definition) {
434
- return definition.kind === 'ScalarTypeDefinition';
435
- }
436
- function isGraphQLScalarExtension(definition) {
437
- return definition.kind === 'ScalarTypeExtension';
438
- }
439
- function isGraphQLInputType(definition) {
440
- return definition.kind === 'InputObjectTypeDefinition';
441
- }
442
- function isGraphQLInputTypeExtension(definition) {
443
- return definition.kind === 'InputObjectTypeExtension';
444
- }
445
- function isGraphQLInterface(definition) {
446
- return definition.kind === 'InterfaceTypeDefinition';
447
- }
448
- function isGraphQLInterfaceExtension(definition) {
449
- return definition.kind === 'InterfaceTypeExtension';
450
- }
451
- function isGraphQLDirective(definition) {
452
- return definition.kind === 'DirectiveDefinition';
453
- }
454
415
  function extractType(type) {
455
416
  let visitedType = type;
456
- while (visitedType.kind === 'ListType' || visitedType.kind === 'NonNullType') {
417
+ while (visitedType.kind === Kind.LIST_TYPE || visitedType.kind === 'NonNullType') {
457
418
  visitedType = visitedType.type;
458
419
  }
459
420
  return visitedType;
460
421
  }
461
- function isSchemaDefinition(node) {
462
- return node.kind === 'SchemaDefinition';
463
- }
464
422
  function isWrappingTypeNode(type) {
465
423
  return type.kind !== Kind.NAMED_TYPE;
466
424
  }
@@ -479,10 +437,23 @@ function printTypeNode(type) {
479
437
  }
480
438
  return type.name.value;
481
439
  }
440
+ var CompareVal;
441
+ (function (CompareVal) {
442
+ CompareVal[CompareVal["A_SMALLER_THAN_B"] = -1] = "A_SMALLER_THAN_B";
443
+ CompareVal[CompareVal["A_EQUALS_B"] = 0] = "A_EQUALS_B";
444
+ CompareVal[CompareVal["A_GREATER_THAN_B"] = 1] = "A_GREATER_THAN_B";
445
+ })(CompareVal || (CompareVal = {}));
446
+ function defaultStringComparator(a, b) {
447
+ if (a < b)
448
+ return CompareVal.A_SMALLER_THAN_B;
449
+ if (a > b)
450
+ return CompareVal.A_GREATER_THAN_B;
451
+ return CompareVal.A_EQUALS_B;
452
+ }
482
453
 
483
- function fieldAlreadyExists(fieldsArr, otherField) {
454
+ function fieldAlreadyExists(fieldsArr, otherField, config) {
484
455
  const result = fieldsArr.find(field => field.name.value === otherField.name.value);
485
- if (result) {
456
+ if (result && !(config === null || config === void 0 ? void 0 : config.ignoreFieldConflicts)) {
486
457
  const t1 = extractType(result.type);
487
458
  const t2 = extractType(otherField.type);
488
459
  if (t1.name.value !== t2.name.value) {
@@ -494,16 +465,18 @@ function fieldAlreadyExists(fieldsArr, otherField) {
494
465
  function mergeFields(type, f1, f2, config) {
495
466
  const result = [...f2];
496
467
  for (const field of f1) {
497
- if (fieldAlreadyExists(result, field)) {
468
+ if (fieldAlreadyExists(result, field, config)) {
498
469
  const existing = result.find((f) => f.name.value === field.name.value);
499
- if (config && config.throwOnConflict) {
500
- preventConflicts(type, existing, field, false);
501
- }
502
- else {
503
- preventConflicts(type, existing, field, true);
504
- }
505
- if (isNonNullTypeNode(field.type) && !isNonNullTypeNode(existing.type)) {
506
- existing.type = field.type;
470
+ if (!(config === null || config === void 0 ? void 0 : config.ignoreFieldConflicts)) {
471
+ if (config === null || config === void 0 ? void 0 : config.throwOnConflict) {
472
+ preventConflicts(type, existing, field, false);
473
+ }
474
+ else {
475
+ preventConflicts(type, existing, field, true);
476
+ }
477
+ if (isNonNullTypeNode(field.type) && !isNonNullTypeNode(existing.type)) {
478
+ existing.type = field.type;
479
+ }
507
480
  }
508
481
  existing.arguments = mergeArguments(field['arguments'] || [], existing.arguments || [], config);
509
482
  existing.directives = mergeDirectives(field.directives, existing.directives, config);
@@ -693,43 +666,95 @@ function mergeUnion(first, second, config) {
693
666
  : first;
694
667
  }
695
668
 
669
+ const operationTypeDefinitionNodeTypeRootTypeMap = {
670
+ query: 'Query',
671
+ mutation: 'Mutation',
672
+ subscription: 'Subscription',
673
+ };
674
+ function mergeOperationTypes(opNodeList = [], existingOpNodeList = []) {
675
+ const finalOpNodeList = [];
676
+ for (const opNodeType in operationTypeDefinitionNodeTypeRootTypeMap) {
677
+ const opNode = opNodeList.find(n => n.operation === opNodeType) || existingOpNodeList.find(n => n.operation === opNodeType);
678
+ if (opNode) {
679
+ finalOpNodeList.push(opNode);
680
+ }
681
+ }
682
+ return finalOpNodeList;
683
+ }
684
+ function mergeSchemaDefs(node, existingNode, config) {
685
+ if (existingNode) {
686
+ return {
687
+ kind: (config === null || config === void 0 ? void 0 : config.convertExtensions) ||
688
+ node.kind === Kind.SCHEMA_DEFINITION ||
689
+ existingNode.kind === Kind.SCHEMA_DEFINITION
690
+ ? Kind.SCHEMA_DEFINITION
691
+ : Kind.SCHEMA_EXTENSION,
692
+ description: node['description'] || existingNode['description'],
693
+ directives: mergeDirectives(node.directives, existingNode.directives, config),
694
+ operationTypes: mergeOperationTypes(node.operationTypes, existingNode.operationTypes),
695
+ };
696
+ }
697
+ return ((config === null || config === void 0 ? void 0 : config.convertExtensions)
698
+ ? {
699
+ ...node,
700
+ kind: Kind.SCHEMA_EXTENSION,
701
+ }
702
+ : node);
703
+ }
704
+
705
+ function isNamedDefinitionNode(definitionNode) {
706
+ return 'name' in definitionNode;
707
+ }
708
+ const schemaDefSymbol = Symbol('schemaDefSymbol');
696
709
  function mergeGraphQLNodes(nodes, config) {
697
- return nodes.reduce((prev, nodeDefinition) => {
698
- const node = nodeDefinition;
699
- if (node && node.name && node.name.value) {
700
- const name = node.name.value;
701
- if (config && config.commentDescriptions) {
702
- collectComment(node);
710
+ var _a, _b;
711
+ const mergedResultMap = {};
712
+ for (const nodeDefinition of nodes) {
713
+ if (isNamedDefinitionNode(nodeDefinition)) {
714
+ const name = nodeDefinition.name.value;
715
+ if (config === null || config === void 0 ? void 0 : config.commentDescriptions) {
716
+ collectComment(nodeDefinition);
703
717
  }
704
- if (config &&
705
- config.exclusions &&
706
- (config.exclusions.includes(name + '.*') || config.exclusions.includes(name))) {
707
- delete prev[name];
718
+ if (((_a = config === null || config === void 0 ? void 0 : config.exclusions) === null || _a === void 0 ? void 0 : _a.includes(name + '.*')) || ((_b = config === null || config === void 0 ? void 0 : config.exclusions) === null || _b === void 0 ? void 0 : _b.includes(name))) {
719
+ delete mergedResultMap[name];
708
720
  }
709
- else if (isGraphQLType(nodeDefinition) || isGraphQLTypeExtension(nodeDefinition)) {
710
- prev[name] = mergeType(nodeDefinition, prev[name], config);
711
- }
712
- else if (isGraphQLEnum(nodeDefinition) || isGraphQLEnumExtension(nodeDefinition)) {
713
- prev[name] = mergeEnum(nodeDefinition, prev[name], config);
714
- }
715
- else if (isGraphQLUnion(nodeDefinition) || isGraphQLUnionExtension(nodeDefinition)) {
716
- prev[name] = mergeUnion(nodeDefinition, prev[name], config);
717
- }
718
- else if (isGraphQLScalar(nodeDefinition) || isGraphQLScalarExtension(nodeDefinition)) {
719
- prev[name] = mergeScalar(nodeDefinition, prev[name], config);
720
- }
721
- else if (isGraphQLInputType(nodeDefinition) || isGraphQLInputTypeExtension(nodeDefinition)) {
722
- prev[name] = mergeInputType(nodeDefinition, prev[name], config);
723
- }
724
- else if (isGraphQLInterface(nodeDefinition) || isGraphQLInterfaceExtension(nodeDefinition)) {
725
- prev[name] = mergeInterface(nodeDefinition, prev[name], config);
726
- }
727
- else if (isGraphQLDirective(nodeDefinition)) {
728
- prev[name] = mergeDirective(nodeDefinition, prev[name]);
721
+ else {
722
+ switch (nodeDefinition.kind) {
723
+ case Kind.OBJECT_TYPE_DEFINITION:
724
+ case Kind.OBJECT_TYPE_EXTENSION:
725
+ mergedResultMap[name] = mergeType(nodeDefinition, mergedResultMap[name], config);
726
+ break;
727
+ case Kind.ENUM_TYPE_DEFINITION:
728
+ case Kind.ENUM_TYPE_EXTENSION:
729
+ mergedResultMap[name] = mergeEnum(nodeDefinition, mergedResultMap[name], config);
730
+ break;
731
+ case Kind.UNION_TYPE_DEFINITION:
732
+ case Kind.UNION_TYPE_EXTENSION:
733
+ mergedResultMap[name] = mergeUnion(nodeDefinition, mergedResultMap[name], config);
734
+ break;
735
+ case Kind.SCALAR_TYPE_DEFINITION:
736
+ case Kind.SCALAR_TYPE_EXTENSION:
737
+ mergedResultMap[name] = mergeScalar(nodeDefinition, mergedResultMap[name], config);
738
+ break;
739
+ case Kind.INPUT_OBJECT_TYPE_DEFINITION:
740
+ case Kind.INPUT_OBJECT_TYPE_EXTENSION:
741
+ mergedResultMap[name] = mergeInputType(nodeDefinition, mergedResultMap[name], config);
742
+ break;
743
+ case Kind.INTERFACE_TYPE_DEFINITION:
744
+ case Kind.INTERFACE_TYPE_EXTENSION:
745
+ mergedResultMap[name] = mergeInterface(nodeDefinition, mergedResultMap[name], config);
746
+ break;
747
+ case Kind.DIRECTIVE_DEFINITION:
748
+ mergedResultMap[name] = mergeDirective(nodeDefinition, mergedResultMap[name]);
749
+ break;
750
+ }
729
751
  }
730
752
  }
731
- return prev;
732
- }, {});
753
+ else if (nodeDefinition.kind === Kind.SCHEMA_DEFINITION || nodeDefinition.kind === Kind.SCHEMA_EXTENSION) {
754
+ mergedResultMap[schemaDefSymbol] = mergeSchemaDefs(nodeDefinition, mergedResultMap[schemaDefSymbol], config);
755
+ }
756
+ }
757
+ return mergedResultMap;
733
758
  }
734
759
 
735
760
  function mergeTypeDefs(types, config) {
@@ -754,58 +779,86 @@ function mergeTypeDefs(types, config) {
754
779
  resetComments();
755
780
  return result;
756
781
  }
782
+ function visitTypeSources(types, allNodes) {
783
+ for (const type of types) {
784
+ if (type) {
785
+ if (Array.isArray(type)) {
786
+ visitTypeSources(types, allNodes);
787
+ }
788
+ else if (isSchema(type)) {
789
+ const documentNode = getDocumentNodeFromSchema(type);
790
+ visitTypeSources(documentNode.definitions, allNodes);
791
+ }
792
+ else if (isStringTypes(type) || isSourceTypes(type)) {
793
+ const documentNode = parse(type);
794
+ visitTypeSources(documentNode.definitions, allNodes);
795
+ }
796
+ else if (isDefinitionNode(type)) {
797
+ allNodes.push(type);
798
+ }
799
+ else {
800
+ visitTypeSources(type.definitions, allNodes);
801
+ }
802
+ }
803
+ }
804
+ }
757
805
  function mergeGraphQLTypes(types, config) {
806
+ var _a, _b;
758
807
  resetComments();
759
- const allNodes = types
760
- .map(type => {
761
- if (Array.isArray(type)) {
762
- type = mergeTypeDefs(type);
763
- }
764
- if (isSchema(type)) {
765
- return getDocumentNodeFromSchema(type);
766
- }
767
- else if (isStringTypes(type) || isSourceTypes(type)) {
768
- return parse(type);
769
- }
770
- return type;
771
- })
772
- .map(ast => ast.definitions)
773
- .reduce((defs, newDef = []) => [...defs, ...newDef], []);
774
- // XXX: right now we don't handle multiple schema definitions
775
- let schemaDef = allNodes.filter(isSchemaDefinition).reduce((def, node) => {
776
- node.operationTypes
777
- .filter(op => op.type.name.value)
778
- .forEach(op => {
779
- def[op.operation] = op.type.name.value;
780
- });
781
- return def;
782
- }, {
783
- query: null,
784
- mutation: null,
785
- subscription: null,
786
- });
808
+ const allNodes = [];
809
+ visitTypeSources(types, allNodes);
787
810
  const mergedNodes = mergeGraphQLNodes(allNodes, config);
788
- const allTypes = Object.keys(mergedNodes);
789
- if (config && config.sort) {
790
- allTypes.sort(typeof config.sort === 'function' ? config.sort : undefined);
811
+ // XXX: right now we don't handle multiple schema definitions
812
+ let schemaDef = mergedNodes[schemaDefSymbol] || {
813
+ kind: Kind.SCHEMA_DEFINITION,
814
+ operationTypes: [],
815
+ };
816
+ if (config === null || config === void 0 ? void 0 : config.useSchemaDefinition) {
817
+ const operationTypes = schemaDef.operationTypes;
818
+ for (const opTypeDefNodeType in operationTypeDefinitionNodeTypeRootTypeMap) {
819
+ const opTypeDefNode = operationTypes.find(operationType => operationType.operation === opTypeDefNodeType);
820
+ if (!opTypeDefNode) {
821
+ const existingPossibleRootType = mergedNodes[operationTypeDefinitionNodeTypeRootTypeMap[opTypeDefNodeType]];
822
+ if (existingPossibleRootType) {
823
+ operationTypes.push({
824
+ kind: Kind.OPERATION_TYPE_DEFINITION,
825
+ type: {
826
+ kind: Kind.NAMED_TYPE,
827
+ name: existingPossibleRootType.name,
828
+ },
829
+ operation: opTypeDefNodeType,
830
+ });
831
+ }
832
+ }
833
+ }
791
834
  }
792
- if (config && config.useSchemaDefinition) {
793
- const queryType = schemaDef.query ? schemaDef.query : allTypes.find(t => t === 'Query');
794
- const mutationType = schemaDef.mutation ? schemaDef.mutation : allTypes.find(t => t === 'Mutation');
795
- const subscriptionType = schemaDef.subscription ? schemaDef.subscription : allTypes.find(t => t === 'Subscription');
835
+ if ((config === null || config === void 0 ? void 0 : config.forceSchemaDefinition) && !((_a = schemaDef === null || schemaDef === void 0 ? void 0 : schemaDef.operationTypes) === null || _a === void 0 ? void 0 : _a.length)) {
796
836
  schemaDef = {
797
- query: queryType,
798
- mutation: mutationType,
799
- subscription: subscriptionType,
837
+ kind: Kind.SCHEMA_DEFINITION,
838
+ operationTypes: [
839
+ {
840
+ kind: Kind.OPERATION_TYPE_DEFINITION,
841
+ operation: 'query',
842
+ type: {
843
+ kind: Kind.NAMED_TYPE,
844
+ name: {
845
+ kind: Kind.NAME,
846
+ value: 'Query',
847
+ },
848
+ },
849
+ },
850
+ ],
800
851
  };
801
852
  }
802
- const schemaDefinition = createSchemaDefinition(schemaDef, {
803
- force: config.forceSchemaDefinition,
804
- });
805
- if (!schemaDefinition) {
806
- return Object.values(mergedNodes);
853
+ const mergedNodeDefinitions = Object.values(mergedNodes);
854
+ if ((_b = schemaDef.operationTypes) === null || _b === void 0 ? void 0 : _b.length) {
855
+ mergedNodeDefinitions.push(schemaDef);
856
+ }
857
+ if (config === null || config === void 0 ? void 0 : config.sort) {
858
+ const sortFn = typeof config.sort === 'function' ? config.sort : defaultStringComparator;
859
+ mergedNodeDefinitions.sort((a, b) => { var _a, _b; return sortFn((_a = a.name) === null || _a === void 0 ? void 0 : _a.value, (_b = b.name) === null || _b === void 0 ? void 0 : _b.value); });
807
860
  }
808
- return [...Object.values(mergedNodes), parse(schemaDefinition).definitions[0]];
861
+ return mergedNodeDefinitions;
809
862
  }
810
863
 
811
864
  function travelSchemaPossibleExtensions(schema, hooks) {
@@ -1000,5 +1053,5 @@ function makeSchema({ resolvers, typeDefs, extensions, }, config) {
1000
1053
  return schema;
1001
1054
  }
1002
1055
 
1003
- export { applyExtensions, collectComment, extractExtensionsFromSchema, extractType, isGraphQLDirective, isGraphQLEnum, isGraphQLEnumExtension, isGraphQLInputType, isGraphQLInputTypeExtension, isGraphQLInterface, isGraphQLInterfaceExtension, isGraphQLScalar, isGraphQLScalarExtension, isGraphQLType, isGraphQLTypeExtension, isGraphQLUnion, isGraphQLUnionExtension, isListTypeNode, isNonNullTypeNode, isSchemaDefinition, isSourceTypes, isStringTypes, isWrappingTypeNode, mergeArguments, mergeDirective, mergeDirectives, mergeEnum, mergeEnumValues, mergeExtensions, mergeFields, mergeGraphQLNodes, mergeGraphQLTypes, mergeInputType, mergeInterface, mergeNamedTypeArray, mergeResolvers, mergeScalar, mergeSchemas, mergeSchemasAsync, mergeType, mergeTypeDefs, mergeUnion, printComment, printTypeNode, printWithComments, pushComment, resetComments, travelSchemaPossibleExtensions };
1056
+ export { CompareVal, applyExtensions, collectComment, defaultStringComparator, extractExtensionsFromSchema, extractType, isListTypeNode, isNamedDefinitionNode, isNonNullTypeNode, isSourceTypes, isStringTypes, isWrappingTypeNode, mergeArguments, mergeDirective, mergeDirectives, mergeEnum, mergeEnumValues, mergeExtensions, mergeFields, mergeGraphQLNodes, mergeGraphQLTypes, mergeInputType, mergeInterface, mergeNamedTypeArray, mergeResolvers, mergeScalar, mergeSchemas, mergeSchemasAsync, mergeType, mergeTypeDefs, mergeUnion, printComment, printTypeNode, printWithComments, pushComment, resetComments, schemaDefSymbol, travelSchemaPossibleExtensions };
1004
1057
  //# sourceMappingURL=index.esm.js.map