valueFromAST.mjs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import objectValues from "../polyfills/objectValues.mjs";
  2. import keyMap from "../jsutils/keyMap.mjs";
  3. import inspect from "../jsutils/inspect.mjs";
  4. import invariant from "../jsutils/invariant.mjs";
  5. import { Kind } from "../language/kinds.mjs";
  6. import { isLeafType, isInputObjectType, isListType, isNonNullType } from "../type/definition.mjs";
  7. /**
  8. * Produces a JavaScript value given a GraphQL Value AST.
  9. *
  10. * A GraphQL type must be provided, which will be used to interpret different
  11. * GraphQL Value literals.
  12. *
  13. * Returns `undefined` when the value could not be validly coerced according to
  14. * the provided type.
  15. *
  16. * | GraphQL Value | JSON Value |
  17. * | -------------------- | ------------- |
  18. * | Input Object | Object |
  19. * | List | Array |
  20. * | Boolean | Boolean |
  21. * | String | String |
  22. * | Int / Float | Number |
  23. * | Enum Value | Mixed |
  24. * | NullValue | null |
  25. *
  26. */
  27. export function valueFromAST(valueNode, type, variables) {
  28. if (!valueNode) {
  29. // When there is no node, then there is also no value.
  30. // Importantly, this is different from returning the value null.
  31. return;
  32. }
  33. if (valueNode.kind === Kind.VARIABLE) {
  34. var variableName = valueNode.name.value;
  35. if (variables == null || variables[variableName] === undefined) {
  36. // No valid return value.
  37. return;
  38. }
  39. var variableValue = variables[variableName];
  40. if (variableValue === null && isNonNullType(type)) {
  41. return; // Invalid: intentionally return no value.
  42. } // Note: This does no further checking that this variable is correct.
  43. // This assumes that this query has been validated and the variable
  44. // usage here is of the correct type.
  45. return variableValue;
  46. }
  47. if (isNonNullType(type)) {
  48. if (valueNode.kind === Kind.NULL) {
  49. return; // Invalid: intentionally return no value.
  50. }
  51. return valueFromAST(valueNode, type.ofType, variables);
  52. }
  53. if (valueNode.kind === Kind.NULL) {
  54. // This is explicitly returning the value null.
  55. return null;
  56. }
  57. if (isListType(type)) {
  58. var itemType = type.ofType;
  59. if (valueNode.kind === Kind.LIST) {
  60. var coercedValues = [];
  61. for (var _i2 = 0, _valueNode$values2 = valueNode.values; _i2 < _valueNode$values2.length; _i2++) {
  62. var itemNode = _valueNode$values2[_i2];
  63. if (isMissingVariable(itemNode, variables)) {
  64. // If an array contains a missing variable, it is either coerced to
  65. // null or if the item type is non-null, it considered invalid.
  66. if (isNonNullType(itemType)) {
  67. return; // Invalid: intentionally return no value.
  68. }
  69. coercedValues.push(null);
  70. } else {
  71. var itemValue = valueFromAST(itemNode, itemType, variables);
  72. if (itemValue === undefined) {
  73. return; // Invalid: intentionally return no value.
  74. }
  75. coercedValues.push(itemValue);
  76. }
  77. }
  78. return coercedValues;
  79. }
  80. var coercedValue = valueFromAST(valueNode, itemType, variables);
  81. if (coercedValue === undefined) {
  82. return; // Invalid: intentionally return no value.
  83. }
  84. return [coercedValue];
  85. }
  86. if (isInputObjectType(type)) {
  87. if (valueNode.kind !== Kind.OBJECT) {
  88. return; // Invalid: intentionally return no value.
  89. }
  90. var coercedObj = Object.create(null);
  91. var fieldNodes = keyMap(valueNode.fields, function (field) {
  92. return field.name.value;
  93. });
  94. for (var _i4 = 0, _objectValues2 = objectValues(type.getFields()); _i4 < _objectValues2.length; _i4++) {
  95. var field = _objectValues2[_i4];
  96. var 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. var 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. } // istanbul ignore else (See: 'https://github.com/graphql/graphql-js/issues/2618')
  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. var 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. } // istanbul ignore next (Not reachable. All possible input types have been considered)
  128. false || invariant(0, 'Unexpected input type: ' + inspect(type));
  129. } // Returns true if the provided valueNode is a variable which is not defined
  130. // in the set of variables.
  131. function isMissingVariable(valueNode, variables) {
  132. return valueNode.kind === Kind.VARIABLE && (variables == null || variables[valueNode.name.value] === undefined);
  133. }