Skip to content

Commit 5b4ade3

Browse files
author
Bret Hubbard
committed
add includeFalsyKeys option
1 parent 5165bd7 commit 5b4ade3

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/__tests__/jsonToGraphQLQuery.tests.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,27 @@ describe('jsonToGraphQL()', () => {
405405
);
406406
});
407407

408+
it('includes fields with falsy values if includeFalsyKeys is true', () => {
409+
const query = {
410+
query: {
411+
Posts: {
412+
__args: {
413+
a: false
414+
},
415+
id: '',
416+
name: ''
417+
},
418+
Lorem: {
419+
id: ''
420+
},
421+
Ipsum: false
422+
}
423+
};
424+
expect(jsonToGraphQLQuery(query, { includeFalsyKeys: true })).to.equal(
425+
'query { Posts (a: false) { id name } Lorem { id } Ipsum }'
426+
);
427+
});
428+
408429
it('ignores a field that exists in the initial object', () => {
409430
const query = {
410431
query: {

src/jsonToGraphQLQuery.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ function convertQuery(node: any, level: number, output: Array<[string, number]>,
142142
output.push(['}', level]);
143143
}
144144

145-
} else if (node[key]) {
145+
} else if (options.includeFalsyKeys === true || node[key]) {
146146
output.push([`${key}`, level]);
147147
}
148148
});
@@ -151,6 +151,7 @@ function convertQuery(node: any, level: number, output: Array<[string, number]>,
151151
export interface IJsonToGraphQLOptions {
152152
pretty?: boolean;
153153
ignoreFields?: string[];
154+
includeFalsyKeys?: boolean;
154155
}
155156

156157
export function jsonToGraphQLQuery(query: any, options: IJsonToGraphQLOptions = {}) {

0 commit comments

Comments
 (0)