UNPKG

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