Skip to content

Commit 586db14

Browse files
committed
fix: Handle deep nested falsy values in result
1 parent 4416738 commit 586db14

File tree

2 files changed

+291
-91
lines changed

2 files changed

+291
-91
lines changed

src/__tests__/falsykeys.tests.ts

Lines changed: 112 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,165 @@
1+
import { expect } from "chai";
2+
import { jsonToGraphQLQuery } from "../";
13

2-
import { expect } from 'chai';
3-
import { jsonToGraphQLQuery } from '../';
4-
5-
describe('jsonToGraphQLQuery() - falsy keys', () => {
6-
7-
it('does not include fields which value is false', () => {
4+
describe("jsonToGraphQLQuery() - falsy keys", () => {
5+
it("does not include fields which value is false", () => {
86
const query = {
97
query: {
108
Posts: {
119
__args: {
12-
a: false
10+
a: false,
1311
},
1412
id: true,
15-
name: false
13+
name: false,
1614
},
1715
Lorem: {
18-
id: true
16+
id: true,
1917
},
20-
Ipsum: false
21-
}
18+
Ipsum: false,
19+
},
2220
};
2321
expect(jsonToGraphQLQuery(query)).to.equal(
24-
'query { Posts (a: false) { id } Lorem { id } }'
22+
"query { Posts (a: false) { id } Lorem { id } }",
2523
);
2624
});
2725

28-
it('includes fields with falsy values if includeFalsyKeys is true', () => {
26+
it("includes fields with falsy values if includeFalsyKeys is true", () => {
2927
const query = {
3028
query: {
3129
Posts: {
3230
__args: {
33-
a: false
31+
a: false,
3432
},
35-
id: '',
36-
name: ''
33+
id: "",
34+
name: "",
3735
},
3836
Lorem: {
39-
id: ''
37+
id: "",
4038
},
41-
Ipsum: false
42-
}
39+
Ipsum: false,
40+
},
4341
};
4442
expect(jsonToGraphQLQuery(query, { includeFalsyKeys: true })).to.equal(
45-
'query { Posts (a: false) { id name } Lorem { id } Ipsum }'
43+
"query { Posts (a: false) { id name } Lorem { id } Ipsum }",
4644
);
4745
});
4846

49-
it('does not include object with only false keys', () => {
47+
it("does not include object with only false keys", () => {
5048
const query = {
5149
query: {
5250
Posts: {
5351
id: true,
54-
name: false
52+
name: false,
5553
},
5654
Lorem: {
57-
id: false
55+
id: false,
5856
},
59-
Ipsum: true
60-
}
57+
Ipsum: true,
58+
},
6159
};
6260
expect(jsonToGraphQLQuery(query)).to.equal(
63-
'query { Posts { id } Ipsum }'
61+
"query { Posts { id } Ipsum }",
6462
);
6563
});
6664

67-
it('does include object with only false keys if includeFalsyKeys is true', () => {
65+
it("does include object with only false keys if includeFalsyKeys is true", () => {
6866
const query = {
6967
query: {
7068
Posts: {
7169
id: true,
72-
name: false
70+
name: false,
7371
},
7472
Lorem: {
75-
id: false
73+
id: false,
7674
},
77-
Ipsum: true
78-
}
75+
Ipsum: true,
76+
},
7977
};
8078
expect(jsonToGraphQLQuery(query, { includeFalsyKeys: true })).to.equal(
81-
'query { Posts { id name } Lorem { id } Ipsum }'
79+
"query { Posts { id name } Lorem { id } Ipsum }",
8280
);
8381
});
8482

83+
it("Includes the nested object if includeFalsyKeys is true", () => {
84+
const query = {
85+
query: {
86+
Posts: {
87+
id: true,
88+
name: false,
89+
},
90+
Lorem: {
91+
Ipsum: {
92+
name: false,
93+
},
94+
},
95+
},
96+
};
97+
expect(jsonToGraphQLQuery(query, { includeFalsyKeys: true })).to.equal(
98+
"query { Posts { id name } Lorem { Ipsum { name } } }",
99+
);
100+
});
101+
102+
it("does not include the object if nested object has falsy values", () => {
103+
const query = {
104+
query: {
105+
Posts: {
106+
id: true,
107+
name: false,
108+
},
109+
Lorem: {
110+
Ipsum: {
111+
name: false,
112+
},
113+
},
114+
},
115+
};
116+
expect(jsonToGraphQLQuery(query)).to.equal("query { Posts { id } }");
117+
});
118+
119+
it("skip objects when deeply nested keys contain falsy values", () => {
120+
const query = {
121+
query: {
122+
id: true,
123+
Posts: {
124+
id: true,
125+
name: false,
126+
},
127+
Lorem: {
128+
Ipsum: {
129+
Dolor: {
130+
Sit: {
131+
amet: false,
132+
},
133+
},
134+
},
135+
details: {
136+
name: false,
137+
address: true,
138+
},
139+
},
140+
},
141+
};
142+
expect(jsonToGraphQLQuery(query)).to.equal(
143+
"query { id Posts { id } Lorem { details { address } } }",
144+
);
145+
});
146+
147+
it("Include values if nested object has falsy values and includeFalsyKeys is true", () => {
148+
const query = {
149+
query: {
150+
Posts: {
151+
id: true,
152+
name: false,
153+
},
154+
Lorem: {
155+
Ipsum: {
156+
name: false,
157+
},
158+
},
159+
},
160+
};
161+
expect(jsonToGraphQLQuery(query, { includeFalsyKeys: true })).to.equal(
162+
"query { Posts { id name } Lorem { Ipsum { name } } }",
163+
);
164+
});
85165
});

0 commit comments

Comments
 (0)