astFromValue.mjs 4.6 KB

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