code snippets graphQL

How To Access the request object inside a GraphQL resolver Function (Apollo-Server-Express)

If you’re working with GraphQL (a query language for your API), and you’re using apollo-server-express, You may notice unlike express-graphql, request object will not be available in context, if it’s not defined in apollo-sever-express middleware, but don’t worry, if want request object available in context, just use following quick hack, not really and you’re done.

normal apollo-server-express middleware:

app.use('/graphql', bodyParser.json(), graphqlExpress({ yourSchema }));

apollo-server-express middleware with request object as context:

consider, you want request user as context:

app.use('/graphql', bodyParser.json(), graphqlExpress(req => ({
  context: { user: req.user }

Here, we are using es6 arrow functions and passing request object in that that it will be available to pass in context of graphql query.

Now, you can pass context in your resolver functions to access request object inside resolver function.

About the author


Vithalreddy.M.B is the Editor of & with a focus on startups, Startup News, Business, and Technology.He is also an Electrical & Electronics Engineer, Technology Lover, and Entrepreneur by choice.Started His first startup at 21.