UNPKG

@graphql-tools/utils

Version:

Common package containing utils and types for GraphQL tools

80 lines (79 loc) 3.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getArgumentValues = getArgumentValues; const cross_inspect_1 = require("cross-inspect"); const graphql_1 = require("graphql"); const errors_js_1 = require("./errors.js"); const jsutils_js_1 = require("./jsutils.js"); /** * Prepares an object map of argument values given a list of argument * definitions and list of argument AST nodes. * * Note: The returned value is a plain Object with a prototype, since it is * exposed to user code. Care should be taken to not pull values from the * Object prototype. */ function getArgumentValues(def, node, variableValues = {}) { const coercedValues = {}; const argumentNodes = node.arguments ?? []; const argNodeMap = argumentNodes.reduce((prev, arg) => ({ ...prev, [arg.name.value]: arg, }), {}); for (const arg of def.args) { const argumentNode = argNodeMap[arg.name]; if (!argumentNode) { if ('default' in arg && arg.default) { // graphql v17 coercedValues[arg.name] = 'value' in arg.default ? arg.default.value : (0, graphql_1.valueFromASTUntyped)(arg.default.literal); } else if (arg.defaultValue !== undefined) { // graphql < v17 coercedValues[arg.name] = arg.defaultValue; } else if ((0, graphql_1.isNonNullType)(arg.type)) { throw (0, errors_js_1.createGraphQLError)(`Argument "${arg.name}" of required type "${(0, cross_inspect_1.inspect)(arg.type)}" ` + 'was not provided.', { nodes: [node], }); } continue; } const valueNode = argumentNode.value; let isNull = valueNode.kind === graphql_1.Kind.NULL; if (valueNode.kind === graphql_1.Kind.VARIABLE) { const variableName = valueNode.name.value; if (variableValues == null || !(0, jsutils_js_1.hasOwnProperty)(variableValues, variableName)) { if (arg.defaultValue !== undefined) { coercedValues[arg.name] = arg.defaultValue; } else if ((0, graphql_1.isNonNullType)(arg.type)) { throw (0, errors_js_1.createGraphQLError)(`Argument "${arg.name}" of required type "${(0, cross_inspect_1.inspect)(arg.type)}" ` + `was provided the variable "$${variableName}" which was not provided a runtime value.`, { nodes: [valueNode], }); } continue; } isNull = variableValues[variableName] == null; } if (isNull && (0, graphql_1.isNonNullType)(arg.type)) { throw (0, errors_js_1.createGraphQLError)(`Argument "${arg.name}" of non-null type "${(0, cross_inspect_1.inspect)(arg.type)}" ` + 'must not be null.', { nodes: [valueNode], }); } const coercedValue = (0, graphql_1.valueFromAST)(valueNode, arg.type, variableValues); if (coercedValue === undefined) { // Note: ValuesOfCorrectTypeRule validation should catch this before // execution. This is a runtime check to ensure execution does not // continue with an invalid argument value. throw (0, errors_js_1.createGraphQLError)(`Argument "${arg.name}" has invalid value ${(0, graphql_1.print)(valueNode)}.`, { nodes: [valueNode], }); } coercedValues[arg.name] = coercedValue; } return coercedValues; }