@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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphql-tools/utils/es5",
3
- "version": "7.3.0",
3
+ "version": "7.5.0",
4
4
  "description": "Common package containing utils and types for GraphQL tools",
5
5
  "sideEffects": false,
6
6
  "peerDependencies": {
@@ -1,5 +1,6 @@
1
- import { GraphQLSchema, GraphQLNamedType, DirectiveNode, InputValueDefinitionNode, GraphQLArgument, GraphQLDirective, DirectiveDefinitionNode, SchemaDefinitionNode, SchemaExtensionNode, GraphQLObjectType, ObjectTypeDefinitionNode, GraphQLField, GraphQLInterfaceType, InterfaceTypeDefinitionNode, UnionTypeDefinitionNode, GraphQLUnionType, GraphQLInputObjectType, InputObjectTypeDefinitionNode, GraphQLInputField, GraphQLEnumType, GraphQLEnumValue, EnumTypeDefinitionNode } from 'graphql';
2
- import { PrintSchemaWithDirectivesOptions } from './types';
1
+ import { GraphQLSchema, GraphQLNamedType, DirectiveNode, FieldDefinitionNode, InputValueDefinitionNode, GraphQLArgument, EnumValueDefinitionNode, GraphQLDirective, DirectiveDefinitionNode, SchemaDefinitionNode, SchemaExtensionNode, GraphQLObjectType, ObjectTypeDefinitionNode, GraphQLField, GraphQLInterfaceType, InterfaceTypeDefinitionNode, UnionTypeDefinitionNode, GraphQLUnionType, GraphQLInputObjectType, InputObjectTypeDefinitionNode, GraphQLInputField, GraphQLEnumType, GraphQLEnumValue, EnumTypeDefinitionNode, GraphQLScalarType, ScalarTypeDefinitionNode, DocumentNode } from 'graphql';
2
+ import { GetDocumentNodeFromSchemaOptions, PrintSchemaWithDirectivesOptions } from './types';
3
+ export declare function getDocumentNodeFromSchema(schema: GraphQLSchema, options?: GetDocumentNodeFromSchemaOptions): DocumentNode;
3
4
  export declare function printSchemaWithDirectives(schema: GraphQLSchema, options?: PrintSchemaWithDirectivesOptions): string;
4
5
  export declare function astFromSchema(schema: GraphQLSchema, pathToDirectivesInExtensions: Array<string>): SchemaDefinitionNode | SchemaExtensionNode;
5
6
  export declare function astFromDirective(directive: GraphQLDirective, schema: GraphQLSchema, pathToDirectivesInExtensions: Array<string>): DirectiveDefinitionNode;
@@ -11,6 +12,10 @@ export declare function astFromInterfaceType(type: GraphQLInterfaceType, schema:
11
12
  export declare function astFromUnionType(type: GraphQLUnionType, schema: GraphQLSchema, pathToDirectivesInExtensions: Array<string>): UnionTypeDefinitionNode;
12
13
  export declare function astFromInputObjectType(type: GraphQLInputObjectType, schema: GraphQLSchema, pathToDirectivesInExtensions: Array<string>): InputObjectTypeDefinitionNode;
13
14
  export declare function astFromEnumType(type: GraphQLEnumType, schema: GraphQLSchema, pathToDirectivesInExtensions: Array<string>): EnumTypeDefinitionNode;
15
+ export declare function astFromScalarType(type: GraphQLScalarType, schema: GraphQLSchema, pathToDirectivesInExtensions: Array<string>): ScalarTypeDefinitionNode;
16
+ export declare function astFromField(field: GraphQLField<any, any>, schema: GraphQLSchema, pathToDirectivesInExtensions: Array<string>): FieldDefinitionNode;
17
+ export declare function astFromInputField(field: GraphQLInputField, schema: GraphQLSchema, pathToDirectivesInExtensions: Array<string>): InputValueDefinitionNode;
18
+ export declare function astFromEnumValue(value: GraphQLEnumValue, schema: GraphQLSchema, pathToDirectivesInExtensions: Array<string>): EnumValueDefinitionNode;
14
19
  export declare function makeDeprecatedDirective(deprecationReason: string): DirectiveNode;
15
- export declare function makeDirective(name: string, args: Record<string, any>, directive: GraphQLDirective): DirectiveNode;
16
- export declare function makeDirectives(schema: GraphQLSchema, directiveValues: Record<string, any>): Array<DirectiveNode>;
20
+ export declare function makeDirectiveNode(name: string, args: Record<string, any>, directive?: GraphQLDirective): DirectiveNode;
21
+ export declare function makeDirectiveNodes(schema: GraphQLSchema, directiveValues: Record<string, any>): Array<DirectiveNode>;
package/es5/types.d.ts CHANGED
@@ -10,9 +10,10 @@ export interface SchemaPrintOptions {
10
10
  */
11
11
  commentDescriptions?: boolean;
12
12
  }
13
- export interface PrintSchemaWithDirectivesOptions extends SchemaPrintOptions {
13
+ export interface GetDocumentNodeFromSchemaOptions {
14
14
  pathToDirectivesInExtensions?: Array<string>;
15
15
  }
16
+ export declare type PrintSchemaWithDirectivesOptions = SchemaPrintOptions & GetDocumentNodeFromSchemaOptions;
16
17
  export declare type Maybe<T> = null | undefined | T;
17
18
  export declare type Constructor<T> = new (...args: any[]) => T;
18
19
  /**
package/index.cjs.js CHANGED
@@ -445,13 +445,11 @@ function astFromValueUntyped(value) {
445
445
  */
446
446
  const integerStringRegExp = /^-?(?:0|[1-9][0-9]*)$/;
447
447
 
448
- // this approach uses the default schema printer rather than a custom solution, so may be more backwards compatible
449
- // currently does not allow customization of printSchema options having to do with comments.
450
- function printSchemaWithDirectives(schema, options = {}) {
448
+ function getDocumentNodeFromSchema(schema, options = {}) {
451
449
  const pathToDirectivesInExtensions = options.pathToDirectivesInExtensions;
452
450
  const typesMap = schema.getTypeMap();
453
451
  const schemaNode = astFromSchema(schema, pathToDirectivesInExtensions);
454
- const result = schemaNode != null ? [graphql.print(schemaNode)] : [];
452
+ const definitions = schemaNode != null ? [schemaNode] : [];
455
453
  for (const typeName in typesMap) {
456
454
  const type = typesMap[typeName];
457
455
  const isPredefinedScalar = graphql.isSpecifiedScalarType(type);
@@ -460,22 +458,22 @@ function printSchemaWithDirectives(schema, options = {}) {
460
458
  continue;
461
459
  }
462
460
  if (graphql.isObjectType(type)) {
463
- result.push(graphql.print(astFromObjectType(type, schema, pathToDirectivesInExtensions)));
461
+ definitions.push(astFromObjectType(type, schema, pathToDirectivesInExtensions));
464
462
  }
465
463
  else if (graphql.isInterfaceType(type)) {
466
- result.push(graphql.print(astFromInterfaceType(type, schema, pathToDirectivesInExtensions)));
464
+ definitions.push(astFromInterfaceType(type, schema, pathToDirectivesInExtensions));
467
465
  }
468
466
  else if (graphql.isUnionType(type)) {
469
- result.push(graphql.print(astFromUnionType(type, schema, pathToDirectivesInExtensions)));
467
+ definitions.push(astFromUnionType(type, schema, pathToDirectivesInExtensions));
470
468
  }
471
469
  else if (graphql.isInputObjectType(type)) {
472
- result.push(graphql.print(astFromInputObjectType(type, schema, pathToDirectivesInExtensions)));
470
+ definitions.push(astFromInputObjectType(type, schema, pathToDirectivesInExtensions));
473
471
  }
474
472
  else if (graphql.isEnumType(type)) {
475
- result.push(graphql.print(astFromEnumType(type, schema, pathToDirectivesInExtensions)));
473
+ definitions.push(astFromEnumType(type, schema, pathToDirectivesInExtensions));
476
474
  }
477
475
  else if (graphql.isScalarType(type)) {
478
- result.push(graphql.print(astFromScalarType(type, schema, pathToDirectivesInExtensions)));
476
+ definitions.push(astFromScalarType(type, schema, pathToDirectivesInExtensions));
479
477
  }
480
478
  else {
481
479
  throw new Error(`Unknown type ${type}.`);
@@ -486,9 +484,18 @@ function printSchemaWithDirectives(schema, options = {}) {
486
484
  if (graphql.isSpecifiedDirective(directive)) {
487
485
  continue;
488
486
  }
489
- result.push(graphql.print(astFromDirective(directive, schema, pathToDirectivesInExtensions)));
487
+ definitions.push(astFromDirective(directive, schema, pathToDirectivesInExtensions));
490
488
  }
491
- return result.join('\n');
489
+ return {
490
+ kind: graphql.Kind.DOCUMENT,
491
+ definitions,
492
+ };
493
+ }
494
+ // this approach uses the default schema printer rather than a custom solution, so may be more backwards compatible
495
+ // currently does not allow customization of printSchema options having to do with comments.
496
+ function printSchemaWithDirectives(schema, options = {}) {
497
+ const documentNode = getDocumentNodeFromSchema(schema, options);
498
+ return graphql.print(documentNode);
492
499
  }
493
500
  function astFromSchema(schema, pathToDirectivesInExtensions) {
494
501
  var _a, _b;
@@ -584,7 +591,7 @@ function getDirectiveNodes(entity, schema, pathToDirectivesInExtensions) {
584
591
  }
585
592
  let directives;
586
593
  if (directivesInExtensions != null) {
587
- directives = makeDirectives(schema, directivesInExtensions);
594
+ directives = makeDirectiveNodes(schema, directivesInExtensions);
588
595
  }
589
596
  else {
590
597
  directives = [].concat(...nodes.filter(node => node.directives != null).map(node => node.directives));
@@ -598,7 +605,7 @@ function getDeprecatableDirectiveNodes(entity, schema, pathToDirectivesInExtensi
598
605
  const directivesInExtensions = getDirectivesInExtensions(entity, pathToDirectivesInExtensions);
599
606
  let directives;
600
607
  if (directivesInExtensions != null) {
601
- directives = makeDirectives(schema, directivesInExtensions);
608
+ directives = makeDirectiveNodes(schema, directivesInExtensions);
602
609
  }
603
610
  else {
604
611
  directives = (_a = entity.astNode) === null || _a === void 0 ? void 0 : _a.directives;
@@ -732,10 +739,34 @@ function astFromEnumType(type, schema, pathToDirectivesInExtensions) {
732
739
  };
733
740
  }
734
741
  function astFromScalarType(type, schema, pathToDirectivesInExtensions) {
735
- var _a, _b;
742
+ var _a, _b, _c, _d;
743
+ let directiveNodesBesidesSpecifiedBy = [];
744
+ let specifiedByDirectiveNode;
745
+ const directivesInExtensions = getDirectivesInExtensions(type, pathToDirectivesInExtensions);
746
+ let allDirectives;
747
+ if (directivesInExtensions != null) {
748
+ allDirectives = makeDirectiveNodes(schema, directivesInExtensions);
749
+ }
750
+ else {
751
+ allDirectives = (_a = type.astNode) === null || _a === void 0 ? void 0 : _a.directives;
752
+ }
753
+ if (allDirectives != null) {
754
+ directiveNodesBesidesSpecifiedBy = allDirectives.filter(directive => directive.name.value !== 'specifiedBy');
755
+ if (type.specifiedByUrl != null) {
756
+ specifiedByDirectiveNode = (_b = allDirectives.filter(directive => directive.name.value === 'specifiedBy')) === null || _b === void 0 ? void 0 : _b[0];
757
+ }
758
+ }
759
+ if (type.specifiedByUrl != null && specifiedByDirectiveNode == null) {
760
+ specifiedByDirectiveNode = makeDirectiveNode('specifiedBy', {
761
+ url: type.specifiedByUrl,
762
+ });
763
+ }
764
+ const directives = specifiedByDirectiveNode == null
765
+ ? directiveNodesBesidesSpecifiedBy
766
+ : [specifiedByDirectiveNode].concat(directiveNodesBesidesSpecifiedBy);
736
767
  return {
737
768
  kind: graphql.Kind.SCALAR_TYPE_DEFINITION,
738
- description: ((_b = (_a = type.astNode) === null || _a === void 0 ? void 0 : _a.description) !== null && _b !== void 0 ? _b : type.description) ? {
769
+ description: ((_d = (_c = type.astNode) === null || _c === void 0 ? void 0 : _c.description) !== null && _d !== void 0 ? _d : type.description) ? {
739
770
  kind: graphql.Kind.STRING,
740
771
  value: type.description,
741
772
  block: true,
@@ -745,7 +776,7 @@ function astFromScalarType(type, schema, pathToDirectivesInExtensions) {
745
776
  kind: graphql.Kind.NAME,
746
777
  value: type.name,
747
778
  },
748
- directives: getDirectiveNodes(type, schema, pathToDirectivesInExtensions),
779
+ directives,
749
780
  };
750
781
  }
751
782
  function astFromField(field, schema, pathToDirectivesInExtensions) {
@@ -783,6 +814,7 @@ function astFromInputField(field, schema, pathToDirectivesInExtensions) {
783
814
  },
784
815
  type: astFromType(field.type),
785
816
  directives: getDeprecatableDirectiveNodes(field, schema, pathToDirectivesInExtensions),
817
+ defaultValue: graphql.astFromValue(field.defaultValue, field.type),
786
818
  };
787
819
  }
788
820
  function astFromEnumValue(value, schema, pathToDirectivesInExtensions) {
@@ -803,9 +835,9 @@ function astFromEnumValue(value, schema, pathToDirectivesInExtensions) {
803
835
  };
804
836
  }
805
837
  function makeDeprecatedDirective(deprecationReason) {
806
- return makeDirective('deprecated', { reason: deprecationReason }, graphql.GraphQLDeprecatedDirective);
838
+ return makeDirectiveNode('deprecated', { reason: deprecationReason }, graphql.GraphQLDeprecatedDirective);
807
839
  }
808
- function makeDirective(name, args, directive) {
840
+ function makeDirectiveNode(name, args, directive) {
809
841
  const directiveArguments = [];
810
842
  if (directive != null) {
811
843
  directive.args.forEach(arg => {
@@ -844,17 +876,17 @@ function makeDirective(name, args, directive) {
844
876
  arguments: directiveArguments,
845
877
  };
846
878
  }
847
- function makeDirectives(schema, directiveValues) {
879
+ function makeDirectiveNodes(schema, directiveValues) {
848
880
  const directiveNodes = [];
849
881
  Object.entries(directiveValues).forEach(([directiveName, arrayOrSingleValue]) => {
850
882
  const directive = schema.getDirective(directiveName);
851
883
  if (Array.isArray(arrayOrSingleValue)) {
852
884
  arrayOrSingleValue.forEach(value => {
853
- directiveNodes.push(makeDirective(directiveName, value, directive));
885
+ directiveNodes.push(makeDirectiveNode(directiveName, value, directive));
854
886
  });
855
887
  }
856
888
  else {
857
- directiveNodes.push(makeDirective(directiveName, arrayOrSingleValue, directive));
889
+ directiveNodes.push(makeDirectiveNode(directiveName, arrayOrSingleValue, directive));
858
890
  }
859
891
  });
860
892
  return directiveNodes;
@@ -4235,7 +4267,7 @@ function isAsyncIterable(value) {
4235
4267
  }
4236
4268
 
4237
4269
  function isDocumentNode(object) {
4238
- return object.kind !== undefined;
4270
+ return object && typeof object === 'object' && 'kind' in object && object.kind === graphql.Kind.DOCUMENT;
4239
4271
  }
4240
4272
 
4241
4273
  exports.SchemaDirectiveVisitor = SchemaDirectiveVisitor;
@@ -4248,9 +4280,13 @@ exports.asArray = asArray;
4248
4280
  exports.astFromArg = astFromArg;
4249
4281
  exports.astFromDirective = astFromDirective;
4250
4282
  exports.astFromEnumType = astFromEnumType;
4283
+ exports.astFromEnumValue = astFromEnumValue;
4284
+ exports.astFromField = astFromField;
4285
+ exports.astFromInputField = astFromInputField;
4251
4286
  exports.astFromInputObjectType = astFromInputObjectType;
4252
4287
  exports.astFromInterfaceType = astFromInterfaceType;
4253
4288
  exports.astFromObjectType = astFromObjectType;
4289
+ exports.astFromScalarType = astFromScalarType;
4254
4290
  exports.astFromSchema = astFromSchema;
4255
4291
  exports.astFromUnionType = astFromUnionType;
4256
4292
  exports.astFromValueUntyped = astFromValueUntyped;
@@ -4280,6 +4316,7 @@ exports.getDeprecatableDirectiveNodes = getDeprecatableDirectiveNodes;
4280
4316
  exports.getDirectiveNodes = getDirectiveNodes;
4281
4317
  exports.getDirectives = getDirectives;
4282
4318
  exports.getDirectivesInExtensions = getDirectivesInExtensions;
4319
+ exports.getDocumentNodeFromSchema = getDocumentNodeFromSchema;
4283
4320
  exports.getFieldsWithDirectives = getFieldsWithDirectives;
4284
4321
  exports.getImplementingTypes = getImplementingTypes;
4285
4322
  exports.getLeadingCommentBlock = getLeadingCommentBlock;
@@ -4299,8 +4336,8 @@ exports.isNamedStub = isNamedStub;
4299
4336
  exports.isNotEqual = isNotEqual;
4300
4337
  exports.isValidPath = isValidPath;
4301
4338
  exports.makeDeprecatedDirective = makeDeprecatedDirective;
4302
- exports.makeDirective = makeDirective;
4303
- exports.makeDirectives = makeDirectives;
4339
+ exports.makeDirectiveNode = makeDirectiveNode;
4340
+ exports.makeDirectiveNodes = makeDirectiveNodes;
4304
4341
  exports.mapAsyncIterator = mapAsyncIterator;
4305
4342
  exports.mapSchema = mapSchema;
4306
4343
  exports.mergeDeep = mergeDeep;