valueFromAST.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.valueFromAST = valueFromAST;
  6. var _objectValues3 = _interopRequireDefault(require("../polyfills/objectValues"));
  7. var _keyMap = _interopRequireDefault(require("../jsutils/keyMap"));
  8. var _inspect = _interopRequireDefault(require("../jsutils/inspect"));
  9. var _invariant = _interopRequireDefault(require("../jsutils/invariant"));
  10. var _isInvalid = _interopRequireDefault(require("../jsutils/isInvalid"));
  11. var _kinds = require("../language/kinds");
  12. var _definition = require("../type/definition");
  13. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  14. /**
  15. * Produces a JavaScript value given a GraphQL Value AST.
  16. *
  17. * A GraphQL type must be provided, which will be used to interpret different
  18. * GraphQL Value literals.
  19. *
  20. * Returns `undefined` when the value could not be validly coerced according to
  21. * the provided type.
  22. *
  23. * | GraphQL Value | JSON Value |
  24. * | -------------------- | ------------- |
  25. * | Input Object | Object |
  26. * | List | Array |
  27. * | Boolean | Boolean |
  28. * | String | String |
  29. * | Int / Float | Number |
  30. * | Enum Value | Mixed |
  31. * | NullValue | null |
  32. *
  33. */
  34. function valueFromAST(valueNode, type, variables) {
  35. if (!valueNode) {
  36. // When there is no node, then there is also no value.
  37. // Importantly, this is different from returning the value null.
  38. return;
  39. }
  40. if ((0, _definition.isNonNullType)(type)) {
  41. if (valueNode.kind === _kinds.Kind.NULL) {
  42. return; // Invalid: intentionally return no value.
  43. }
  44. return valueFromAST(valueNode, type.ofType, variables);
  45. }
  46. if (valueNode.kind === _kinds.Kind.NULL) {
  47. // This is explicitly returning the value null.
  48. return null;
  49. }
  50. if (valueNode.kind === _kinds.Kind.VARIABLE) {
  51. var variableName = valueNode.name.value;
  52. if (!variables || (0, _isInvalid.default)(variables[variableName])) {
  53. // No valid return value.
  54. return;
  55. }
  56. var variableValue = variables[variableName];
  57. if (variableValue === null && (0, _definition.isNonNullType)(type)) {
  58. return; // Invalid: intentionally return no value.
  59. } // Note: This does no further checking that this variable is correct.
  60. // This assumes that this query has been validated and the variable
  61. // usage here is of the correct type.
  62. return variableValue;
  63. }
  64. if ((0, _definition.isListType)(type)) {
  65. var itemType = type.ofType;
  66. if (valueNode.kind === _kinds.Kind.LIST) {
  67. var coercedValues = [];
  68. for (var _i2 = 0, _valueNode$values2 = valueNode.values; _i2 < _valueNode$values2.length; _i2++) {
  69. var itemNode = _valueNode$values2[_i2];
  70. if (isMissingVariable(itemNode, variables)) {
  71. // If an array contains a missing variable, it is either coerced to
  72. // null or if the item type is non-null, it considered invalid.
  73. if ((0, _definition.isNonNullType)(itemType)) {
  74. return; // Invalid: intentionally return no value.
  75. }
  76. coercedValues.push(null);
  77. } else {
  78. var itemValue = valueFromAST(itemNode, itemType, variables);
  79. if ((0, _isInvalid.default)(itemValue)) {
  80. return; // Invalid: intentionally return no value.
  81. }
  82. coercedValues.push(itemValue);
  83. }
  84. }
  85. return coercedValues;
  86. }
  87. var coercedValue = valueFromAST(valueNode, itemType, variables);
  88. if ((0, _isInvalid.default)(coercedValue)) {
  89. return; // Invalid: intentionally return no value.
  90. }
  91. return [coercedValue];
  92. }
  93. if ((0, _definition.isInputObjectType)(type)) {
  94. if (valueNode.kind !== _kinds.Kind.OBJECT) {
  95. return; // Invalid: intentionally return no value.
  96. }
  97. var coercedObj = Object.create(null);
  98. var fieldNodes = (0, _keyMap.default)(valueNode.fields, function (field) {
  99. return field.name.value;
  100. });
  101. for (var _i4 = 0, _objectValues2 = (0, _objectValues3.default)(type.getFields()); _i4 < _objectValues2.length; _i4++) {
  102. var field = _objectValues2[_i4];
  103. var fieldNode = fieldNodes[field.name];
  104. if (!fieldNode || isMissingVariable(fieldNode.value, variables)) {
  105. if (field.defaultValue !== undefined) {
  106. coercedObj[field.name] = field.defaultValue;
  107. } else if ((0, _definition.isNonNullType)(field.type)) {
  108. return; // Invalid: intentionally return no value.
  109. }
  110. continue;
  111. }
  112. var fieldValue = valueFromAST(fieldNode.value, field.type, variables);
  113. if ((0, _isInvalid.default)(fieldValue)) {
  114. return; // Invalid: intentionally return no value.
  115. }
  116. coercedObj[field.name] = fieldValue;
  117. }
  118. return coercedObj;
  119. }
  120. if ((0, _definition.isEnumType)(type)) {
  121. if (valueNode.kind !== _kinds.Kind.ENUM) {
  122. return; // Invalid: intentionally return no value.
  123. }
  124. var enumValue = type.getValue(valueNode.value);
  125. if (!enumValue) {
  126. return; // Invalid: intentionally return no value.
  127. }
  128. return enumValue.value;
  129. }
  130. /* istanbul ignore else */
  131. if ((0, _definition.isScalarType)(type)) {
  132. // Scalars fulfill parsing a literal value via parseLiteral().
  133. // Invalid values represent a failure to parse correctly, in which case
  134. // no value is returned.
  135. var result;
  136. try {
  137. result = type.parseLiteral(valueNode, variables);
  138. } catch (_error) {
  139. return; // Invalid: intentionally return no value.
  140. }
  141. if ((0, _isInvalid.default)(result)) {
  142. return; // Invalid: intentionally return no value.
  143. }
  144. return result;
  145. } // Not reachable. All possible input types have been considered.
  146. /* istanbul ignore next */
  147. (0, _invariant.default)(false, 'Unexpected input type: ' + (0, _inspect.default)(type));
  148. } // Returns true if the provided valueNode is a variable which is not defined
  149. // in the set of variables.
  150. function isMissingVariable(valueNode, variables) {
  151. return valueNode.kind === _kinds.Kind.VARIABLE && (!variables || (0, _isInvalid.default)(variables[valueNode.name.value]));
  152. }