express-graphql passes the express request as a default value for context. This is very convenient when I want to do something such as check a user's authentication from the request header.
However, it does not appear that there is a way to get a handle on the express request object from inside a resolver when using graphql-server-express. Is there a specific reason you left this out? Am I missing some other way to get a handle on the req?
Thanks.
@pikelnys if you want, you can pass the request object into the context that every resolver gets. you can do this in the function that returns the configuration. Something like this (copied from here) will do:
graphqlExpress(request => ({
schema: typeDefinitionArray,
context: { user: request.session.user }
}))
@helfer Thank you! But this should be documented 馃槄
@helfer wait how?
app.use('/api', bodyParser.json(), jwt({
secret: process.env.JWT_SECRET,
credentialsRequired: false,
}), graphqlExpress(request => ({
schema,
context: {
authUser: request.user
}
})));
returns typeerror: graphqlExpress is not a function
i can't figure that out
@Muphet when your importing graphqlExpress you need to do
const { graphqlHTTP } = require("express-graphql");
then use graphqlHTTP in place of graphqlExpress
Most helpful comment
@pikelnys if you want, you can pass the
requestobject into thecontextthat every resolver gets. you can do this in the function that returns the configuration. Something like this (copied from here) will do: