valueFromAST.js.flow 5.3 KB

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