UNPKG

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