astFromValue.js.flow 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // @flow strict
  2. import isFinite from '../polyfills/isFinite';
  3. import arrayFrom from '../polyfills/arrayFrom';
  4. import objectValues from '../polyfills/objectValues';
  5. import inspect from '../jsutils/inspect';
  6. import invariant from '../jsutils/invariant';
  7. import isObjectLike from '../jsutils/isObjectLike';
  8. import isCollection from '../jsutils/isCollection';
  9. import type { ValueNode } from '../language/ast';
  10. import { Kind } from '../language/kinds';
  11. import type { GraphQLInputType } from '../type/definition';
  12. import { GraphQLID } from '../type/scalars';
  13. import {
  14. isLeafType,
  15. isEnumType,
  16. isInputObjectType,
  17. isListType,
  18. isNonNullType,
  19. } from '../type/definition';
  20. /**
  21. * Produces a GraphQL Value AST given a JavaScript object.
  22. * Function will match JavaScript/JSON values to GraphQL AST schema format
  23. * by using suggested GraphQLInputType. For example:
  24. *
  25. * astFromValue("value", GraphQLString)
  26. *
  27. * A GraphQL type must be provided, which will be used to interpret different
  28. * JavaScript values.
  29. *
  30. * | JSON Value | GraphQL Value |
  31. * | ------------- | -------------------- |
  32. * | Object | Input Object |
  33. * | Array | List |
  34. * | Boolean | Boolean |
  35. * | String | String / Enum Value |
  36. * | Number | Int / Float |
  37. * | Mixed | Enum Value |
  38. * | null | NullValue |
  39. *
  40. */
  41. export function astFromValue(value: mixed, type: GraphQLInputType): ?ValueNode {
  42. if (isNonNullType(type)) {
  43. const astValue = astFromValue(value, type.ofType);
  44. if (astValue?.kind === Kind.NULL) {
  45. return null;
  46. }
  47. return astValue;
  48. }
  49. // only explicit null, not undefined, NaN
  50. if (value === null) {
  51. return { kind: Kind.NULL };
  52. }
  53. // undefined
  54. if (value === undefined) {
  55. return null;
  56. }
  57. // Convert JavaScript array to GraphQL list. If the GraphQLType is a list, but
  58. // the value is not an array, convert the value using the list's item type.
  59. if (isListType(type)) {
  60. const itemType = type.ofType;
  61. if (isCollection(value)) {
  62. const valuesNodes = [];
  63. // Since we transpile for-of in loose mode it doesn't support iterators
  64. // and it's required to first convert iteratable into array
  65. for (const item of arrayFrom(value)) {
  66. const itemNode = astFromValue(item, itemType);
  67. if (itemNode != null) {
  68. valuesNodes.push(itemNode);
  69. }
  70. }
  71. return { kind: Kind.LIST, values: valuesNodes };
  72. }
  73. return astFromValue(value, itemType);
  74. }
  75. // Populate the fields of the input object by creating ASTs from each value
  76. // in the JavaScript object according to the fields in the input type.
  77. if (isInputObjectType(type)) {
  78. if (!isObjectLike(value)) {
  79. return null;
  80. }
  81. const fieldNodes = [];
  82. for (const field of objectValues(type.getFields())) {
  83. const fieldValue = astFromValue(value[field.name], field.type);
  84. if (fieldValue) {
  85. fieldNodes.push({
  86. kind: Kind.OBJECT_FIELD,
  87. name: { kind: Kind.NAME, value: field.name },
  88. value: fieldValue,
  89. });
  90. }
  91. }
  92. return { kind: Kind.OBJECT, fields: fieldNodes };
  93. }
  94. // istanbul ignore else (See: 'https://github.com/graphql/graphql-js/issues/2618')
  95. if (isLeafType(type)) {
  96. // Since value is an internally represented value, it must be serialized
  97. // to an externally represented value before converting into an AST.
  98. const serialized = type.serialize(value);
  99. if (serialized == null) {
  100. return null;
  101. }
  102. // Others serialize based on their corresponding JavaScript scalar types.
  103. if (typeof serialized === 'boolean') {
  104. return { kind: Kind.BOOLEAN, value: serialized };
  105. }
  106. // JavaScript numbers can be Int or Float values.
  107. if (typeof serialized === 'number' && isFinite(serialized)) {
  108. const stringNum = String(serialized);
  109. return integerStringRegExp.test(stringNum)
  110. ? { kind: Kind.INT, value: stringNum }
  111. : { kind: Kind.FLOAT, value: stringNum };
  112. }
  113. if (typeof serialized === 'string') {
  114. // Enum types use Enum literals.
  115. if (isEnumType(type)) {
  116. return { kind: Kind.ENUM, value: serialized };
  117. }
  118. // ID types can use Int literals.
  119. if (type === GraphQLID && integerStringRegExp.test(serialized)) {
  120. return { kind: Kind.INT, value: serialized };
  121. }
  122. return {
  123. kind: Kind.STRING,
  124. value: serialized,
  125. };
  126. }
  127. throw new TypeError(`Cannot convert value to AST: ${inspect(serialized)}.`);
  128. }
  129. // istanbul ignore next (Not reachable. All possible input types have been considered)
  130. invariant(false, 'Unexpected input type: ' + inspect((type: empty)));
  131. }
  132. /**
  133. * IntValue:
  134. * - NegativeSign? 0
  135. * - NegativeSign? NonZeroDigit ( Digit+ )?
  136. */
  137. const integerStringRegExp = /^-?(?:0|[1-9][0-9]*)$/;