astFromValue.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.astFromValue = astFromValue;
  6. var _iterall = require("iterall");
  7. var _objectValues3 = _interopRequireDefault(require("../polyfills/objectValues"));
  8. var _inspect = _interopRequireDefault(require("../jsutils/inspect"));
  9. var _invariant = _interopRequireDefault(require("../jsutils/invariant"));
  10. var _isNullish = _interopRequireDefault(require("../jsutils/isNullish"));
  11. var _isInvalid = _interopRequireDefault(require("../jsutils/isInvalid"));
  12. var _isObjectLike = _interopRequireDefault(require("../jsutils/isObjectLike"));
  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 value.
  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. * | Mixed | Enum Value |
  31. * | null | NullValue |
  32. *
  33. */
  34. function astFromValue(value, type) {
  35. if ((0, _definition.isNonNullType)(type)) {
  36. var astValue = astFromValue(value, type.ofType);
  37. if (astValue && astValue.kind === _kinds.Kind.NULL) {
  38. return null;
  39. }
  40. return astValue;
  41. } // only explicit null, not undefined, NaN
  42. if (value === null) {
  43. return {
  44. kind: _kinds.Kind.NULL
  45. };
  46. } // undefined, NaN
  47. if ((0, _isInvalid.default)(value)) {
  48. return null;
  49. } // Convert JavaScript array to GraphQL list. If the GraphQLType is a list, but
  50. // the value is not an array, convert the value using the list's item type.
  51. if ((0, _definition.isListType)(type)) {
  52. var itemType = type.ofType;
  53. if ((0, _iterall.isCollection)(value)) {
  54. var valuesNodes = [];
  55. (0, _iterall.forEach)(value, function (item) {
  56. var itemNode = astFromValue(item, itemType);
  57. if (itemNode) {
  58. valuesNodes.push(itemNode);
  59. }
  60. });
  61. return {
  62. kind: _kinds.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 ((0, _definition.isInputObjectType)(type)) {
  70. if (!(0, _isObjectLike.default)(value)) {
  71. return null;
  72. }
  73. var fieldNodes = [];
  74. for (var _i2 = 0, _objectValues2 = (0, _objectValues3.default)(type.getFields()); _i2 < _objectValues2.length; _i2++) {
  75. var field = _objectValues2[_i2];
  76. var fieldValue = astFromValue(value[field.name], field.type);
  77. if (fieldValue) {
  78. fieldNodes.push({
  79. kind: _kinds.Kind.OBJECT_FIELD,
  80. name: {
  81. kind: _kinds.Kind.NAME,
  82. value: field.name
  83. },
  84. value: fieldValue
  85. });
  86. }
  87. }
  88. return {
  89. kind: _kinds.Kind.OBJECT,
  90. fields: fieldNodes
  91. };
  92. }
  93. /* istanbul ignore else */
  94. if ((0, _definition.isLeafType)(type)) {
  95. // Since value is an internally represented value, it must be serialized
  96. // to an externally represented value before converting into an AST.
  97. var serialized = type.serialize(value);
  98. if ((0, _isNullish.default)(serialized)) {
  99. return null;
  100. } // Others serialize based on their corresponding JavaScript scalar types.
  101. if (typeof serialized === 'boolean') {
  102. return {
  103. kind: _kinds.Kind.BOOLEAN,
  104. value: serialized
  105. };
  106. } // JavaScript numbers can be Int or Float values.
  107. if (typeof serialized === 'number') {
  108. var stringNum = String(serialized);
  109. return integerStringRegExp.test(stringNum) ? {
  110. kind: _kinds.Kind.INT,
  111. value: stringNum
  112. } : {
  113. kind: _kinds.Kind.FLOAT,
  114. value: stringNum
  115. };
  116. }
  117. if (typeof serialized === 'string') {
  118. // Enum types use Enum literals.
  119. if ((0, _definition.isEnumType)(type)) {
  120. return {
  121. kind: _kinds.Kind.ENUM,
  122. value: serialized
  123. };
  124. } // ID types can use Int literals.
  125. if (type === _scalars.GraphQLID && integerStringRegExp.test(serialized)) {
  126. return {
  127. kind: _kinds.Kind.INT,
  128. value: serialized
  129. };
  130. }
  131. return {
  132. kind: _kinds.Kind.STRING,
  133. value: serialized
  134. };
  135. }
  136. throw new TypeError("Cannot convert value to AST: ".concat((0, _inspect.default)(serialized)));
  137. } // Not reachable. All possible input types have been considered.
  138. /* istanbul ignore next */
  139. (0, _invariant.default)(false, 'Unexpected input type: ' + (0, _inspect.default)(type));
  140. }
  141. /**
  142. * IntValue:
  143. * - NegativeSign? 0
  144. * - NegativeSign? NonZeroDigit ( Digit+ )?
  145. */
  146. var integerStringRegExp = /^-?(?:0|[1-9][0-9]*)$/;