@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/index.d.ts CHANGED
@@ -49,3 +49,4 @@ export * from './getArgumentValues';
49
49
  export * from './valueMatchesCriteria';
50
50
  export * from './isAsyncIterable';
51
51
  export * from './isDocumentNode';
52
+ export * from './astFromValueUntyped';
package/index.esm.js CHANGED
@@ -381,6 +381,79 @@ function astFromType(type) {
381
381
  };
382
382
  }
383
383
 
384
+ /**
385
+ * Produces a GraphQL Value AST given a JavaScript object.
386
+ * Function will match JavaScript/JSON values to GraphQL AST schema format
387
+ * by using the following mapping.
388
+ *
389
+ * | JSON Value | GraphQL Value |
390
+ * | ------------- | -------------------- |
391
+ * | Object | Input Object |
392
+ * | Array | List |
393
+ * | Boolean | Boolean |
394
+ * | String | String |
395
+ * | Number | Int / Float |
396
+ * | null | NullValue |
397
+ *
398
+ */
399
+ function astFromValueUntyped(value) {
400
+ // only explicit null, not undefined, NaN
401
+ if (value === null) {
402
+ return { kind: Kind.NULL };
403
+ }
404
+ // undefined
405
+ if (value === undefined) {
406
+ return null;
407
+ }
408
+ // Convert JavaScript array to GraphQL list. If the GraphQLType is a list, but
409
+ // the value is not an array, convert the value using the list's item type.
410
+ if (Array.isArray(value)) {
411
+ const valuesNodes = [];
412
+ value.forEach(item => {
413
+ const itemNode = astFromValueUntyped(item);
414
+ if (itemNode != null) {
415
+ valuesNodes.push(itemNode);
416
+ }
417
+ });
418
+ return { kind: Kind.LIST, values: valuesNodes };
419
+ }
420
+ if (typeof value === 'object') {
421
+ const fieldNodes = [];
422
+ Object.entries(value).forEach(([fieldName, fieldValue]) => {
423
+ const ast = astFromValueUntyped(fieldValue);
424
+ if (ast) {
425
+ fieldNodes.push({
426
+ kind: Kind.OBJECT_FIELD,
427
+ name: { kind: Kind.NAME, value: fieldName },
428
+ value: ast,
429
+ });
430
+ }
431
+ });
432
+ return { kind: Kind.OBJECT, fields: fieldNodes };
433
+ }
434
+ // Others serialize based on their corresponding JavaScript scalar types.
435
+ if (typeof value === 'boolean') {
436
+ return { kind: Kind.BOOLEAN, value };
437
+ }
438
+ // JavaScript numbers can be Int or Float values.
439
+ if (typeof value === 'number' && isFinite(value)) {
440
+ const stringNum = String(value);
441
+ return integerStringRegExp.test(stringNum)
442
+ ? { kind: Kind.INT, value: stringNum }
443
+ : { kind: Kind.FLOAT, value: stringNum };
444
+ }
445
+ if (typeof value === 'string') {
446
+ return { kind: Kind.STRING, value };
447
+ }
448
+ throw new TypeError(`Cannot convert value to AST: ${value}.`);
449
+ }
450
+ /**
451
+ * IntValue:
452
+ * - NegativeSign? 0
453
+ * - NegativeSign? NonZeroDigit ( Digit+ )?
454
+ */
455
+ const integerStringRegExp = /^-?(?:0|[1-9][0-9]*)$/;
456
+
384
457
  // this approach uses the default schema printer rather than a custom solution, so may be more backwards compatible
385
458
  // currently does not allow customization of printSchema options having to do with comments.
386
459
  function printSchemaWithDirectives(schema, options = {}) {
@@ -743,19 +816,34 @@ function makeDeprecatedDirective(deprecationReason) {
743
816
  }
744
817
  function makeDirective(name, args, directive) {
745
818
  const directiveArguments = [];
746
- Object.entries(args).forEach(([argName, argValue]) => {
747
- const directiveArg = directive.args.find(arg => arg.name === argName);
748
- if (directiveArg) {
819
+ if (directive != null) {
820
+ directive.args.forEach(arg => {
821
+ const argName = arg.name;
822
+ const argValue = args[argName];
823
+ if (argValue !== undefined) {
824
+ directiveArguments.push({
825
+ kind: Kind.ARGUMENT,
826
+ name: {
827
+ kind: Kind.NAME,
828
+ value: argName,
829
+ },
830
+ value: astFromValue(argValue, arg.type),
831
+ });
832
+ }
833
+ });
834
+ }
835
+ else {
836
+ Object.entries(args).forEach(([argName, argValue]) => {
749
837
  directiveArguments.push({
750
838
  kind: Kind.ARGUMENT,
751
839
  name: {
752
840
  kind: Kind.NAME,
753
841
  value: argName,
754
842
  },
755
- value: astFromValue(argValue, directiveArg.type),
843
+ value: astFromValueUntyped(argValue),
756
844
  });
757
- }
758
- });
845
+ });
846
+ }
759
847
  return {
760
848
  kind: Kind.DIRECTIVE,
761
849
  name: {
@@ -769,15 +857,13 @@ function makeDirectives(schema, directiveValues) {
769
857
  const directiveNodes = [];
770
858
  Object.entries(directiveValues).forEach(([directiveName, arrayOrSingleValue]) => {
771
859
  const directive = schema.getDirective(directiveName);
772
- if (directive != null) {
773
- if (Array.isArray(arrayOrSingleValue)) {
774
- arrayOrSingleValue.forEach(value => {
775
- directiveNodes.push(makeDirective(directiveName, value, directive));
776
- });
777
- }
778
- else {
779
- directiveNodes.push(makeDirective(directiveName, arrayOrSingleValue, directive));
780
- }
860
+ if (Array.isArray(arrayOrSingleValue)) {
861
+ arrayOrSingleValue.forEach(value => {
862
+ directiveNodes.push(makeDirective(directiveName, value, directive));
863
+ });
864
+ }
865
+ else {
866
+ directiveNodes.push(makeDirective(directiveName, arrayOrSingleValue, directive));
781
867
  }
782
868
  });
783
869
  return directiveNodes;
@@ -4156,5 +4242,5 @@ function isDocumentNode(object) {
4156
4242
  return object.kind !== undefined;
4157
4243
  }
4158
4244
 
4159
- export { MapperKind, SchemaDirectiveVisitor, SchemaVisitor, VisitSchemaKind, addTypes, appendObjectFields, argsToFieldConfigArgumentMap, argumentToArgumentConfig, asArray, buildOperationNodeForField, checkValidationErrors, cloneDirective, cloneSchema, cloneType, collectFields, compareNodes, compareStrings, correctASTNodes, createNamedStub, createSchemaDefinition, createStub, debugLog, fieldToFieldConfig, filterSchema, fixSchemaAst, fixWindowsPath, flattenArray, forEachDefaultValue, forEachField, getArgumentValues, getBuiltInForStub, getDirectives, getDirectivesInExtensions, getFieldsWithDirectives, getImplementingTypes, getLeadingCommentBlock, getResolversFromSchema, getResponseKeyFromInfo, getUserTypesFromSchema, healSchema, healTypes, implementsAbstractType, inputFieldToFieldConfig, isAsyncIterable, isDescribable, isDocumentNode, isDocumentString, isEqual, isNamedStub, isNotEqual, isValidPath, 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 };
4245
+ export { MapperKind, SchemaDirectiveVisitor, SchemaVisitor, VisitSchemaKind, addTypes, appendObjectFields, argsToFieldConfigArgumentMap, argumentToArgumentConfig, asArray, astFromValueUntyped, buildOperationNodeForField, checkValidationErrors, cloneDirective, cloneSchema, cloneType, collectFields, compareNodes, compareStrings, correctASTNodes, createNamedStub, createSchemaDefinition, createStub, debugLog, fieldToFieldConfig, filterSchema, fixSchemaAst, fixWindowsPath, flattenArray, forEachDefaultValue, forEachField, getArgumentValues, getBuiltInForStub, getDirectives, getDirectivesInExtensions, getFieldsWithDirectives, getImplementingTypes, getLeadingCommentBlock, getResolversFromSchema, getResponseKeyFromInfo, getUserTypesFromSchema, healSchema, healTypes, implementsAbstractType, inputFieldToFieldConfig, isAsyncIterable, isDescribable, isDocumentNode, isDocumentString, isEqual, isNamedStub, isNotEqual, isValidPath, 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 };
4160
4246
  //# sourceMappingURL=index.esm.js.map