getArgumentValues.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.getArgumentValues = void 0;
  4. const graphql_1 = require("graphql");
  5. const errors_js_1 = require("./errors.js");
  6. const inspect_js_1 = require("./inspect.js");
  7. /**
  8. * Prepares an object map of argument values given a list of argument
  9. * definitions and list of argument AST nodes.
  10. *
  11. * Note: The returned value is a plain Object with a prototype, since it is
  12. * exposed to user code. Care should be taken to not pull values from the
  13. * Object prototype.
  14. */
  15. function getArgumentValues(def, node, variableValues = {}) {
  16. var _a;
  17. const variableMap = Object.entries(variableValues).reduce((prev, [key, value]) => ({
  18. ...prev,
  19. [key]: value,
  20. }), {});
  21. const coercedValues = {};
  22. const argumentNodes = (_a = node.arguments) !== null && _a !== void 0 ? _a : [];
  23. const argNodeMap = argumentNodes.reduce((prev, arg) => ({
  24. ...prev,
  25. [arg.name.value]: arg,
  26. }), {});
  27. for (const { name, type: argType, defaultValue } of def.args) {
  28. const argumentNode = argNodeMap[name];
  29. if (!argumentNode) {
  30. if (defaultValue !== undefined) {
  31. coercedValues[name] = defaultValue;
  32. }
  33. else if ((0, graphql_1.isNonNullType)(argType)) {
  34. throw (0, errors_js_1.createGraphQLError)(`Argument "${name}" of required type "${(0, inspect_js_1.inspect)(argType)}" ` + 'was not provided.', {
  35. nodes: [node],
  36. });
  37. }
  38. continue;
  39. }
  40. const valueNode = argumentNode.value;
  41. let isNull = valueNode.kind === graphql_1.Kind.NULL;
  42. if (valueNode.kind === graphql_1.Kind.VARIABLE) {
  43. const variableName = valueNode.name.value;
  44. if (variableValues == null || variableMap[variableName] == null) {
  45. if (defaultValue !== undefined) {
  46. coercedValues[name] = defaultValue;
  47. }
  48. else if ((0, graphql_1.isNonNullType)(argType)) {
  49. throw (0, errors_js_1.createGraphQLError)(`Argument "${name}" of required type "${(0, inspect_js_1.inspect)(argType)}" ` +
  50. `was provided the variable "$${variableName}" which was not provided a runtime value.`, {
  51. nodes: [valueNode],
  52. });
  53. }
  54. continue;
  55. }
  56. isNull = variableValues[variableName] == null;
  57. }
  58. if (isNull && (0, graphql_1.isNonNullType)(argType)) {
  59. throw (0, errors_js_1.createGraphQLError)(`Argument "${name}" of non-null type "${(0, inspect_js_1.inspect)(argType)}" ` + 'must not be null.', {
  60. nodes: [valueNode],
  61. });
  62. }
  63. const coercedValue = (0, graphql_1.valueFromAST)(valueNode, argType, variableValues);
  64. if (coercedValue === undefined) {
  65. // Note: ValuesOfCorrectTypeRule validation should catch this before
  66. // execution. This is a runtime check to ensure execution does not
  67. // continue with an invalid argument value.
  68. throw (0, errors_js_1.createGraphQLError)(`Argument "${name}" has invalid value ${(0, graphql_1.print)(valueNode)}.`, {
  69. nodes: [valueNode],
  70. });
  71. }
  72. coercedValues[name] = coercedValue;
  73. }
  74. return coercedValues;
  75. }
  76. exports.getArgumentValues = getArgumentValues;