@graphql-tools/wrap
Version:
A set of utils for faster development of GraphQL tools
32 lines (31 loc) • 1.69 kB
JavaScript
import { isInterfaceType } from 'graphql';
import TransformCompositeFields from './TransformCompositeFields.js';
export default class TransformInterfaceFields {
constructor(interfaceFieldTransformer, fieldNodeTransformer) {
this.interfaceFieldTransformer = interfaceFieldTransformer;
this.fieldNodeTransformer = fieldNodeTransformer;
}
_getTransformer() {
const transformer = this.transformer;
if (transformer === undefined) {
throw new Error(`The TransformInterfaceFields transform's "transformRequest" and "transformResult" methods cannot be used without first calling "transformSchema".`);
}
return transformer;
}
transformSchema(originalWrappingSchema, subschemaConfig) {
const compositeToObjectFieldTransformer = (typeName, fieldName, fieldConfig) => {
if (isInterfaceType(originalWrappingSchema.getType(typeName))) {
return this.interfaceFieldTransformer(typeName, fieldName, fieldConfig);
}
return undefined;
};
this.transformer = new TransformCompositeFields(compositeToObjectFieldTransformer, this.fieldNodeTransformer);
return this.transformer.transformSchema(originalWrappingSchema, subschemaConfig);
}
transformRequest(originalRequest, delegationContext, transformationContext) {
return this._getTransformer().transformRequest(originalRequest, delegationContext, transformationContext);
}
transformResult(originalResult, delegationContext, transformationContext) {
return this._getTransformer().transformResult(originalResult, delegationContext, transformationContext);
}
}