valueFromAST.js 5.3 KB

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