将apollo-server与express
一起使用时,我打开需要关闭的上下文(数据库连接)。所以我是这样做的:
const server = new ApolloServer({
schema,
playground: config.enableGraphiQL,
context: (req: express.Request) => req.res.locals.context
});
const app = express();
app.use((req, res, next) => {
logger.debug('Query start');
res.locals.context = new Context();
next();
res.on("finish", () => {
logger.debug('Query finish');
const context: Context = res.locals.context;
context.connection.close();
});
});
server.applyMiddleware({ app });
使用createTestClient
中的apollo-server-testing
时,我不知道如何关闭它。这是代码:
const server = new ApolloServerBase({
schema,
context: () => new Context() //// FIXME: not closing
});
const client = createTestClient(server);
因此数据库连接未关闭。如何让钩子/事件通知执行已结束?