-
-
Notifications
You must be signed in to change notification settings - Fork 807
/
filter-document-kind.ts
44 lines (41 loc) · 1.08 KB
/
filter-document-kind.ts
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
import { env } from 'process';
import { DefinitionNode, DocumentNode, Kind } from 'graphql';
/**
* @internal
*/
export const filterKind = (
content: DocumentNode | undefined,
filterKinds: null | string[],
): DocumentNode | undefined => {
if (
content &&
content.definitions &&
content.definitions.length &&
filterKinds &&
filterKinds.length > 0
) {
const invalidDefinitions: DefinitionNode[] = [];
const validDefinitions: DefinitionNode[] = [];
for (const definitionNode of content.definitions) {
if (filterKinds.includes(definitionNode.kind)) {
invalidDefinitions.push(definitionNode);
} else {
validDefinitions.push(definitionNode);
}
}
if (invalidDefinitions.length > 0) {
if (env['DEBUG']) {
for (const d of invalidDefinitions) {
console.log(
`Filtered document of kind ${d.kind} due to filter policy (${filterKinds.join(', ')})`,
);
}
}
}
return {
kind: Kind.DOCUMENT,
definitions: validDefinitions,
};
}
return content;
};