1 | import { matcher } from 'micromatch';
|
2 | import { FilterRootFields, FilterObjectFields, FilterInputObjectFields } from '@graphql-tools/wrap';
|
3 | import { applySchemaTransforms, applyRequestTransforms, applyResultTransforms } from '@graphql-tools/utils';
|
4 |
|
5 | class FilterTransform {
|
6 | constructor(options) {
|
7 | this.transforms = [];
|
8 | const { config } = options;
|
9 | for (const filter of config) {
|
10 | const [typeName, fieldGlob] = filter.split('.');
|
11 | const isMatch = matcher(fieldGlob.trim());
|
12 | this.transforms.push(new FilterRootFields((rootTypeName, rootFieldName) => {
|
13 | if (rootTypeName === typeName) {
|
14 | return isMatch(rootFieldName);
|
15 | }
|
16 | return true;
|
17 | }));
|
18 | this.transforms.push(new FilterObjectFields((objectTypeName, objectFieldName) => {
|
19 | if (objectTypeName === typeName) {
|
20 | return isMatch(objectFieldName);
|
21 | }
|
22 | return true;
|
23 | }));
|
24 | this.transforms.push(new FilterInputObjectFields((inputObjectTypeName, inputObjectFieldName) => {
|
25 | if (inputObjectTypeName === typeName) {
|
26 | return isMatch(inputObjectFieldName);
|
27 | }
|
28 | return true;
|
29 | }));
|
30 | }
|
31 | }
|
32 | transformSchema(schema) {
|
33 | return applySchemaTransforms(schema, this.transforms);
|
34 | }
|
35 | transformRequest(request) {
|
36 | return applyRequestTransforms(request, this.transforms);
|
37 | }
|
38 | transformResult(result) {
|
39 | return applyResultTransforms(result, this.transforms);
|
40 | }
|
41 | }
|
42 |
|
43 | export default FilterTransform;
|
44 |
|