@graphql-mesh/transform-filter-schema
Version:
107 lines (106 loc) • 5.45 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
const minimatch_1 = require("minimatch");
const utils_1 = require("@graphql-mesh/utils");
const utils_2 = require("@graphql-tools/utils");
const wrap_1 = require("@graphql-tools/wrap");
class WrapFilter {
constructor({ config: { filters, filterDeprecatedFields, filterDeprecatedTypes }, }) {
this.transforms = [];
for (const filter of filters) {
const [typeName, fieldNameOrGlob, argsGlob] = filter.split('.');
const typeMatcher = new minimatch_1.Minimatch(typeName);
// TODO: deprecate this in next major release as dscussed in #1605
if (!fieldNameOrGlob) {
this.transforms.push(new wrap_1.FilterTypes(type => {
return typeMatcher.match(type.name);
}));
continue;
}
let fixedFieldGlob = argsGlob || fieldNameOrGlob;
if (fixedFieldGlob.includes('{') && !fixedFieldGlob.includes(',')) {
fixedFieldGlob = fieldNameOrGlob.replace('{', '').replace('}', '');
}
fixedFieldGlob = fixedFieldGlob.split(', ').join(',');
const globalTypeMatcher = new minimatch_1.Minimatch(fixedFieldGlob.trim());
if (typeName === 'Type') {
this.transforms.push(new wrap_1.FilterTypes(type => {
return globalTypeMatcher.match(type.name);
}));
continue;
}
if (argsGlob) {
const fieldMatcher = new minimatch_1.Minimatch(fieldNameOrGlob);
this.transforms.push(new wrap_1.TransformCompositeFields((fieldTypeName, fieldName, fieldConfig) => {
if (typeMatcher.match(fieldTypeName) && fieldMatcher.match(fieldName)) {
const fieldArgs = Object.entries(fieldConfig.args).reduce((args, [argName, argConfig]) => !globalTypeMatcher.match(argName) ? args : { ...args, [argName]: argConfig }, {});
return { ...fieldConfig, args: fieldArgs };
}
return undefined;
}));
continue;
}
// If the glob is not for Types nor Args, finally we register Fields filters
this.transforms.push(new wrap_1.FilterRootFields((rootTypeName, rootFieldName) => {
if (typeMatcher.match(rootTypeName)) {
return globalTypeMatcher.match(rootFieldName);
}
return true;
}));
this.transforms.push(new wrap_1.FilterObjectFields((objectTypeName, objectFieldName) => {
if (typeMatcher.match(objectTypeName)) {
return globalTypeMatcher.match(objectFieldName);
}
return true;
}));
this.transforms.push(new wrap_1.FilterInputObjectFields((inputObjectTypeName, inputObjectFieldName) => {
if (typeMatcher.match(inputObjectTypeName)) {
return globalTypeMatcher.match(inputObjectFieldName);
}
return true;
}));
this.transforms.push(new wrap_1.FilterInterfaceFields((interfaceTypeName, interfaceFieldName) => {
if (typeMatcher.match(interfaceTypeName)) {
return globalTypeMatcher.match(interfaceFieldName);
}
return true;
}));
}
if (filterDeprecatedFields) {
this.transforms.push(new wrap_1.FilterRootFields((_, fieldName, fieldConfig) => {
return !fieldConfig.deprecationReason;
}));
this.transforms.push(new wrap_1.FilterObjectFields((_, fieldName, fieldConfig) => {
return !fieldConfig.deprecationReason;
}));
this.transforms.push(new wrap_1.FilterInputObjectFields((_, fieldName, fieldConfig) => {
return !fieldConfig.deprecationReason;
}));
this.transforms.push(new wrap_1.FilterInterfaceFields((_, fieldName, fieldConfig) => {
return !fieldConfig.deprecationReason;
}));
}
if (filterDeprecatedTypes) {
this.transforms.push(new wrap_1.FilterTypes(type => {
return !type.astNode?.directives?.some(directive => directive.name.value === 'deprecated');
}));
}
}
transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) {
let finalSchema = (0, utils_1.applySchemaTransforms)(originalWrappingSchema, subschemaConfig, transformedSchema, this.transforms);
finalSchema = (0, utils_2.mapSchema)(finalSchema, {
[utils_2.MapperKind.ROOT_OBJECT]: type => {
if (Object.keys(type.getFields()).length === 0)
return null;
},
});
return finalSchema;
}
transformRequest(originalRequest, delegationContext, transformationContext) {
return (0, utils_1.applyRequestTransforms)(originalRequest, delegationContext, transformationContext, this.transforms);
}
transformResult(originalResult, delegationContext, transformationContext) {
return (0, utils_1.applyResultTransforms)(originalResult, delegationContext, transformationContext, this.transforms);
}
}
exports.default = WrapFilter;
;