UNPKG

@graphql-mesh/transform-prefix

Version:
105 lines (100 loc) 4.33 kB
import { RenameTypes, RenameRootFields } from '@graphql-tools/wrap'; import { applySchemaTransforms, applyRequestTransforms, applyResultTransforms } from '@graphql-mesh/utils'; import { resolvers } from 'graphql-scalars'; import { isSpecifiedScalarType } from 'graphql'; import { mapSchema, MapperKind, renameType } from '@graphql-tools/utils'; class WrapPrefix { constructor(options) { this.transforms = []; const { apiName, config } = options; let prefix = null; if (config.value) { prefix = config.value; } else if (apiName) { prefix = `${apiName}_`; } if (!prefix) { throw new Error(`Transform 'prefix' has missing config: prefix`); } const ignoreList = [ ...(config.ignore || []), 'date', 'hostname', 'regex', 'json-pointer', 'relative-json-pointer', 'uri-reference', 'uri-template', ...Object.keys(resolvers), ]; const includeTypes = config.includeTypes !== false; if (includeTypes) { this.transforms.push(new RenameTypes(typeName => (ignoreList.includes(typeName) ? typeName : `${prefix}${typeName}`))); } const includeRootOperations = config.includeRootOperations === true; if (includeRootOperations) { this.transforms.push(new RenameRootFields((typeName, fieldName) => ignoreList.includes(typeName) || ignoreList.includes(`${typeName}.${fieldName}`) ? fieldName : `${prefix}${fieldName}`)); } } transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) { return applySchemaTransforms(originalWrappingSchema, subschemaConfig, transformedSchema, this.transforms); } transformRequest(originalRequest, delegationContext, transformationContext) { return applyRequestTransforms(originalRequest, delegationContext, transformationContext, this.transforms); } transformResult(originalResult, delegationContext, transformationContext) { return applyResultTransforms(originalResult, delegationContext, transformationContext, this.transforms); } } const rootOperations = new Set(['Query', 'Mutation', 'Subscription']); class BarePrefix { constructor(options) { this.noWrap = true; const { apiName, config } = options; this.ignoreList = config.ignore || []; 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.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 }, }), }); } } const PrefixTransform = (function PrefixTransform(options) { return options.config.mode === 'bare' ? new BarePrefix(options) : new WrapPrefix(options); }); export default PrefixTransform;