values.js 8.0 KB

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