(
def: GraphQLField<any, any> | GraphQLDirective,
node: FieldNode | DirectiveNode,
variableValues: Record<string, any> = {},
)
| 22 | * Object prototype. |
| 23 | */ |
| 24 | export function getArgumentValues( |
| 25 | def: GraphQLField<any, any> | GraphQLDirective, |
| 26 | node: FieldNode | DirectiveNode, |
| 27 | variableValues: Record<string, any> = {}, |
| 28 | ): Record<string, any> { |
| 29 | const coercedValues = {}; |
| 30 | |
| 31 | const argumentNodes = node.arguments ?? []; |
| 32 | const argNodeMap: Record<string, ArgumentNode> = argumentNodes.reduce( |
| 33 | (prev, arg) => ({ |
| 34 | ...prev, |
| 35 | [arg.name.value]: arg, |
| 36 | }), |
| 37 | {}, |
| 38 | ); |
| 39 | |
| 40 | for (const { name, type: argType, defaultValue } of def.args) { |
| 41 | const argumentNode = argNodeMap[name]; |
| 42 | |
| 43 | if (!argumentNode) { |
| 44 | if (defaultValue !== undefined) { |
| 45 | coercedValues[name] = defaultValue; |
| 46 | } else if (isNonNullType(argType)) { |
| 47 | throw createGraphQLError( |
| 48 | `Argument "${name}" of required type "${inspect(argType)}" ` + 'was not provided.', |
| 49 | { |
| 50 | nodes: [node], |
| 51 | }, |
| 52 | ); |
| 53 | } |
| 54 | continue; |
| 55 | } |
| 56 | |
| 57 | const valueNode = argumentNode.value; |
| 58 | let isNull = valueNode.kind === Kind.NULL; |
| 59 | |
| 60 | if (valueNode.kind === Kind.VARIABLE) { |
| 61 | const variableName = valueNode.name.value; |
| 62 | if (variableValues == null || !hasOwnProperty(variableValues, variableName)) { |
| 63 | if (defaultValue !== undefined) { |
| 64 | coercedValues[name] = defaultValue; |
| 65 | } else if (isNonNullType(argType)) { |
| 66 | throw createGraphQLError( |
| 67 | `Argument "${name}" of required type "${inspect(argType)}" ` + |
| 68 | `was provided the variable "$${variableName}" which was not provided a runtime value.`, |
| 69 | { |
| 70 | nodes: [valueNode], |
| 71 | }, |
| 72 | ); |
| 73 | } |
| 74 | continue; |
| 75 | } |
| 76 | isNull = variableValues[variableName] == null; |
| 77 | } |
| 78 | |
| 79 | if (isNull && isNonNullType(argType)) { |
| 80 | throw createGraphQLError( |
| 81 | `Argument "${name}" of non-null type "${inspect(argType)}" ` + 'must not be null.', |
no test coverage detected
searching dependent graphs…