UNPKG

1.51 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.extractVariables = void 0;
4const graphql_1 = require("graphql");
5function extractVariables(inputValue) {
6 const path = [];
7 const variablePaths = Object.create(null);
8 const keyPathVisitor = {
9 enter: (_node, key) => {
10 if (typeof key === 'number') {
11 path.push(key);
12 }
13 },
14 leave: (_node, key) => {
15 if (typeof key === 'number') {
16 path.pop();
17 }
18 },
19 };
20 const fieldPathVisitor = {
21 enter: (node) => {
22 path.push(node.name.value);
23 },
24 leave: () => {
25 path.pop();
26 },
27 };
28 const variableVisitor = {
29 enter: (node, key) => {
30 if (typeof key === 'number') {
31 variablePaths[node.name.value] = path.concat([key]);
32 }
33 else {
34 variablePaths[node.name.value] = path.slice();
35 }
36 return {
37 kind: graphql_1.Kind.NULL,
38 };
39 },
40 };
41 const newInputValue = (0, graphql_1.visit)(inputValue, {
42 [graphql_1.Kind.OBJECT]: keyPathVisitor,
43 [graphql_1.Kind.LIST]: keyPathVisitor,
44 [graphql_1.Kind.OBJECT_FIELD]: fieldPathVisitor,
45 [graphql_1.Kind.VARIABLE]: variableVisitor,
46 });
47 return {
48 inputValue: newInputValue,
49 variablePaths,
50 };
51}
52exports.extractVariables = extractVariables;