How do I get access to the express response object within the rootValue resolver method?
i.e.
app.use('/api/messages', graphqlHTTP({
schema: SpatialMessageSchema,
rootValue: SpatialMessageResolver,
graphiql: true,
}));
myResolver(args, res){
@leebyron Is the suggested approach with handling express resolvers to return a Promise?
Hey Erik,
You can check out the example in https://github.com/graphql/express-graphql#providing-extensions which illustrates using the original response object
For your case of wanting to use request as the rootObject, you could do:
app.use('/api/messages', graphqlHTTP(request => ({
schema: SpatialMessageSchema,
rootValue: request,
graphiql: true,
})));
If you need to do some work before resolving the GraphQL request, then yes you can return a Promise.
app.use('/graphql', graphqlHTTP(request => {
return doSomethingSlowThenPromise().then(() => ({
schema: mySchema,
graphiql: true
}))
})
You can also always use middleware ahead of the GraphQL middleware:
// some random middleware that takes a minute
app.use('/graphql', (req, res, next) => {
doSomethingSlowThenCall(value => {
req.someValue = value;
// then continue
next();
})
});
app.use('/graphql', graphqlHTTP(request => ({
schema: mySchema,
rootValue: request.someValue,
graphiql: true
}))
Most helpful comment
Hey Erik,
You can check out the example in https://github.com/graphql/express-graphql#providing-extensions which illustrates using the original response object
For your case of wanting to use
requestas the rootObject, you could do:If you need to do some work before resolving the GraphQL request, then yes you can return a Promise.
You can also always use middleware ahead of the GraphQL middleware: