Apollo-server: graphqlExpress has no way of accepting express request through context

Created on 9 Jun 2017  路  4Comments  路  Source: apollographql/apollo-server

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.

Most helpful comment

@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 }
}))

All 4 comments

@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 馃槄

PS: https://github.com/apollographql/apollo-server/pull/757

@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

Was this page helpful?
0 / 5 - 0 ratings