@graphql-mesh/transform-prefix
Version:
61 lines (60 loc) • 2.75 kB
JavaScript
import { isSpecifiedScalarType, } from 'graphql';
import { MapperKind, mapSchema, renameType } from '@graphql-tools/utils';
import { ignoreList as defaultIgnoreList } from './shared.js';
const rootOperations = new Set(['Query', 'Mutation', 'Subscription']);
export default class BarePrefix {
constructor(options) {
this.noWrap = true;
const { apiName, config } = options;
this.ignoreList = [...(config.ignore || []), ...defaultIgnoreList];
this.includeRootOperations = config.includeRootOperations === true;
this.includeTypes = config.includeTypes !== false;
this.prefix = null;
if (config.value) {
this.prefix = config.value;
}
else if (apiName) {
this.prefix = `${apiName}_`;
}
if (!this.prefix) {
throw new Error(`Transform 'prefix' has missing config: prefix`);
}
}
transformSchema(schema) {
return mapSchema(schema, {
[MapperKind.TYPE]: (type) => {
if (this.includeTypes && !isSpecifiedScalarType(type)) {
const currentName = type.name;
if (!this.ignoreList.includes(currentName)) {
return renameType(type, this.prefix + currentName);
}
}
return undefined;
},
[MapperKind.ABSTRACT_TYPE]: type => {
if (this.includeTypes && !isSpecifiedScalarType(type)) {
const existingResolver = type.resolveType;
type.resolveType = async (data, context, info, abstractType) => {
const typeName = await existingResolver(data, context, info, abstractType);
return this.prefix + typeName;
};
const currentName = type.name;
return renameType(type, this.prefix + currentName);
}
return undefined;
},
[MapperKind.ROOT_OBJECT]() {
return undefined;
},
...(this.includeRootOperations && {
[MapperKind.COMPOSITE_FIELD]: (fieldConfig, fieldName, typeName) => {
return !rootOperations.has(typeName) || // check we're in a root Type
this.ignoreList.includes(typeName) || // check if type is to be ignored
this.ignoreList.includes(`${typeName}.${fieldName}`) // check if field in type is to be ignored
? undefined // do not perform any change
: [`${this.prefix}${fieldName}`, fieldConfig]; // apply prefix
},
}),
});
}
}