graphql-modules
Version:
Create reusable, maintainable, testable and extendable GraphQL modules
44 lines (43 loc) • 2.05 kB
JavaScript
import { execute, } from 'graphql';
import { isNotSchema } from '../shared/utils';
export function executionCreator({ contextBuilder, }) {
const createExecution = (options) => {
// Custom or original execute function
const executeFn = (options === null || options === void 0 ? void 0 : options.execute) || execute;
return (argsOrSchema, document, rootValue, contextValue, variableValues, operationName, fieldResolver, typeResolver) => {
function perform({ context, ɵdestroy: destroy, }) {
const executionArgs = isNotSchema(argsOrSchema)
? {
...argsOrSchema,
contextValue: context,
}
: {
schema: argsOrSchema,
document: document,
rootValue,
contextValue: context,
variableValues,
operationName,
fieldResolver,
typeResolver,
};
// It's important to wrap the executeFn within a promise
// so we can easily control the end of execution (with finally)
return Promise.resolve()
.then(() => executeFn(executionArgs))
.finally(destroy);
}
if (options === null || options === void 0 ? void 0 : options.controller) {
// Wrap `perform` in the controller's own ALS frame so that
// `@ExecutionContext()` reads inside the resolver — including
// after an `await`.
const controller = options.controller;
return controller.ɵrunWithContext(() => perform(controller));
}
return contextBuilder(isNotSchema(argsOrSchema)
? argsOrSchema.contextValue
: contextValue).runWithContext(perform);
};
};
return createExecution;
}