howto change/extend express-graphql context without recreating the request handler ?
/** */
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);
};
I noticed graphqlHTTP accepts a promise for options.
Not sure how is this going to help.
as the (req)=> Promise<Otions>
(options-callback) would need to be built again for every request anyway ?
mI missing something ?
NOTE: please ignore the context building implementation it could be anything you like
This seems to do the trick, partially...
/** */
const handler = graphqlHTTP(async (req: any) => {
return {
schema,
graphiql: process.env.NODE_ENV !== "production",
/** this doesn't work */
context: {
req,
session: await getSession({ req }),
},
};
});
but all the context dependencies need to be declared upfront. Rebuilding the callback will require to build the handler.
Thanks
Given the lack of feedback ... I'm not sure if I'm asking something 'obvious' or obviously wrong :).
But, ...anyway, this is the closest I've found
/** */
const handler = graphqlHTTP(async (req) => {
return {
schema,
graphiql: process.env.NODE_ENV !== "production",
/** this doesn't work */
context: {
req,
session: await getSession({ req }),
},
};
});
...still, lets you alone to build your own dependency graph by external means, like dynamic import, require's , dependency injection, singletons, HOC ...etc