Skip to content

Commit 1d5ce73

Browse files
committed
Fix issue when nested arg is null
1 parent f343f69 commit 1d5ce73

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/__tests__/jsonToGraphQLQuery.tests.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,32 @@ describe('jsonToGraphQL()', () => {
120120
}`);
121121
});
122122

123+
it('converts a query with null arguments and nested nulls', () => {
124+
const query = {
125+
query: {
126+
Posts: {
127+
__args: {
128+
where: {
129+
id: null,
130+
},
131+
orderBy: null
132+
},
133+
id: true,
134+
title: true,
135+
post_date: true
136+
}
137+
}
138+
} as any;
139+
expect(jsonToGraphQLQuery(query, { pretty: true })).to.equal(
140+
`query {
141+
Posts (where: {id: null}, orderBy: null) {
142+
id
143+
title
144+
post_date
145+
}
146+
}`);
147+
});
148+
123149
it('converts a query with nested objects', () => {
124150
const query = {
125151
query: {

src/jsonToGraphQLQuery.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
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') {
4+
if (typeof obj_from_json !== 'object' || obj_from_json === null) {
55
// not an object, stringify using native function
66
return JSON.stringify(obj_from_json);
77
}

0 commit comments

Comments
 (0)