astFromValue.mjs 5.0 KB

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