Skip to content

Commit 59a236c

Browse files
committed
Correctly convert arguments with nested arrays
1 parent 34aa001 commit 59a236c

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

src/__tests__/jsonToGraphQLQuery.tests.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,33 @@ describe('jsonToGraphQL()', () => {
9393
}`);
9494
});
9595

96+
it('converts a query with JSON arguments containing arrays of objects', () => {
97+
const query = {
98+
query: {
99+
Posts: {
100+
__args: {
101+
or: [
102+
{ published: true },
103+
{ rating: [{ _gt: 3 }] }
104+
],
105+
orderBy: 'post_date'
106+
},
107+
id: true,
108+
title: true,
109+
post_date: true
110+
}
111+
}
112+
};
113+
expect(jsonToGraphQLQuery(query, { pretty: true })).to.equal(
114+
`query {
115+
Posts (or: [{published: true},{rating: [{_gt: 3}]}], orderBy: "post_date") {
116+
id
117+
title
118+
post_date
119+
}
120+
}`);
121+
});
122+
96123
it('converts a query with nested objects', () => {
97124
const query = {
98125
query: {

src/jsonToGraphQLQuery.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11

2-
function stringify(obj_from_json: any) {
2+
function stringify(obj_from_json: any): string {
33
// Cheers to Derek: https://stackoverflow.com/questions/11233498/json-stringify-without-quotes-on-properties
4-
if (typeof obj_from_json !== 'object' || Array.isArray(obj_from_json)) {
4+
if (typeof obj_from_json !== 'object') {
55
// not an object, stringify using native function
66
return JSON.stringify(obj_from_json);
77
}
8+
else if (Array.isArray(obj_from_json)) {
9+
return `[${obj_from_json.map((item) => stringify(item)).join(',')}]`;
10+
}
811
// Implements recursive object serialization according to JSON spec
912
// but without quotes around the keys.
1013
const props: string = Object

0 commit comments

Comments
 (0)