@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/index.esm.js CHANGED
@@ -439,13 +439,11 @@ function astFromValueUntyped(value) {
439
439
  */
440
440
  const integerStringRegExp = /^-?(?:0|[1-9][0-9]*)$/;
441
441
 
442
- // this approach uses the default schema printer rather than a custom solution, so may be more backwards compatible
443
- // currently does not allow customization of printSchema options having to do with comments.
444
- function printSchemaWithDirectives(schema, options = {}) {
442
+ function getDocumentNodeFromSchema(schema, options = {}) {
445
443
  const pathToDirectivesInExtensions = options.pathToDirectivesInExtensions;
446
444
  const typesMap = schema.getTypeMap();
447
445
  const schemaNode = astFromSchema(schema, pathToDirectivesInExtensions);
448
- const result = schemaNode != null ? [print(schemaNode)] : [];
446
+ const definitions = schemaNode != null ? [schemaNode] : [];
449
447
  for (const typeName in typesMap) {
450
448
  const type = typesMap[typeName];
451
449
  const isPredefinedScalar = isSpecifiedScalarType(type);
@@ -454,22 +452,22 @@ function printSchemaWithDirectives(schema, options = {}) {
454
452
  continue;
455
453
  }
456
454
  if (isObjectType(type)) {
457
- result.push(print(astFromObjectType(type, schema, pathToDirectivesInExtensions)));
455
+ definitions.push(astFromObjectType(type, schema, pathToDirectivesInExtensions));
458
456
  }
459
457
  else if (isInterfaceType(type)) {
460
- result.push(print(astFromInterfaceType(type, schema, pathToDirectivesInExtensions)));
458
+ definitions.push(astFromInterfaceType(type, schema, pathToDirectivesInExtensions));
461
459
  }
462
460
  else if (isUnionType(type)) {
463
- result.push(print(astFromUnionType(type, schema, pathToDirectivesInExtensions)));
461
+ definitions.push(astFromUnionType(type, schema, pathToDirectivesInExtensions));
464
462
  }
465
463
  else if (isInputObjectType(type)) {
466
- result.push(print(astFromInputObjectType(type, schema, pathToDirectivesInExtensions)));
464
+ definitions.push(astFromInputObjectType(type, schema, pathToDirectivesInExtensions));
467
465
  }
468
466
  else if (isEnumType(type)) {
469
- result.push(print(astFromEnumType(type, schema, pathToDirectivesInExtensions)));
467
+ definitions.push(astFromEnumType(type, schema, pathToDirectivesInExtensions));
470
468
  }
471
469
  else if (isScalarType(type)) {
472
- result.push(print(astFromScalarType(type, schema, pathToDirectivesInExtensions)));
470
+ definitions.push(astFromScalarType(type, schema, pathToDirectivesInExtensions));
473
471
  }
474
472
  else {
475
473
  throw new Error(`Unknown type ${type}.`);
@@ -480,9 +478,18 @@ function printSchemaWithDirectives(schema, options = {}) {
480
478
  if (isSpecifiedDirective(directive)) {
481
479
  continue;
482
480
  }
483
- result.push(print(astFromDirective(directive, schema, pathToDirectivesInExtensions)));
481
+ definitions.push(astFromDirective(directive, schema, pathToDirectivesInExtensions));
484
482
  }
485
- return result.join('\n');
483
+ return {
484
+ kind: Kind.DOCUMENT,
485
+ definitions,
486
+ };
487
+ }
488
+ // this approach uses the default schema printer rather than a custom solution, so may be more backwards compatible
489
+ // currently does not allow customization of printSchema options having to do with comments.
490
+ function printSchemaWithDirectives(schema, options = {}) {
491
+ const documentNode = getDocumentNodeFromSchema(schema, options);
492
+ return print(documentNode);
486
493
  }
487
494
  function astFromSchema(schema, pathToDirectivesInExtensions) {
488
495
  var _a, _b;
@@ -578,7 +585,7 @@ function getDirectiveNodes(entity, schema, pathToDirectivesInExtensions) {
578
585
  }
579
586
  let directives;
580
587
  if (directivesInExtensions != null) {
581
- directives = makeDirectives(schema, directivesInExtensions);
588
+ directives = makeDirectiveNodes(schema, directivesInExtensions);
582
589
  }
583
590
  else {
584
591
  directives = [].concat(...nodes.filter(node => node.directives != null).map(node => node.directives));
@@ -592,7 +599,7 @@ function getDeprecatableDirectiveNodes(entity, schema, pathToDirectivesInExtensi
592
599
  const directivesInExtensions = getDirectivesInExtensions(entity, pathToDirectivesInExtensions);
593
600
  let directives;
594
601
  if (directivesInExtensions != null) {
595
- directives = makeDirectives(schema, directivesInExtensions);
602
+ directives = makeDirectiveNodes(schema, directivesInExtensions);
596
603
  }
597
604
  else {
598
605
  directives = (_a = entity.astNode) === null || _a === void 0 ? void 0 : _a.directives;
@@ -726,10 +733,34 @@ function astFromEnumType(type, schema, pathToDirectivesInExtensions) {
726
733
  };
727
734
  }
728
735
  function astFromScalarType(type, schema, pathToDirectivesInExtensions) {
729
- var _a, _b;
736
+ var _a, _b, _c, _d;
737
+ let directiveNodesBesidesSpecifiedBy = [];
738
+ let specifiedByDirectiveNode;
739
+ const directivesInExtensions = getDirectivesInExtensions(type, pathToDirectivesInExtensions);
740
+ let allDirectives;
741
+ if (directivesInExtensions != null) {
742
+ allDirectives = makeDirectiveNodes(schema, directivesInExtensions);
743
+ }
744
+ else {
745
+ allDirectives = (_a = type.astNode) === null || _a === void 0 ? void 0 : _a.directives;
746
+ }
747
+ if (allDirectives != null) {
748
+ directiveNodesBesidesSpecifiedBy = allDirectives.filter(directive => directive.name.value !== 'specifiedBy');
749
+ if (type.specifiedByUrl != null) {
750
+ specifiedByDirectiveNode = (_b = allDirectives.filter(directive => directive.name.value === 'specifiedBy')) === null || _b === void 0 ? void 0 : _b[0];
751
+ }
752
+ }
753
+ if (type.specifiedByUrl != null && specifiedByDirectiveNode == null) {
754
+ specifiedByDirectiveNode = makeDirectiveNode('specifiedBy', {
755
+ url: type.specifiedByUrl,
756
+ });
757
+ }
758
+ const directives = specifiedByDirectiveNode == null
759
+ ? directiveNodesBesidesSpecifiedBy
760
+ : [specifiedByDirectiveNode].concat(directiveNodesBesidesSpecifiedBy);
730
761
  return {
731
762
  kind: Kind.SCALAR_TYPE_DEFINITION,
732
- description: ((_b = (_a = type.astNode) === null || _a === void 0 ? void 0 : _a.description) !== null && _b !== void 0 ? _b : type.description) ? {
763
+ description: ((_d = (_c = type.astNode) === null || _c === void 0 ? void 0 : _c.description) !== null && _d !== void 0 ? _d : type.description) ? {
733
764
  kind: Kind.STRING,
734
765
  value: type.description,
735
766
  block: true,
@@ -739,7 +770,7 @@ function astFromScalarType(type, schema, pathToDirectivesInExtensions) {
739
770
  kind: Kind.NAME,
740
771
  value: type.name,
741
772
  },
742
- directives: getDirectiveNodes(type, schema, pathToDirectivesInExtensions),
773
+ directives,
743
774
  };
744
775
  }
745
776
  function astFromField(field, schema, pathToDirectivesInExtensions) {
@@ -777,6 +808,7 @@ function astFromInputField(field, schema, pathToDirectivesInExtensions) {
777
808
  },
778
809
  type: astFromType(field.type),
779
810
  directives: getDeprecatableDirectiveNodes(field, schema, pathToDirectivesInExtensions),
811
+ defaultValue: astFromValue(field.defaultValue, field.type),
780
812
  };
781
813
  }
782
814
  function astFromEnumValue(value, schema, pathToDirectivesInExtensions) {
@@ -797,9 +829,9 @@ function astFromEnumValue(value, schema, pathToDirectivesInExtensions) {
797
829
  };
798
830
  }
799
831
  function makeDeprecatedDirective(deprecationReason) {
800
- return makeDirective('deprecated', { reason: deprecationReason }, GraphQLDeprecatedDirective);
832
+ return makeDirectiveNode('deprecated', { reason: deprecationReason }, GraphQLDeprecatedDirective);
801
833
  }
802
- function makeDirective(name, args, directive) {
834
+ function makeDirectiveNode(name, args, directive) {
803
835
  const directiveArguments = [];
804
836
  if (directive != null) {
805
837
  directive.args.forEach(arg => {
@@ -838,17 +870,17 @@ function makeDirective(name, args, directive) {
838
870
  arguments: directiveArguments,
839
871
  };
840
872
  }
841
- function makeDirectives(schema, directiveValues) {
873
+ function makeDirectiveNodes(schema, directiveValues) {
842
874
  const directiveNodes = [];
843
875
  Object.entries(directiveValues).forEach(([directiveName, arrayOrSingleValue]) => {
844
876
  const directive = schema.getDirective(directiveName);
845
877
  if (Array.isArray(arrayOrSingleValue)) {
846
878
  arrayOrSingleValue.forEach(value => {
847
- directiveNodes.push(makeDirective(directiveName, value, directive));
879
+ directiveNodes.push(makeDirectiveNode(directiveName, value, directive));
848
880
  });
849
881
  }
850
882
  else {
851
- directiveNodes.push(makeDirective(directiveName, arrayOrSingleValue, directive));
883
+ directiveNodes.push(makeDirectiveNode(directiveName, arrayOrSingleValue, directive));
852
884
  }
853
885
  });
854
886
  return directiveNodes;
@@ -4231,8 +4263,8 @@ function isAsyncIterable(value) {
4231
4263
  }
4232
4264
 
4233
4265
  function isDocumentNode(object) {
4234
- return object.kind !== undefined;
4266
+ return object && typeof object === 'object' && 'kind' in object && object.kind === Kind.DOCUMENT;
4235
4267
  }
4236
4268
 
4237
- 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 };
4269
+ 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 };
4238
4270
  //# sourceMappingURL=index.esm.js.map