astFromValue.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.astFromValue = astFromValue;
  6. var _isFinite = _interopRequireDefault(require("../polyfills/isFinite.js"));
  7. var _objectValues3 = _interopRequireDefault(require("../polyfills/objectValues.js"));
  8. var _inspect = _interopRequireDefault(require("../jsutils/inspect.js"));
  9. var _invariant = _interopRequireDefault(require("../jsutils/invariant.js"));
  10. var _isObjectLike = _interopRequireDefault(require("../jsutils/isObjectLike.js"));
  11. var _safeArrayFrom = _interopRequireDefault(require("../jsutils/safeArrayFrom.js"));
  12. var _kinds = require("../language/kinds.js");
  13. var _scalars = require("../type/scalars.js");
  14. var _definition = require("../type/definition.js");
  15. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  16. /**
  17. * Produces a GraphQL Value AST given a JavaScript object.
  18. * Function will match JavaScript/JSON values to GraphQL AST schema format
  19. * by using suggested GraphQLInputType. For example:
  20. *
  21. * astFromValue("value", GraphQLString)
  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. function astFromValue(value, type) {
  38. if ((0, _definition.isNonNullType)(type)) {
  39. var astValue = astFromValue(value, type.ofType);
  40. if ((astValue === null || astValue === void 0 ? void 0 : astValue.kind) === _kinds.Kind.NULL) {
  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. var itemType = type.ofType;
  56. var items = (0, _safeArrayFrom.default)(value);
  57. if (items != null) {
  58. var valuesNodes = [];
  59. for (var _i2 = 0; _i2 < items.length; _i2++) {
  60. var item = items[_i2];
  61. var itemNode = astFromValue(item, itemType);
  62. if (itemNode != null) {
  63. valuesNodes.push(itemNode);
  64. }
  65. }
  66. return {
  67. kind: _kinds.Kind.LIST,
  68. values: valuesNodes
  69. };
  70. }
  71. return astFromValue(value, itemType);
  72. } // Populate the fields of the input object by creating ASTs from each value
  73. // in the JavaScript object according to the fields in the input type.
  74. if ((0, _definition.isInputObjectType)(type)) {
  75. if (!(0, _isObjectLike.default)(value)) {
  76. return null;
  77. }
  78. var fieldNodes = [];
  79. for (var _i4 = 0, _objectValues2 = (0, _objectValues3.default)(type.getFields()); _i4 < _objectValues2.length; _i4++) {
  80. var field = _objectValues2[_i4];
  81. var fieldValue = astFromValue(value[field.name], field.type);
  82. if (fieldValue) {
  83. fieldNodes.push({
  84. kind: _kinds.Kind.OBJECT_FIELD,
  85. name: {
  86. kind: _kinds.Kind.NAME,
  87. value: field.name
  88. },
  89. value: fieldValue
  90. });
  91. }
  92. }
  93. return {
  94. kind: _kinds.Kind.OBJECT,
  95. fields: fieldNodes
  96. };
  97. } // istanbul ignore else (See: 'https://github.com/graphql/graphql-js/issues/2618')
  98. if ((0, _definition.isLeafType)(type)) {
  99. // Since value is an internally represented value, it must be serialized
  100. // to an externally represented value before converting into an AST.
  101. var serialized = type.serialize(value);
  102. if (serialized == null) {
  103. return null;
  104. } // Others serialize based on their corresponding JavaScript scalar types.
  105. if (typeof serialized === 'boolean') {
  106. return {
  107. kind: _kinds.Kind.BOOLEAN,
  108. value: serialized
  109. };
  110. } // JavaScript numbers can be Int or Float values.
  111. if (typeof serialized === 'number' && (0, _isFinite.default)(serialized)) {
  112. var stringNum = String(serialized);
  113. return integerStringRegExp.test(stringNum) ? {
  114. kind: _kinds.Kind.INT,
  115. value: stringNum
  116. } : {
  117. kind: _kinds.Kind.FLOAT,
  118. value: stringNum
  119. };
  120. }
  121. if (typeof serialized === 'string') {
  122. // Enum types use Enum literals.
  123. if ((0, _definition.isEnumType)(type)) {
  124. return {
  125. kind: _kinds.Kind.ENUM,
  126. value: serialized
  127. };
  128. } // ID types can use Int literals.
  129. if (type === _scalars.GraphQLID && integerStringRegExp.test(serialized)) {
  130. return {
  131. kind: _kinds.Kind.INT,
  132. value: serialized
  133. };
  134. }
  135. return {
  136. kind: _kinds.Kind.STRING,
  137. value: serialized
  138. };
  139. }
  140. throw new TypeError("Cannot convert value to AST: ".concat((0, _inspect.default)(serialized), "."));
  141. } // istanbul ignore next (Not reachable. All possible input types have been considered)
  142. false || (0, _invariant.default)(0, 'Unexpected input type: ' + (0, _inspect.default)(type));
  143. }
  144. /**
  145. * IntValue:
  146. * - NegativeSign? 0
  147. * - NegativeSign? NonZeroDigit ( Digit+ )?
  148. */
  149. var integerStringRegExp = /^-?(?:0|[1-9][0-9]*)$/;