forked from open-source-labs/Quell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateQueryObj.js
37 lines (32 loc) · 1 KB
/
createQueryObj.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
createQueryObj takes in a map of field(keys) and true/false(values) creating an query object containing the fields (false) missing from cache. This will then be converted into a GQL query string in the next step.
*/
function createQueryObj(map) {
const output = {};
// !! assumes there is only ONE main query, and not multiples !!
for (let key in map) {
const reduced = reducer(map[key]);
if (reduced.length > 0) {
output[key] = reduced;
}
}
function reducer(obj) {
const fields = [];
for (let key in obj) {
// For each property, determine if the property is a false value...
if (obj[key] === false) fields.push(key);
// ...or another object type
if (typeof obj[key] === 'object') {
let newObjType = {};
let reduced = reducer(obj[key]);
if (reduced.length > 0) {
newObjType[key] = reduced;
fields.push(newObjType);
}
}
}
return fields;
}
return output;
};
module.exports = createQueryObj;