graphql-modules
Version:
Create reusable, maintainable, testable and extendable GraphQL modules
40 lines (39 loc) • 1.77 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) {
return perform(options.controller);
}
return contextBuilder(isNotSchema(argsOrSchema)
? argsOrSchema.contextValue
: contextValue).runWithContext(perform);
};
};
return createExecution;
}