astFromValue.js 4.9 KB

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