valueFromAST.mjs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import { inspect } from '../jsutils/inspect.mjs';
  2. import { invariant } from '../jsutils/invariant.mjs';
  3. import { keyMap } from '../jsutils/keyMap.mjs';
  4. import { Kind } from '../language/kinds.mjs';
  5. import {
  6. isInputObjectType,
  7. isLeafType,
  8. isListType,
  9. isNonNullType,
  10. } from '../type/definition.mjs';
  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. export 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 === 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 && 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 (isNonNullType(type)) {
  52. if (valueNode.kind === Kind.NULL) {
  53. return; // Invalid: intentionally return no value.
  54. }
  55. return valueFromAST(valueNode, type.ofType, variables);
  56. }
  57. if (valueNode.kind === Kind.NULL) {
  58. // This is explicitly returning the value null.
  59. return null;
  60. }
  61. if (isListType(type)) {
  62. const itemType = type.ofType;
  63. if (valueNode.kind === 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 (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 (isInputObjectType(type)) {
  90. if (valueNode.kind !== Kind.OBJECT) {
  91. return; // Invalid: intentionally return no value.
  92. }
  93. const coercedObj = Object.create(null);
  94. const fieldNodes = keyMap(valueNode.fields, (field) => field.name.value);
  95. for (const field of Object.values(type.getFields())) {
  96. const fieldNode = fieldNodes[field.name];
  97. if (!fieldNode || isMissingVariable(fieldNode.value, variables)) {
  98. if (field.defaultValue !== undefined) {
  99. coercedObj[field.name] = field.defaultValue;
  100. } else if (isNonNullType(field.type)) {
  101. return; // Invalid: intentionally return no value.
  102. }
  103. continue;
  104. }
  105. const fieldValue = valueFromAST(fieldNode.value, field.type, variables);
  106. if (fieldValue === undefined) {
  107. return; // Invalid: intentionally return no value.
  108. }
  109. coercedObj[field.name] = fieldValue;
  110. }
  111. return coercedObj;
  112. }
  113. if (isLeafType(type)) {
  114. // Scalars and Enums fulfill parsing a literal value via parseLiteral().
  115. // Invalid values represent a failure to parse correctly, in which case
  116. // no value is returned.
  117. let result;
  118. try {
  119. result = type.parseLiteral(valueNode, variables);
  120. } catch (_error) {
  121. return; // Invalid: intentionally return no value.
  122. }
  123. if (result === undefined) {
  124. return; // Invalid: intentionally return no value.
  125. }
  126. return result;
  127. }
  128. /* c8 ignore next 3 */
  129. // Not reachable, all possible input types have been considered.
  130. false || invariant(false, 'Unexpected input type: ' + inspect(type));
  131. } // Returns true if the provided valueNode is a variable which is not defined
  132. // in the set of variables.
  133. function isMissingVariable(valueNode, variables) {
  134. return (
  135. valueNode.kind === Kind.VARIABLE &&
  136. (variables == null || variables[valueNode.name.value] === undefined)
  137. );
  138. }