values.js 7.7 KB

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