valueFromAST.mjs 5.5 KB

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