UNPKG

@graphql-mesh/transform-encapsulate

Version:
53 lines (49 loc) 2.39 kB
'use strict'; const wrap = require('@graphql-tools/wrap'); const utils = require('@graphql-tools/utils'); const utils$1 = require('@graphql-mesh/utils'); const DEFUALT_APPLY_TO = { query: true, mutation: true, subscription: true, }; class EncapsulateTransform { constructor(options) { this.transformMap = {}; this.transforms = []; const config = options.config; const name = (config === null || config === void 0 ? void 0 : config.name) || options.apiName; if (!name) { throw new Error(`Unable to execute encapsulate transform without a name. Please make sure to use it over a specific schema, or specify a name in your configuration!`); } const applyTo = { ...DEFUALT_APPLY_TO, ...((config === null || config === void 0 ? void 0 : config.applyTo) || {}) }; if (applyTo.query) { this.transformMap.Query = new wrap.WrapType('Query', `${name}Query`, name); } if (applyTo.mutation) { this.transformMap.Mutation = new wrap.WrapType('Mutation', `${name}Mutation`, name); } if (applyTo.subscription) { this.transformMap.Subscription = new wrap.WrapType('Subscription', `${name}Subscription`, name); } } *generateSchemaTransforms(originalWrappingSchema) { for (const typeName of Object.keys(this.transformMap)) { const fieldConfigMap = utils.selectObjectFields(originalWrappingSchema, typeName, () => true); if (Object.keys(fieldConfigMap).length) { yield this.transformMap[typeName]; } } } transformSchema(originalWrappingSchema, subschemaConfig, transformedSchema) { this.transforms = [...this.generateSchemaTransforms(originalWrappingSchema)]; return utils$1.applySchemaTransforms(originalWrappingSchema, subschemaConfig, transformedSchema, this.transforms); } transformRequest(originalRequest, delegationContext, transformationContext) { return utils$1.applyRequestTransforms(originalRequest, delegationContext, transformationContext, this.transforms); } transformResult(originalResult, delegationContext, transformationContext) { return utils$1.applyResultTransforms(originalResult, delegationContext, transformationContext, this.transforms); } } module.exports = EncapsulateTransform;