MCPcopy
hub / github.com/ardatan/graphql-tools / getArgumentValues

Function getArgumentValues

packages/utils/src/getArgumentValues.ts:24–100  ·  view source on GitHub ↗
(
  def: GraphQLField<any, any> | GraphQLDirective,
  node: FieldNode | DirectiveNode,
  variableValues: Record<string, any> = {},
)

Source from the content-addressed store, hash-verified

22 * Object prototype.
23 */
24export 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.',

Callers 3

executeFieldFunction · 0.90
executeSubscriptionFunction · 0.90
getDirectiveExtensionsFunction · 0.85

Calls 4

hasOwnPropertyFunction · 0.90
createGraphQLErrorFunction · 0.85
inspectFunction · 0.85
printFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…