Skip to content

How to access req object in resolver using express? #135

@yosletpp

Description

@yosletpp

I'm tying to make an graphQL endpoint in a already existing express project, Have a authentication middleware that evaluates the token and attach user object to req (req.user) I want to be able to get this user in the resolver. example:

....
import { createHandler } from 'graphql-http/lib/use/express';
....

const schema2 = buildSchema(`
  type Query {
    hello: String
  }
`);

const root2 = {
  hello: (args, context, info) => {
    console.log(context.user);
    // some logic with the user here
    return 'Hello, world!'
  },
};

router.post(
  '/business2',
  createHandler({
    schema: schema2,
    rootValue: root2,
    context: (req) => ({
      user: req.user
    })
    // Pass req.user into the context,
  })
);

if I import createHandler like this

const { createHandler } = require('graphql-http/lib/use/express')

req.user is undefined

but if I import createHanlder like this:

const { createHandler } = require('graphql-http')

I get req.user correctly but, the endpoints gives me timeout.

What is the proper way to do this?? Thanks so much in advance.

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions