valueFromAST.js 5.7 KB

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