astFromValue.js 5.6 KB

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