graphql-modules
Version:
Create reusable, maintainable, testable and extendable GraphQL modules
43 lines (42 loc) • 1.92 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.executionCreator = executionCreator;
const graphql_1 = require("graphql");
const utils_1 = require("../shared/utils");
function executionCreator({ contextBuilder, }) {
const createExecution = (options) => {
// Custom or original execute function
const executeFn = (options === null || options === void 0 ? void 0 : options.execute) || graphql_1.execute;
return (argsOrSchema, document, rootValue, contextValue, variableValues, operationName, fieldResolver, typeResolver) => {
function perform({ context, ɵdestroy: destroy, }) {
const executionArgs = (0, utils_1.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((0, utils_1.isNotSchema)(argsOrSchema)
? argsOrSchema.contextValue
: contextValue).runWithContext(perform);
};
};
return createExecution;
}