values.mjs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. import find from "../polyfills/find.mjs";
  2. import keyMap from "../jsutils/keyMap.mjs";
  3. import inspect from "../jsutils/inspect.mjs";
  4. import printPathArray from "../jsutils/printPathArray.mjs";
  5. import { GraphQLError } from "../error/GraphQLError.mjs";
  6. import { Kind } from "../language/kinds.mjs";
  7. import { print } from "../language/printer.mjs";
  8. import { isInputType, isNonNullType } from "../type/definition.mjs";
  9. import { typeFromAST } from "../utilities/typeFromAST.mjs";
  10. import { valueFromAST } from "../utilities/valueFromAST.mjs";
  11. import { coerceInputValue } from "../utilities/coerceInputValue.mjs";
  12. /**
  13. * Prepares an object map of variableValues of the correct type based on the
  14. * provided variable definitions and arbitrary input. If the input cannot be
  15. * parsed to match the variable definitions, a GraphQLError will be thrown.
  16. *
  17. * Note: The returned value is a plain Object with a prototype, since it is
  18. * exposed to user code. Care should be taken to not pull values from the
  19. * Object prototype.
  20. *
  21. * @internal
  22. */
  23. export function getVariableValues(schema, varDefNodes, inputs, options) {
  24. var errors = [];
  25. var maxErrors = options === null || options === void 0 ? void 0 : options.maxErrors;
  26. try {
  27. var coerced = coerceVariableValues(schema, varDefNodes, inputs, function (error) {
  28. if (maxErrors != null && errors.length >= maxErrors) {
  29. throw new GraphQLError('Too many errors processing variables, error limit reached. Execution aborted.');
  30. }
  31. errors.push(error);
  32. });
  33. if (errors.length === 0) {
  34. return {
  35. coerced: coerced
  36. };
  37. }
  38. } catch (error) {
  39. errors.push(error);
  40. }
  41. return {
  42. errors: errors
  43. };
  44. }
  45. function coerceVariableValues(schema, varDefNodes, inputs, onError) {
  46. var coercedValues = {};
  47. var _loop = function _loop(_i2) {
  48. var varDefNode = varDefNodes[_i2];
  49. var varName = varDefNode.variable.name.value;
  50. var varType = typeFromAST(schema, varDefNode.type);
  51. if (!isInputType(varType)) {
  52. // Must use input types for variables. This should be caught during
  53. // validation, however is checked again here for safety.
  54. var varTypeStr = print(varDefNode.type);
  55. onError(new GraphQLError("Variable \"$".concat(varName, "\" expected value of type \"").concat(varTypeStr, "\" which cannot be used as an input type."), varDefNode.type));
  56. return "continue";
  57. }
  58. if (!hasOwnProperty(inputs, varName)) {
  59. if (varDefNode.defaultValue) {
  60. coercedValues[varName] = valueFromAST(varDefNode.defaultValue, varType);
  61. } else if (isNonNullType(varType)) {
  62. var _varTypeStr = inspect(varType);
  63. onError(new GraphQLError("Variable \"$".concat(varName, "\" of required type \"").concat(_varTypeStr, "\" was not provided."), varDefNode));
  64. }
  65. return "continue";
  66. }
  67. var value = inputs[varName];
  68. if (value === null && isNonNullType(varType)) {
  69. var _varTypeStr2 = inspect(varType);
  70. onError(new GraphQLError("Variable \"$".concat(varName, "\" of non-null type \"").concat(_varTypeStr2, "\" must not be null."), varDefNode));
  71. return "continue";
  72. }
  73. coercedValues[varName] = coerceInputValue(value, varType, function (path, invalidValue, error) {
  74. var prefix = "Variable \"$".concat(varName, "\" got invalid value ") + inspect(invalidValue);
  75. if (path.length > 0) {
  76. prefix += " at \"".concat(varName).concat(printPathArray(path), "\"");
  77. }
  78. onError(new GraphQLError(prefix + '; ' + error.message, varDefNode, undefined, undefined, undefined, error.originalError));
  79. });
  80. };
  81. for (var _i2 = 0; _i2 < varDefNodes.length; _i2++) {
  82. var _ret = _loop(_i2);
  83. if (_ret === "continue") continue;
  84. }
  85. return coercedValues;
  86. }
  87. /**
  88. * Prepares an object map of argument values given a list of argument
  89. * definitions and list of argument AST nodes.
  90. *
  91. * Note: The returned value is a plain Object with a prototype, since it is
  92. * exposed to user code. Care should be taken to not pull values from the
  93. * Object prototype.
  94. *
  95. * @internal
  96. */
  97. export function getArgumentValues(def, node, variableValues) {
  98. var _node$arguments;
  99. var coercedValues = {}; // istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2203')
  100. var argumentNodes = (_node$arguments = node.arguments) !== null && _node$arguments !== void 0 ? _node$arguments : [];
  101. var argNodeMap = keyMap(argumentNodes, function (arg) {
  102. return arg.name.value;
  103. });
  104. for (var _i4 = 0, _def$args2 = def.args; _i4 < _def$args2.length; _i4++) {
  105. var argDef = _def$args2[_i4];
  106. var name = argDef.name;
  107. var argType = argDef.type;
  108. var argumentNode = argNodeMap[name];
  109. if (!argumentNode) {
  110. if (argDef.defaultValue !== undefined) {
  111. coercedValues[name] = argDef.defaultValue;
  112. } else if (isNonNullType(argType)) {
  113. throw new GraphQLError("Argument \"".concat(name, "\" of required type \"").concat(inspect(argType), "\" ") + 'was not provided.', node);
  114. }
  115. continue;
  116. }
  117. var valueNode = argumentNode.value;
  118. var isNull = valueNode.kind === Kind.NULL;
  119. if (valueNode.kind === Kind.VARIABLE) {
  120. var variableName = valueNode.name.value;
  121. if (variableValues == null || !hasOwnProperty(variableValues, variableName)) {
  122. if (argDef.defaultValue !== undefined) {
  123. coercedValues[name] = argDef.defaultValue;
  124. } else if (isNonNullType(argType)) {
  125. throw new GraphQLError("Argument \"".concat(name, "\" of required type \"").concat(inspect(argType), "\" ") + "was provided the variable \"$".concat(variableName, "\" which was not provided a runtime value."), valueNode);
  126. }
  127. continue;
  128. }
  129. isNull = variableValues[variableName] == null;
  130. }
  131. if (isNull && isNonNullType(argType)) {
  132. throw new GraphQLError("Argument \"".concat(name, "\" of non-null type \"").concat(inspect(argType), "\" ") + 'must not be null.', valueNode);
  133. }
  134. var coercedValue = valueFromAST(valueNode, argType, variableValues);
  135. if (coercedValue === undefined) {
  136. // Note: ValuesOfCorrectTypeRule validation should catch this before
  137. // execution. This is a runtime check to ensure execution does not
  138. // continue with an invalid argument value.
  139. throw new GraphQLError("Argument \"".concat(name, "\" has invalid value ").concat(print(valueNode), "."), valueNode);
  140. }
  141. coercedValues[name] = coercedValue;
  142. }
  143. return coercedValues;
  144. }
  145. /**
  146. * Prepares an object map of argument values given a directive definition
  147. * and a AST node which may contain directives. Optionally also accepts a map
  148. * of variable values.
  149. *
  150. * If the directive does not exist on the node, returns undefined.
  151. *
  152. * Note: The returned value is a plain Object with a prototype, since it is
  153. * exposed to user code. Care should be taken to not pull values from the
  154. * Object prototype.
  155. */
  156. export function getDirectiveValues(directiveDef, node, variableValues) {
  157. var directiveNode = node.directives && find(node.directives, function (directive) {
  158. return directive.name.value === directiveDef.name;
  159. });
  160. if (directiveNode) {
  161. return getArgumentValues(directiveDef, directiveNode, variableValues);
  162. }
  163. }
  164. function hasOwnProperty(obj, prop) {
  165. return Object.prototype.hasOwnProperty.call(obj, prop);
  166. }