Skip to content

Commit 04274ae

Browse files
authored
fix: Handle deep nested falsy values in result (#68)
Handled deeply nested falsy values in result. This meant backtracking on parent keys.
1 parent 4416738 commit 04274ae

File tree

5 files changed

+308
-112
lines changed

5 files changed

+308
-112
lines changed

src/__tests__/falsykeys.tests.ts

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
21
import { expect } from 'chai';
32
import { jsonToGraphQLQuery } from '../';
43

54
describe('jsonToGraphQLQuery() - falsy keys', () => {
6-
75
it('does not include fields which value is false', () => {
86
const query = {
97
query: {
@@ -82,4 +80,86 @@ describe('jsonToGraphQLQuery() - falsy keys', () => {
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
});

src/__tests__/mutations.tests.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
1-
21
import { expect } from 'chai';
32
import { jsonToGraphQLQuery } from '../';
43

54
describe('jsonToGraphQLQuery() - mutations', () => {
6-
75
it('simple mutation', () => {
86
const mutation = {
97
mutation: {
108
delete_post: {
119
__args: { id: 1234 },
12-
id: true,
10+
id: true
1311
}
1412
}
1513
};
1614
expect(jsonToGraphQLQuery(mutation, { pretty: true })).to.equal(
17-
`mutation {
15+
`mutation {
1816
delete_post (id: 1234) {
1917
id
2018
}
21-
}`);
22-
})
19+
}`
20+
);
21+
});
2322

2423
it('correctly converts mutations with no specified return fields', () => {
2524
const query = {
@@ -35,7 +34,7 @@ describe('jsonToGraphQLQuery() - mutations', () => {
3534
expect(jsonToGraphQLQuery(query, { pretty: true })).to.equal(
3635
`mutation {
3736
create_post (title: "My Awesome Post", body: "This post is awesome!")
38-
}`);
37+
}`
38+
);
3939
});
40-
4140
});

src/__tests__/name.tests.ts

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@ describe('jsonToGraphQLQuery() - name', () => {
99
lorem: {
1010
__aliasFor: 'Posts',
1111
__args: {
12-
arg1: 20,
12+
arg1: 20
1313
},
14-
id: true,
14+
id: true
1515
},
1616
larem: {
1717
__aliasFor: 'Posts',
1818
__args: {
19-
arg2: 10,
19+
arg2: 10
2020
},
21-
id: true,
22-
},
23-
},
21+
id: true
22+
}
23+
}
2424
};
2525
expect(jsonToGraphQLQuery(query)).to.equal(
2626
'query NewName { lorem: Posts (arg1: 20) { id } larem: Posts (arg2: 10) { id } }'
@@ -34,18 +34,18 @@ describe('jsonToGraphQLQuery() - name', () => {
3434
one: {
3535
__aliasFor: 'Posts',
3636
__args: {
37-
arg1: 20,
37+
arg1: 20
3838
},
39-
id: true,
39+
id: true
4040
},
4141
two: {
4242
__aliasFor: 'Posts',
4343
__args: {
44-
arg2: 10,
44+
arg2: 10
4545
},
46-
id: true,
47-
},
48-
},
46+
id: true
47+
}
48+
}
4949
};
5050
expect(jsonToGraphQLQuery(query)).to.equal(
5151
'mutation NewName { one: Posts (arg1: 20) { id } two: Posts (arg2: 10) { id } }'
@@ -62,12 +62,12 @@ describe('jsonToGraphQLQuery() - combinations', () => {
6262
Posts: {
6363
__args: {
6464
arg1: 20,
65-
arg2: new VariableType('variable1'),
65+
arg2: new VariableType('variable1')
6666
},
6767
id: true,
68-
title: true,
69-
},
70-
},
68+
title: true
69+
}
70+
}
7171
};
7272
expect(jsonToGraphQLQuery(query, { pretty: true })).to.equal(
7373
`query NewName {
@@ -79,24 +79,23 @@ describe('jsonToGraphQLQuery() - combinations', () => {
7979
);
8080
});
8181

82-
8382
it('correctly converts query with name/variables', () => {
8483
const query = {
8584
query: {
8685
__name: 'NewName',
8786
__variables: {
8887
variable1: 'String!',
89-
variableWithDefault: 'String = "default_value"',
88+
variableWithDefault: 'String = "default_value"'
9089
},
9190
Posts: {
9291
__args: {
9392
arg1: 20,
94-
arg2: new VariableType('variable1'),
93+
arg2: new VariableType('variable1')
9594
},
9695
id: true,
97-
title: true,
98-
},
99-
},
96+
title: true
97+
}
98+
}
10099
};
101100
expect(jsonToGraphQLQuery(query, { pretty: true })).to.equal(
102101
`query NewName ($variable1: String!, $variableWithDefault: String = "default_value") {
@@ -114,65 +113,65 @@ describe('jsonToGraphQLQuery() - combinations', () => {
114113
__name: 'NewName',
115114
__variables: {
116115
someString: 'String!',
117-
varWithDefault: 'String = "default_value"',
116+
varWithDefault: 'String = "default_value"'
118117
},
119118
one: {
120119
__aliasFor: 'Posts',
121120
__args: {
122121
arg1: 20,
123122
arg2: new VariableType('someString'),
124-
status: new EnumType('PUBLISHED'),
123+
status: new EnumType('PUBLISHED')
125124
},
126125
name: false,
127126
id: true,
128127
title: true,
129128
comments: {
130129
__args: {
131-
offensiveOnly: true,
130+
offensiveOnly: true
132131
},
133132
id: true,
134133
comment: true,
135-
user: true,
136-
},
134+
user: true
135+
}
137136
},
138137
Post: {
139138
__args: {
140139
arg1: 20,
141-
arg2: new VariableType('someString'),
140+
arg2: new VariableType('someString')
142141
},
143142
__on: {
144143
__typeName: 'ConfigurablePost',
145-
id: true,
144+
id: true
146145
},
147146
name: false,
148147
title: true,
149148
comments: {
150149
__args: {
151-
offensiveOnly: true,
150+
offensiveOnly: true
152151
},
153152
id: true,
154153
comment: true,
155-
user: true,
156-
},
154+
user: true
155+
}
157156
},
158157
Posts: {
159158
__args: {
160159
arg1: 20,
161-
arg2: new VariableType('someString'),
160+
arg2: new VariableType('someString')
162161
},
163162
name: false,
164163
id: true,
165164
title: true,
166165
comments: {
167166
__args: {
168-
offensiveOnly: true,
167+
offensiveOnly: true
169168
},
170169
id: true,
171170
comment: true,
172-
user: true,
173-
},
174-
},
175-
},
171+
user: true
172+
}
173+
}
174+
}
176175
};
177176
expect(jsonToGraphQLQuery(query, { pretty: true })).to.equal(
178177
`query NewName ($someString: String!, $varWithDefault: String = "default_value") {

0 commit comments

Comments
 (0)