Skip to content

Commit 23c16ce

Browse files
author
Jakub Knejzlik
committed
Fix filter/args parsing for nested objects
1 parent cbec29e commit 23c16ce

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

index.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ function parceFind(_levelA) {
4343
//=====================================================
4444

4545
function getGraphQLValue(value) {
46-
if ("string" === typeof value) {
46+
if (value === null) {
47+
return value;
48+
} else if ("string" === typeof value) {
4749
value = JSON.stringify(value);
4850
} else if (Array.isArray(value)) {
4951
value = value
@@ -57,6 +59,7 @@ function getGraphQLValue(value) {
5759
value = value.toSource().slice(2,-2);
5860
else*/
5961
value = objectToString(value);
62+
if (value === "{}") return undefined;
6063
//console.error("No toSource!!",value);
6164
}
6265
return value;
@@ -70,7 +73,10 @@ function objectToString(obj) {
7073
continue;
7174
}
7275
// if ("object" === typeof obj[prop]) {
73-
sourceA.push(`${prop}:${getGraphQLValue(obj[prop])}`);
76+
let value = getGraphQLValue(obj[prop]);
77+
if (typeof value !== "undefined") {
78+
sourceA.push(`${prop}:${value}`);
79+
}
7480
// } else {
7581
// sourceA.push(`${prop}:${obj[prop]}`);
7682
// }
@@ -95,7 +101,9 @@ function Query(_fnNameS, _aliasS_OR_Filter) {
95101
if ("{}" === val) {
96102
continue;
97103
}
98-
this.headA.push(`${propS}:${val}`);
104+
if (typeof val !== "undefined") {
105+
this.headA.push(`${propS}:${val}`);
106+
}
99107
}
100108
return this;
101109
};

test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,22 @@ describe("graphql query builder", function() {
169169
expect(removeSpaces(expeted)).to.equal(removeSpaces(ItemQuery));
170170
});
171171

172+
it("should skip empty objects in filter/args for nested objects", function() {
173+
let expeted =
174+
'inventory(toy:"jack in the box",input:{blah:"foo",nullTest:null}) { id }';
175+
176+
let ChildsToy = {
177+
toy: "jack in the box",
178+
utils: {},
179+
input: { blah: "foo", nullTest: null, objectTest: {} }
180+
};
181+
182+
let ItemQuery = new Query("inventory", ChildsToy);
183+
ItemQuery.find("id");
184+
185+
expect(removeSpaces(expeted)).to.equal(removeSpaces(ItemQuery));
186+
});
187+
172188
it("should throw Error if find input items have zero props", function() {
173189
expect(() => new Query("x").find({})).to.throw(Error);
174190
});

0 commit comments

Comments
 (0)