如何在不重新创建请求处理程序的情况下更改或扩展 express-graphql 上下文?

How to change or extend express-graphql context without recreating the request handler?

如何在不重新创建请求处理程序的情况下 change/extend 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 }),
    },
  };
});

但所有上下文依赖项都需要预先声明。 重建回调将需要构建处理程序。

谢谢

鉴于缺乏反馈...我不确定我问的是 'obvious' 还是明显错误 :)。

但是,...无论如何,这是我找到的最接近的

/** */
const handler = graphqlHTTP(async (req) => {  
  return {
    schema,
    graphiql: process.env.NODE_ENV !== "production",
    /** this doesn't work */
    context: {
      req,
      session: await getSession({ req }),
    },
  };
});

...仍然,让您可以通过外部方式单独构建自己的依赖图,例如动态导入、require's、依赖注入、单例、HOC ...等