Skip to content

Commit bc3541b

Browse files
JohnyDaysmickhansen
authored andcommitted
Support where option from arguments, by parsing JSON arguments in simplifyAST (#267)
Where option wasn't actually working in my case, had to make this change to the argument parsing. Cheers
1 parent 38165a5 commit bc3541b

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/simplifyAST.js

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

29+
function simplifyObjectValue(objectValue) {
30+
return objectValue.fields.reduce((memo, field) => {
31+
memo[field.name.value] =
32+
field.value.kind === 'IntValue' ? parseInt( field.value.value, 10 ) :
33+
field.value.kind === 'FloatValue' ? parseFloat( field.value.value ) :
34+
field.value.kind === 'ObjectValue' ? simplifyObjectValue( field.value ) :
35+
field.value.value;
36+
37+
return memo;
38+
}, {});
39+
}
40+
2941
function simplifyValue(value, info) {
3042
if (value.values) {
3143
return value.values.map(value => simplifyValue(value, info));
3244
}
3345
if ('value' in value) {
3446
return value.value;
3547
}
48+
if (value.kind === 'ObjectValue') {
49+
return simplifyObjectValue(value);
50+
}
3651
if (value.name) {
3752
return info.variableValues[value.name.value];
3853
}

test/unit/simplifyAST.test.js

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

99+
it('should simplify a basic structure with object args', function () {
100+
expect(simplifyAST(parse(`
101+
{
102+
luke: human(contact: { phone: "91264646" }) {
103+
name
104+
}
105+
}
106+
`))).to.deep.equal({
107+
args: {},
108+
fields: {
109+
luke: {
110+
key: "human",
111+
args: {
112+
contact: { phone: "91264646" }
113+
},
114+
fields: {
115+
name: {
116+
args: {},
117+
fields: {}
118+
}
119+
}
120+
}
121+
}
122+
})
123+
});
124+
99125
it('should simplify a basic structure with nested array args', function () {
100126
expect(simplifyAST(parse(`
101127
{

0 commit comments

Comments
 (0)