-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathindex.js
67 lines (61 loc) · 2.25 KB
/
index.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
function getBooleanArgumentValue(info, ast) {
const argument = ast.arguments[0].value;
switch (argument.kind) {
case 'BooleanValue':
return argument.value;
case 'Variable':
return info.variableValues[argument.name.value];
}
}
function isExcludedByDirective(info, ast) {
const directives = ast.directives || [];
let isExcluded = false;
directives.forEach((directive) => {
switch (directive.name.value) {
case 'include':
isExcluded = isExcluded || !getBooleanArgumentValue(info, directive);
break;
case 'skip':
isExcluded = isExcluded || getBooleanArgumentValue(info, directive);
break;
}
});
return isExcluded;
}
function dotConcat(a, b) {
return a ? `${a}.${b}` : b;
}
function getFieldSet(info, asts = info.fieldASTs || info.fieldNodes, prefix = '', maxDepth) {
// for recursion: fragments doesn't have many sets
if (!Array.isArray(asts)) {
asts = [asts];
}
const selections = asts.reduce((selections, source) => {
if (source && source.selectionSet && source.selectionSet.selections) {
selections.push(...source.selectionSet.selections);
}
return selections;
}, []);
return selections.reduce((set, ast) => {
if (isExcludedByDirective(info, ast)) {
return set;
}
switch (ast.kind) {
case 'Field':
const newPrefix = dotConcat(prefix, ast.name.value);
if (ast.selectionSet && maxDepth > 1) {
return Object.assign({}, set, getFieldSet(info, ast, newPrefix, maxDepth - 1));
} else {
set[newPrefix] = true;
return set;
}
case 'InlineFragment':
return Object.assign({}, set, getFieldSet(info, ast, prefix, maxDepth));
case 'FragmentSpread':
return Object.assign({}, set, getFieldSet(info, info.fragments[ast.name.value], prefix, maxDepth));
}
}, {});
}
module.exports = function getFieldList(info, maxDepth = Number.MAX_SAFE_INTEGER) {
return Object.keys(getFieldSet(info, undefined, undefined, maxDepth));
};