@graphql-tools/utils 7.2.1 → 7.2.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.
@@ -0,0 +1,17 @@
1
+ import { ValueNode } from 'graphql';
2
+ /**
3
+ * Produces a GraphQL Value AST given a JavaScript object.
4
+ * Function will match JavaScript/JSON values to GraphQL AST schema format
5
+ * by using the following mapping.
6
+ *
7
+ * | JSON Value | GraphQL Value |
8
+ * | ------------- | -------------------- |
9
+ * | Object | Input Object |
10
+ * | Array | List |
11
+ * | Boolean | Boolean |
12
+ * | String | String |
13
+ * | Number | Int / Float |
14
+ * | null | NullValue |
15
+ *
16
+ */
17
+ export declare function astFromValueUntyped(value: any): ValueNode;
@@ -0,0 +1,17 @@
1
+ import { ValueNode } from 'graphql';
2
+ /**
3
+ * Produces a GraphQL Value AST given a JavaScript object.
4
+ * Function will match JavaScript/JSON values to GraphQL AST schema format
5
+ * by using the following mapping.
6
+ *
7
+ * | JSON Value | GraphQL Value |
8
+ * | ------------- | -------------------- |
9
+ * | Object | Input Object |
10
+ * | Array | List |
11
+ * | Boolean | Boolean |
12
+ * | String | String |
13
+ * | Number | Int / Float |
14
+ * | null | NullValue |
15
+ *
16
+ */
17
+ export declare function astFromValueUntyped(value: any): ValueNode;
package/es5/index.cjs.js CHANGED
@@ -437,6 +437,80 @@ function astFromType(type) {
437
437
  };
438
438
  }
439
439
 
440
+ /**
441
+ * Produces a GraphQL Value AST given a JavaScript object.
442
+ * Function will match JavaScript/JSON values to GraphQL AST schema format
443
+ * by using the following mapping.
444
+ *
445
+ * | JSON Value | GraphQL Value |
446
+ * | ------------- | -------------------- |
447
+ * | Object | Input Object |
448
+ * | Array | List |
449
+ * | Boolean | Boolean |
450
+ * | String | String |
451
+ * | Number | Int / Float |
452
+ * | null | NullValue |
453
+ *
454
+ */
455
+ function astFromValueUntyped(value) {
456
+ // only explicit null, not undefined, NaN
457
+ if (value === null) {
458
+ return { kind: graphql.Kind.NULL };
459
+ }
460
+ // undefined
461
+ if (value === undefined) {
462
+ return null;
463
+ }
464
+ // Convert JavaScript array to GraphQL list. If the GraphQLType is a list, but
465
+ // the value is not an array, convert the value using the list's item type.
466
+ if (Array.isArray(value)) {
467
+ var valuesNodes_1 = [];
468
+ value.forEach(function (item) {
469
+ var itemNode = astFromValueUntyped(item);
470
+ if (itemNode != null) {
471
+ valuesNodes_1.push(itemNode);
472
+ }
473
+ });
474
+ return { kind: graphql.Kind.LIST, values: valuesNodes_1 };
475
+ }
476
+ if (typeof value === 'object') {
477
+ var fieldNodes_1 = [];
478
+ Object.entries(value).forEach(function (_a) {
479
+ var _b = tslib.__read(_a, 2), fieldName = _b[0], fieldValue = _b[1];
480
+ var ast = astFromValueUntyped(fieldValue);
481
+ if (ast) {
482
+ fieldNodes_1.push({
483
+ kind: graphql.Kind.OBJECT_FIELD,
484
+ name: { kind: graphql.Kind.NAME, value: fieldName },
485
+ value: ast,
486
+ });
487
+ }
488
+ });
489
+ return { kind: graphql.Kind.OBJECT, fields: fieldNodes_1 };
490
+ }
491
+ // Others serialize based on their corresponding JavaScript scalar types.
492
+ if (typeof value === 'boolean') {
493
+ return { kind: graphql.Kind.BOOLEAN, value: value };
494
+ }
495
+ // JavaScript numbers can be Int or Float values.
496
+ if (typeof value === 'number' && isFinite(value)) {
497
+ var stringNum = String(value);
498
+ return integerStringRegExp.test(stringNum)
499
+ ? { kind: graphql.Kind.INT, value: stringNum }
500
+ : { kind: graphql.Kind.FLOAT, value: stringNum };
501
+ }
502
+ if (typeof value === 'string') {
503
+ return { kind: graphql.Kind.STRING, value: value };
504
+ }
505
+ throw new TypeError("Cannot convert value to AST: " + value + ".");
506
+ }
507
+ /**
508
+ * IntValue:
509
+ * - NegativeSign? 0
510
+ * - NegativeSign? NonZeroDigit ( Digit+ )?
511
+ */
512
+ var integerStringRegExp = /^-?(?:0|[1-9][0-9]*)$/;
513
+
440
514
  // this approach uses the default schema printer rather than a custom solution, so may be more backwards compatible
441
515
  // currently does not allow customization of printSchema options having to do with comments.
442
516
  function printSchemaWithDirectives(schema, options) {
@@ -813,20 +887,35 @@ function makeDeprecatedDirective(deprecationReason) {
813
887
  }
814
888
  function makeDirective(name, args, directive) {
815
889
  var directiveArguments = [];
816
- Object.entries(args).forEach(function (_a) {
817
- var _b = tslib.__read(_a, 2), argName = _b[0], argValue = _b[1];
818
- var directiveArg = directive.args.find(function (arg) { return arg.name === argName; });
819
- if (directiveArg) {
890
+ if (directive != null) {
891
+ directive.args.forEach(function (arg) {
892
+ var argName = arg.name;
893
+ var argValue = args[argName];
894
+ if (argValue !== undefined) {
895
+ directiveArguments.push({
896
+ kind: graphql.Kind.ARGUMENT,
897
+ name: {
898
+ kind: graphql.Kind.NAME,
899
+ value: argName,
900
+ },
901
+ value: graphql.astFromValue(argValue, arg.type),
902
+ });
903
+ }
904
+ });
905
+ }
906
+ else {
907
+ Object.entries(args).forEach(function (_a) {
908
+ var _b = tslib.__read(_a, 2), argName = _b[0], argValue = _b[1];
820
909
  directiveArguments.push({
821
910
  kind: graphql.Kind.ARGUMENT,
822
911
  name: {
823
912
  kind: graphql.Kind.NAME,
824
913
  value: argName,
825
914
  },
826
- value: graphql.astFromValue(argValue, directiveArg.type),
915
+ value: astFromValueUntyped(argValue),
827
916
  });
828
- }
829
- });
917
+ });
918
+ }
830
919
  return {
831
920
  kind: graphql.Kind.DIRECTIVE,
832
921
  name: {
@@ -841,15 +930,13 @@ function makeDirectives(schema, directiveValues) {
841
930
  Object.entries(directiveValues).forEach(function (_a) {
842
931
  var _b = tslib.__read(_a, 2), directiveName = _b[0], arrayOrSingleValue = _b[1];
843
932
  var directive = schema.getDirective(directiveName);
844
- if (directive != null) {
845
- if (Array.isArray(arrayOrSingleValue)) {
846
- arrayOrSingleValue.forEach(function (value) {
847
- directiveNodes.push(makeDirective(directiveName, value, directive));
848
- });
849
- }
850
- else {
851
- directiveNodes.push(makeDirective(directiveName, arrayOrSingleValue, directive));
852
- }
933
+ if (Array.isArray(arrayOrSingleValue)) {
934
+ arrayOrSingleValue.forEach(function (value) {
935
+ directiveNodes.push(makeDirective(directiveName, value, directive));
936
+ });
937
+ }
938
+ else {
939
+ directiveNodes.push(makeDirective(directiveName, arrayOrSingleValue, directive));
853
940
  }
854
941
  });
855
942
  return directiveNodes;
@@ -4220,6 +4307,7 @@ exports.appendObjectFields = appendObjectFields;
4220
4307
  exports.argsToFieldConfigArgumentMap = argsToFieldConfigArgumentMap;
4221
4308
  exports.argumentToArgumentConfig = argumentToArgumentConfig;
4222
4309
  exports.asArray = asArray;
4310
+ exports.astFromValueUntyped = astFromValueUntyped;
4223
4311
  exports.buildOperationNodeForField = buildOperationNodeForField;
4224
4312
  exports.checkValidationErrors = checkValidationErrors;
4225
4313
  exports.cloneDirective = cloneDirective;