Skip to content

Commit 38165a5

Browse files
ivnivnchmickhansen
authored andcommitted
simplifyValue (#259)
* simplifyValue * add unit-tests * fix unit-tests
1 parent 4486f10 commit 38165a5

File tree

2 files changed

+67
-5
lines changed

2 files changed

+67
-5
lines changed

src/simplifyAST.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@ function isFragment(info, ast) {
2626
return hasFragments(info) && info.fragments[ast.name.value] && ast.kind !== 'FragmentDefinition';
2727
}
2828

29+
function simplifyValue(value, info) {
30+
if (value.values) {
31+
return value.values.map(value => simplifyValue(value, info));
32+
}
33+
if ('value' in value) {
34+
return value.value;
35+
}
36+
if (value.name) {
37+
return info.variableValues[value.name.value];
38+
}
39+
}
40+
2941
module.exports = function simplifyAST(ast, info, parent) {
3042
var selections;
3143
info = info || {};
@@ -73,11 +85,7 @@ module.exports = function simplifyAST(ast, info, parent) {
7385
}
7486

7587
simpleAST.fields[key].args = selection.arguments.reduce(function (args, arg) {
76-
if (arg.value.values) {
77-
args[arg.name.value] = arg.value.values.map(value => value.value);
78-
} else {
79-
args[arg.name.value] = arg.value.value;
80-
}
88+
args[arg.name.value] = simplifyValue(arg.value, info);
8189
return args;
8290
}, {});
8391

test/unit/simplifyAST.test.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,60 @@ describe('simplifyAST', function () {
9696
})
9797
});
9898

99+
it('should simplify a basic structure with nested array args', function () {
100+
expect(simplifyAST(parse(`
101+
{
102+
user(units: ["1", "2", ["3", ["4"], [["5"], "6"], "7"]]) {
103+
name
104+
}
105+
}
106+
`))).to.deep.equal({
107+
args: {},
108+
fields: {
109+
user: {
110+
args: {
111+
units: ["1", "2", ["3", ["4"], [["5"], "6"], "7"]]
112+
},
113+
fields: {
114+
name: {
115+
args: {},
116+
fields: {}
117+
}
118+
}
119+
}
120+
}
121+
})
122+
});
123+
124+
it('should simplify a basic structure with variable args', function () {
125+
expect(simplifyAST(parse(`
126+
{
127+
user(id: $id) {
128+
name
129+
}
130+
}
131+
`), {
132+
variableValues: {
133+
id: "1"
134+
}
135+
})).to.deep.equal({
136+
args: {},
137+
fields: {
138+
user: {
139+
args: {
140+
id: "1"
141+
},
142+
fields: {
143+
name: {
144+
args: {},
145+
fields: {}
146+
}
147+
}
148+
}
149+
}
150+
})
151+
});
152+
99153
it('should simplify a basic structure with an inline fragment', function () {
100154
expect(simplifyAST(parse(`
101155
{

0 commit comments

Comments
 (0)