@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.
package/es5/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphql-tools/utils/es5",
3
- "version": "7.2.1",
3
+ "version": "7.2.3",
4
4
  "description": "Common package containting utils and types for GraphQL tools",
5
5
  "sideEffects": false,
6
6
  "peerDependencies": {
package/index.cjs.js CHANGED
@@ -387,6 +387,79 @@ function astFromType(type) {
387
387
  };
388
388
  }
389
389
 
390
+ /**
391
+ * Produces a GraphQL Value AST given a JavaScript object.
392
+ * Function will match JavaScript/JSON values to GraphQL AST schema format
393
+ * by using the following mapping.
394
+ *
395
+ * | JSON Value | GraphQL Value |
396
+ * | ------------- | -------------------- |
397
+ * | Object | Input Object |
398
+ * | Array | List |
399
+ * | Boolean | Boolean |
400
+ * | String | String |
401
+ * | Number | Int / Float |
402
+ * | null | NullValue |
403
+ *
404
+ */
405
+ function astFromValueUntyped(value) {
406
+ // only explicit null, not undefined, NaN
407
+ if (value === null) {
408
+ return { kind: graphql.Kind.NULL };
409
+ }
410
+ // undefined
411
+ if (value === undefined) {
412
+ return null;
413
+ }
414
+ // Convert JavaScript array to GraphQL list. If the GraphQLType is a list, but
415
+ // the value is not an array, convert the value using the list's item type.
416
+ if (Array.isArray(value)) {
417
+ const valuesNodes = [];
418
+ value.forEach(item => {
419
+ const itemNode = astFromValueUntyped(item);
420
+ if (itemNode != null) {
421
+ valuesNodes.push(itemNode);
422
+ }
423
+ });
424
+ return { kind: graphql.Kind.LIST, values: valuesNodes };
425
+ }
426
+ if (typeof value === 'object') {
427
+ const fieldNodes = [];
428
+ Object.entries(value).forEach(([fieldName, fieldValue]) => {
429
+ const ast = astFromValueUntyped(fieldValue);
430
+ if (ast) {
431
+ fieldNodes.push({
432
+ kind: graphql.Kind.OBJECT_FIELD,
433
+ name: { kind: graphql.Kind.NAME, value: fieldName },
434
+ value: ast,
435
+ });
436
+ }
437
+ });
438
+ return { kind: graphql.Kind.OBJECT, fields: fieldNodes };
439
+ }
440
+ // Others serialize based on their corresponding JavaScript scalar types.
441
+ if (typeof value === 'boolean') {
442
+ return { kind: graphql.Kind.BOOLEAN, value };
443
+ }
444
+ // JavaScript numbers can be Int or Float values.
445
+ if (typeof value === 'number' && isFinite(value)) {
446
+ const stringNum = String(value);
447
+ return integerStringRegExp.test(stringNum)
448
+ ? { kind: graphql.Kind.INT, value: stringNum }
449
+ : { kind: graphql.Kind.FLOAT, value: stringNum };
450
+ }
451
+ if (typeof value === 'string') {
452
+ return { kind: graphql.Kind.STRING, value };
453
+ }
454
+ throw new TypeError(`Cannot convert value to AST: ${value}.`);
455
+ }
456
+ /**
457
+ * IntValue:
458
+ * - NegativeSign? 0
459
+ * - NegativeSign? NonZeroDigit ( Digit+ )?
460
+ */
461
+ const integerStringRegExp = /^-?(?:0|[1-9][0-9]*)$/;
462
+
390
463
  // this approach uses the default schema printer rather than a custom solution, so may be more backwards compatible
391
464
  // currently does not allow customization of printSchema options having to do with comments.
392
465
  function printSchemaWithDirectives(schema, options = {}) {
@@ -749,19 +822,34 @@ function makeDeprecatedDirective(deprecationReason) {
749
822
  }
750
823
  function makeDirective(name, args, directive) {
751
824
  const directiveArguments = [];
752
- Object.entries(args).forEach(([argName, argValue]) => {
753
- const directiveArg = directive.args.find(arg => arg.name === argName);
754
- if (directiveArg) {
825
+ if (directive != null) {
826
+ directive.args.forEach(arg => {
827
+ const argName = arg.name;
828
+ const argValue = args[argName];
829
+ if (argValue !== undefined) {
830
+ directiveArguments.push({
831
+ kind: graphql.Kind.ARGUMENT,
832
+ name: {
833
+ kind: graphql.Kind.NAME,
834
+ value: argName,
835
+ },
836
+ value: graphql.astFromValue(argValue, arg.type),
837
+ });
838
+ }
839
+ });
840
+ }
841
+ else {
842
+ Object.entries(args).forEach(([argName, argValue]) => {
755
843
  directiveArguments.push({
756
844
  kind: graphql.Kind.ARGUMENT,
757
845
  name: {
758
846
  kind: graphql.Kind.NAME,
759
847
  value: argName,
760
848
  },
761
- value: graphql.astFromValue(argValue, directiveArg.type),
849
+ value: astFromValueUntyped(argValue),
762
850
  });
763
- }
764
- });
851
+ });
852
+ }
765
853
  return {
766
854
  kind: graphql.Kind.DIRECTIVE,
767
855
  name: {
@@ -775,15 +863,13 @@ function makeDirectives(schema, directiveValues) {
775
863
  const directiveNodes = [];
776
864
  Object.entries(directiveValues).forEach(([directiveName, arrayOrSingleValue]) => {
777
865
  const directive = schema.getDirective(directiveName);
778
- if (directive != null) {
779
- if (Array.isArray(arrayOrSingleValue)) {
780
- arrayOrSingleValue.forEach(value => {
781
- directiveNodes.push(makeDirective(directiveName, value, directive));
782
- });
783
- }
784
- else {
785
- directiveNodes.push(makeDirective(directiveName, arrayOrSingleValue, directive));
786
- }
866
+ if (Array.isArray(arrayOrSingleValue)) {
867
+ arrayOrSingleValue.forEach(value => {
868
+ directiveNodes.push(makeDirective(directiveName, value, directive));
869
+ });
870
+ }
871
+ else {
872
+ directiveNodes.push(makeDirective(directiveName, arrayOrSingleValue, directive));
787
873
  }
788
874
  });
789
875
  return directiveNodes;
@@ -4167,6 +4253,7 @@ exports.appendObjectFields = appendObjectFields;
4167
4253
  exports.argsToFieldConfigArgumentMap = argsToFieldConfigArgumentMap;
4168
4254
  exports.argumentToArgumentConfig = argumentToArgumentConfig;
4169
4255
  exports.asArray = asArray;
4256
+ exports.astFromValueUntyped = astFromValueUntyped;
4170
4257
  exports.buildOperationNodeForField = buildOperationNodeForField;
4171
4258
  exports.checkValidationErrors = checkValidationErrors;
4172
4259
  exports.cloneDirective = cloneDirective;