UNPKG

1.48 kBJavaScriptView Raw
1import { keyValMap } from '../jsutils/keyValMap.mjs';
2import { Kind } from '../language/kinds.mjs';
3/**
4 * Produces a JavaScript value given a GraphQL Value AST.
5 *
6 * Unlike `valueFromAST()`, no type is provided. The resulting JavaScript value
7 * will reflect the provided GraphQL value AST.
8 *
9 * | GraphQL Value | JavaScript Value |
10 * | -------------------- | ---------------- |
11 * | Input Object | Object |
12 * | List | Array |
13 * | Boolean | Boolean |
14 * | String / Enum | String |
15 * | Int / Float | Number |
16 * | Null | null |
17 *
18 */
19
20export function valueFromASTUntyped(valueNode, variables) {
21 switch (valueNode.kind) {
22 case Kind.NULL:
23 return null;
24
25 case Kind.INT:
26 return parseInt(valueNode.value, 10);
27
28 case Kind.FLOAT:
29 return parseFloat(valueNode.value);
30
31 case Kind.STRING:
32 case Kind.ENUM:
33 case Kind.BOOLEAN:
34 return valueNode.value;
35
36 case Kind.LIST:
37 return valueNode.values.map((node) =>
38 valueFromASTUntyped(node, variables),
39 );
40
41 case Kind.OBJECT:
42 return keyValMap(
43 valueNode.fields,
44 (field) => field.name.value,
45 (field) => valueFromASTUntyped(field.value, variables),
46 );
47
48 case Kind.VARIABLE:
49 return variables === null || variables === void 0
50 ? void 0
51 : variables[valueNode.name.value];
52 }
53}