astFromValue.js.flow 4.3 KB

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