/
getArgumentValues.ts
101 lines (91 loc) · 3.01 KB
/
getArgumentValues.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import {
valueFromAST,
GraphQLField,
GraphQLDirective,
DirectiveNode,
FieldNode,
isNonNullType,
GraphQLError,
Kind,
print,
} from 'graphql';
import { keyMap } from './keyMap';
import { inspect } from './inspect';
/**
* 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.
*/
export function getArgumentValues(
def: GraphQLField<any, any> | GraphQLDirective,
node: FieldNode | DirectiveNode,
variableValues?: { [variableName: string]: any },
): { [argument: string]: any } {
const coercedValues = {};
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
const argumentNodes = node.arguments ?? [];
const argNodeMap = keyMap(argumentNodes, (arg) => arg.name.value);
for (const argDef of def.args) {
const name = argDef.name;
const argType = argDef.type;
const argumentNode = argNodeMap[name];
if (!argumentNode) {
if (argDef.defaultValue !== undefined) {
coercedValues[name] = argDef.defaultValue;
} else if (isNonNullType(argType)) {
throw new GraphQLError(
`Argument "${name}" of required type "${inspect(argType)}" ` +
'was not provided.',
node,
);
}
continue;
}
const valueNode = argumentNode.value;
let isNull = valueNode.kind === Kind.NULL;
if (valueNode.kind === Kind.VARIABLE) {
const variableName = valueNode.name.value;
if (
variableValues == null ||
!hasOwnProperty(variableValues, variableName)
) {
if (argDef.defaultValue !== undefined) {
coercedValues[name] = argDef.defaultValue;
} else if (isNonNullType(argType)) {
throw new GraphQLError(
`Argument "${name}" of required type "${inspect(argType)}" ` +
`was provided the variable "$${variableName}" which was not provided a runtime value.`,
valueNode,
);
}
continue;
}
isNull = variableValues[variableName] == null;
}
if (isNull && isNonNullType(argType)) {
throw new GraphQLError(
`Argument "${name}" of non-null type "${inspect(argType)}" ` +
'must not be null.',
valueNode,
);
}
const coercedValue = valueFromAST(valueNode, argType, 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 new GraphQLError(
`Argument "${name}" has invalid value ${print(valueNode)}.`,
valueNode,
);
}
coercedValues[name] = coercedValue;
}
return coercedValues;
}
function hasOwnProperty(obj: any, prop: string): boolean {
return Object.prototype.hasOwnProperty.call(obj, prop);
}