@graphql-tools/utils 7.3.0 → 7.5.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.
package/es5/index.esm.js CHANGED
@@ -490,15 +490,13 @@ function astFromValueUntyped(value) {
490
490
  */
491
491
  var integerStringRegExp = /^-?(?:0|[1-9][0-9]*)$/;
492
492
 
493
- // this approach uses the default schema printer rather than a custom solution, so may be more backwards compatible
494
- // currently does not allow customization of printSchema options having to do with comments.
495
- function printSchemaWithDirectives(schema, options) {
493
+ function getDocumentNodeFromSchema(schema, options) {
496
494
  var e_1, _a;
497
495
  if (options === void 0) { options = {}; }
498
496
  var pathToDirectivesInExtensions = options.pathToDirectivesInExtensions;
499
497
  var typesMap = schema.getTypeMap();
500
498
  var schemaNode = astFromSchema(schema, pathToDirectivesInExtensions);
501
- var result = schemaNode != null ? [print(schemaNode)] : [];
499
+ var definitions = schemaNode != null ? [schemaNode] : [];
502
500
  for (var typeName in typesMap) {
503
501
  var type = typesMap[typeName];
504
502
  var isPredefinedScalar = isSpecifiedScalarType(type);
@@ -507,22 +505,22 @@ function printSchemaWithDirectives(schema, options) {
507
505
  continue;
508
506
  }
509
507
  if (isObjectType(type)) {
510
- result.push(print(astFromObjectType(type, schema, pathToDirectivesInExtensions)));
508
+ definitions.push(astFromObjectType(type, schema, pathToDirectivesInExtensions));
511
509
  }
512
510
  else if (isInterfaceType(type)) {
513
- result.push(print(astFromInterfaceType(type, schema, pathToDirectivesInExtensions)));
511
+ definitions.push(astFromInterfaceType(type, schema, pathToDirectivesInExtensions));
514
512
  }
515
513
  else if (isUnionType(type)) {
516
- result.push(print(astFromUnionType(type, schema, pathToDirectivesInExtensions)));
514
+ definitions.push(astFromUnionType(type, schema, pathToDirectivesInExtensions));
517
515
  }
518
516
  else if (isInputObjectType(type)) {
519
- result.push(print(astFromInputObjectType(type, schema, pathToDirectivesInExtensions)));
517
+ definitions.push(astFromInputObjectType(type, schema, pathToDirectivesInExtensions));
520
518
  }
521
519
  else if (isEnumType(type)) {
522
- result.push(print(astFromEnumType(type, schema, pathToDirectivesInExtensions)));
520
+ definitions.push(astFromEnumType(type, schema, pathToDirectivesInExtensions));
523
521
  }
524
522
  else if (isScalarType(type)) {
525
- result.push(print(astFromScalarType(type, schema, pathToDirectivesInExtensions)));
523
+ definitions.push(astFromScalarType(type, schema, pathToDirectivesInExtensions));
526
524
  }
527
525
  else {
528
526
  throw new Error("Unknown type " + type + ".");
@@ -535,7 +533,7 @@ function printSchemaWithDirectives(schema, options) {
535
533
  if (isSpecifiedDirective(directive)) {
536
534
  continue;
537
535
  }
538
- result.push(print(astFromDirective(directive, schema, pathToDirectivesInExtensions)));
536
+ definitions.push(astFromDirective(directive, schema, pathToDirectivesInExtensions));
539
537
  }
540
538
  }
541
539
  catch (e_1_1) { e_1 = { error: e_1_1 }; }
@@ -545,7 +543,17 @@ function printSchemaWithDirectives(schema, options) {
545
543
  }
546
544
  finally { if (e_1) throw e_1.error; }
547
545
  }
548
- return result.join('\n');
546
+ return {
547
+ kind: Kind.DOCUMENT,
548
+ definitions: definitions,
549
+ };
550
+ }
551
+ // this approach uses the default schema printer rather than a custom solution, so may be more backwards compatible
552
+ // currently does not allow customization of printSchema options having to do with comments.
553
+ function printSchemaWithDirectives(schema, options) {
554
+ if (options === void 0) { options = {}; }
555
+ var documentNode = getDocumentNodeFromSchema(schema, options);
556
+ return print(documentNode);
549
557
  }
550
558
  function astFromSchema(schema, pathToDirectivesInExtensions) {
551
559
  var _a, _b;
@@ -641,7 +649,7 @@ function getDirectiveNodes(entity, schema, pathToDirectivesInExtensions) {
641
649
  }
642
650
  var directives;
643
651
  if (directivesInExtensions != null) {
644
- directives = makeDirectives(schema, directivesInExtensions);
652
+ directives = makeDirectiveNodes(schema, directivesInExtensions);
645
653
  }
646
654
  else {
647
655
  directives = [].concat.apply([], __spread(nodes.filter(function (node) { return node.directives != null; }).map(function (node) { return node.directives; })));
@@ -655,7 +663,7 @@ function getDeprecatableDirectiveNodes(entity, schema, pathToDirectivesInExtensi
655
663
  var directivesInExtensions = getDirectivesInExtensions(entity, pathToDirectivesInExtensions);
656
664
  var directives;
657
665
  if (directivesInExtensions != null) {
658
- directives = makeDirectives(schema, directivesInExtensions);
666
+ directives = makeDirectiveNodes(schema, directivesInExtensions);
659
667
  }
660
668
  else {
661
669
  directives = (_a = entity.astNode) === null || _a === void 0 ? void 0 : _a.directives;
@@ -791,10 +799,34 @@ function astFromEnumType(type, schema, pathToDirectivesInExtensions) {
791
799
  };
792
800
  }
793
801
  function astFromScalarType(type, schema, pathToDirectivesInExtensions) {
794
- var _a, _b;
802
+ var _a, _b, _c, _d;
803
+ var directiveNodesBesidesSpecifiedBy = [];
804
+ var specifiedByDirectiveNode;
805
+ var directivesInExtensions = getDirectivesInExtensions(type, pathToDirectivesInExtensions);
806
+ var allDirectives;
807
+ if (directivesInExtensions != null) {
808
+ allDirectives = makeDirectiveNodes(schema, directivesInExtensions);
809
+ }
810
+ else {
811
+ allDirectives = (_a = type.astNode) === null || _a === void 0 ? void 0 : _a.directives;
812
+ }
813
+ if (allDirectives != null) {
814
+ directiveNodesBesidesSpecifiedBy = allDirectives.filter(function (directive) { return directive.name.value !== 'specifiedBy'; });
815
+ if (type.specifiedByUrl != null) {
816
+ specifiedByDirectiveNode = (_b = allDirectives.filter(function (directive) { return directive.name.value === 'specifiedBy'; })) === null || _b === void 0 ? void 0 : _b[0];
817
+ }
818
+ }
819
+ if (type.specifiedByUrl != null && specifiedByDirectiveNode == null) {
820
+ specifiedByDirectiveNode = makeDirectiveNode('specifiedBy', {
821
+ url: type.specifiedByUrl,
822
+ });
823
+ }
824
+ var directives = specifiedByDirectiveNode == null
825
+ ? directiveNodesBesidesSpecifiedBy
826
+ : [specifiedByDirectiveNode].concat(directiveNodesBesidesSpecifiedBy);
795
827
  return {
796
828
  kind: Kind.SCALAR_TYPE_DEFINITION,
797
- description: ((_b = (_a = type.astNode) === null || _a === void 0 ? void 0 : _a.description) !== null && _b !== void 0 ? _b : type.description) ? {
829
+ description: ((_d = (_c = type.astNode) === null || _c === void 0 ? void 0 : _c.description) !== null && _d !== void 0 ? _d : type.description) ? {
798
830
  kind: Kind.STRING,
799
831
  value: type.description,
800
832
  block: true,
@@ -804,7 +836,7 @@ function astFromScalarType(type, schema, pathToDirectivesInExtensions) {
804
836
  kind: Kind.NAME,
805
837
  value: type.name,
806
838
  },
807
- directives: getDirectiveNodes(type, schema, pathToDirectivesInExtensions),
839
+ directives: directives,
808
840
  };
809
841
  }
810
842
  function astFromField(field, schema, pathToDirectivesInExtensions) {
@@ -842,6 +874,7 @@ function astFromInputField(field, schema, pathToDirectivesInExtensions) {
842
874
  },
843
875
  type: astFromType(field.type),
844
876
  directives: getDeprecatableDirectiveNodes(field, schema, pathToDirectivesInExtensions),
877
+ defaultValue: astFromValue(field.defaultValue, field.type),
845
878
  };
846
879
  }
847
880
  function astFromEnumValue(value, schema, pathToDirectivesInExtensions) {
@@ -862,9 +895,9 @@ function astFromEnumValue(value, schema, pathToDirectivesInExtensions) {
862
895
  };
863
896
  }
864
897
  function makeDeprecatedDirective(deprecationReason) {
865
- return makeDirective('deprecated', { reason: deprecationReason }, GraphQLDeprecatedDirective);
898
+ return makeDirectiveNode('deprecated', { reason: deprecationReason }, GraphQLDeprecatedDirective);
866
899
  }
867
- function makeDirective(name, args, directive) {
900
+ function makeDirectiveNode(name, args, directive) {
868
901
  var directiveArguments = [];
869
902
  if (directive != null) {
870
903
  directive.args.forEach(function (arg) {
@@ -904,18 +937,18 @@ function makeDirective(name, args, directive) {
904
937
  arguments: directiveArguments,
905
938
  };
906
939
  }
907
- function makeDirectives(schema, directiveValues) {
940
+ function makeDirectiveNodes(schema, directiveValues) {
908
941
  var directiveNodes = [];
909
942
  Object.entries(directiveValues).forEach(function (_a) {
910
943
  var _b = __read(_a, 2), directiveName = _b[0], arrayOrSingleValue = _b[1];
911
944
  var directive = schema.getDirective(directiveName);
912
945
  if (Array.isArray(arrayOrSingleValue)) {
913
946
  arrayOrSingleValue.forEach(function (value) {
914
- directiveNodes.push(makeDirective(directiveName, value, directive));
947
+ directiveNodes.push(makeDirectiveNode(directiveName, value, directive));
915
948
  });
916
949
  }
917
950
  else {
918
- directiveNodes.push(makeDirective(directiveName, arrayOrSingleValue, directive));
951
+ directiveNodes.push(makeDirectiveNode(directiveName, arrayOrSingleValue, directive));
919
952
  }
920
953
  });
921
954
  return directiveNodes;
@@ -4288,8 +4321,8 @@ function isAsyncIterable(value) {
4288
4321
  }
4289
4322
 
4290
4323
  function isDocumentNode(object) {
4291
- return object.kind !== undefined;
4324
+ return object && typeof object === 'object' && 'kind' in object && object.kind === Kind.DOCUMENT;
4292
4325
  }
4293
4326
 
4294
- export { MapperKind, SchemaDirectiveVisitor, SchemaVisitor, VisitSchemaKind, addTypes, appendObjectFields, argsToFieldConfigArgumentMap, argumentToArgumentConfig, asArray, astFromArg, astFromDirective, astFromEnumType, astFromInputObjectType, astFromInterfaceType, astFromObjectType, astFromSchema, astFromUnionType, astFromValueUntyped, buildOperationNodeForField, checkValidationErrors, cloneDirective, cloneSchema, cloneType, collectFields, compareNodes, compareStrings, correctASTNodes, createNamedStub, createSchemaDefinition, createStub, debugLog, fieldToFieldConfig, filterSchema, fixSchemaAst, fixWindowsPath, flattenArray, forEachDefaultValue, forEachField, getArgumentValues, getBuiltInForStub, getDeprecatableDirectiveNodes, getDirectiveNodes, getDirectives, getDirectivesInExtensions, getFieldsWithDirectives, getImplementingTypes, getLeadingCommentBlock, getResolversFromSchema, getResponseKeyFromInfo, getUserTypesFromSchema, healSchema, healTypes, implementsAbstractType, inputFieldToFieldConfig, isAsyncIterable, isDescribable, isDocumentNode, isDocumentString, isEqual, isNamedStub, isNotEqual, isValidPath, makeDeprecatedDirective, makeDirective, makeDirectives, mapAsyncIterator, mapSchema, mergeDeep, modifyObjectFields, nodeToString, observableToAsyncIterable, parseGraphQLJSON, parseGraphQLSDL, parseInputValue, parseInputValueLiteral, parseSelectionSet, printSchemaWithDirectives, pruneSchema, relocatedError, removeObjectFields, renameType, rewireTypes, selectObjectFields, serializeInputValue, transformCommentsToDescriptions, transformInputValue, updateArgument, validateGraphQlDocuments, valueMatchesCriteria, visitData, visitErrors, visitResult, visitSchema };
4327
+ export { MapperKind, SchemaDirectiveVisitor, SchemaVisitor, VisitSchemaKind, addTypes, appendObjectFields, argsToFieldConfigArgumentMap, argumentToArgumentConfig, asArray, astFromArg, astFromDirective, astFromEnumType, astFromEnumValue, astFromField, astFromInputField, astFromInputObjectType, astFromInterfaceType, astFromObjectType, astFromScalarType, astFromSchema, astFromUnionType, astFromValueUntyped, buildOperationNodeForField, checkValidationErrors, cloneDirective, cloneSchema, cloneType, collectFields, compareNodes, compareStrings, correctASTNodes, createNamedStub, createSchemaDefinition, createStub, debugLog, fieldToFieldConfig, filterSchema, fixSchemaAst, fixWindowsPath, flattenArray, forEachDefaultValue, forEachField, getArgumentValues, getBuiltInForStub, getDeprecatableDirectiveNodes, getDirectiveNodes, getDirectives, getDirectivesInExtensions, getDocumentNodeFromSchema, getFieldsWithDirectives, getImplementingTypes, getLeadingCommentBlock, getResolversFromSchema, getResponseKeyFromInfo, getUserTypesFromSchema, healSchema, healTypes, implementsAbstractType, inputFieldToFieldConfig, isAsyncIterable, isDescribable, isDocumentNode, isDocumentString, isEqual, isNamedStub, isNotEqual, isValidPath, makeDeprecatedDirective, makeDirectiveNode, makeDirectiveNodes, mapAsyncIterator, mapSchema, mergeDeep, modifyObjectFields, nodeToString, observableToAsyncIterable, parseGraphQLJSON, parseGraphQLSDL, parseInputValue, parseInputValueLiteral, parseSelectionSet, printSchemaWithDirectives, pruneSchema, relocatedError, removeObjectFields, renameType, rewireTypes, selectObjectFields, serializeInputValue, transformCommentsToDescriptions, transformInputValue, updateArgument, validateGraphQlDocuments, valueMatchesCriteria, visitData, visitErrors, visitResult, visitSchema };
4295
4328
  //# sourceMappingURL=index.esm.js.map