UNPKG

2.68 kBJavaScriptView Raw
1import { matcher } from 'micromatch';
2import { FilterTypes, FilterRootFields, FilterObjectFields, FilterInputObjectFields } from '@graphql-tools/wrap';
3import { applySchemaTransforms, applyRequestTransforms, applyResultTransforms } from '@graphql-mesh/utils';
4
5class FilterTransform {
6 constructor(options) {
7 this.transforms = [];
8 const { config } = options;
9 for (const filter of config) {
10 const [typeName, fieldGlob] = filter.split('.');
11 if (!fieldGlob) {
12 const isMatch = matcher(typeName);
13 this.transforms.push(new FilterTypes(type => {
14 return !isMatch(type.name);
15 }));
16 }
17 else {
18 let fixedFieldGlob = fieldGlob;
19 if (fixedFieldGlob.includes('{') && !fixedFieldGlob.includes(',')) {
20 fixedFieldGlob = fieldGlob.replace('{', '').replace('}', '');
21 }
22 fixedFieldGlob = fixedFieldGlob.split(', ').join(',');
23 const isMatch = matcher(fixedFieldGlob.trim());
24 this.transforms.push(new FilterRootFields((rootTypeName, rootFieldName) => {
25 if (rootTypeName === typeName) {
26 return isMatch(rootFieldName);
27 }
28 return true;
29 }));
30 this.transforms.push(new FilterObjectFields((objectTypeName, objectFieldName) => {
31 if (objectTypeName === typeName) {
32 return isMatch(objectFieldName);
33 }
34 return true;
35 }));
36 this.transforms.push(new FilterInputObjectFields((inputObjectTypeName, inputObjectFieldName) => {
37 if (inputObjectTypeName === typeName) {
38 return isMatch(inputObjectFieldName);
39 }
40 return true;
41 }));
42 }
43 }
44 }
45 transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
46 return applySchemaTransforms(originalWrappingSchema, subschemaConfig, transformedSchema, this.transforms);
47 }
48 transformRequest(originalRequest, delegationContext, transformationContext) {
49 return applyRequestTransforms(originalRequest, delegationContext, transformationContext, this.transforms);
50 }
51 transformResult(originalResult, delegationContext, transformationContext) {
52 return applyResultTransforms(originalResult, delegationContext, transformationContext, this.transforms);
53 }
54}
55
56export default FilterTransform;
57//# sourceMappingURL=index.esm.js.map