如何在不重新创建请求处理程序的情况下更改/扩展 express-graphql 上下文?
/** */
const schema = makeExecutableSchema({
typeDefs,
resolvers,
});
/** */
export default (req: any, res: any, context: any) => {
console.log("context:", context);
const handler = graphqlHTTP({
schema,
graphiql: process.env.NODE_ENV !== "production",
context: {
req,
...context,
},
});
return handler(req, res);
};
我注意到 graphqlHTTP 接受选项的承诺。
不知道这有什么帮助。因为(req)=> Promise<Otions>无论如何都需要为每个请求重新构建(选项回调)?
我错过了什么?
注意:请忽略上下文构建实现,它可以是你喜欢的任何东西
这似乎可以解决问题,部分...
/** */
const handler = graphqlHTTP(async (req: any) => {
return {
schema,
graphiql: process.env.NODE_ENV !== "production",
/** this doesn't work */
context: {
req,
session: await getSession({ req }),
},
};
});
但是所有上下文依赖项都需要预先声明。重建回调将需要构建处理程序。
谢谢