How to access request headers in custom graphQL resolvers when using neo4j-graphql-js?

1k Views Asked by At

When trying:

const server = new ApolloServer({
  schema: schema,
  context: { req, driver, neo4jDatabase: process.env.NEO4J_DATABASE },
  introspection: true,
  playground: true,
})

error: req is not defined

and when trying:

const server = new ApolloServer({
  schema: schema,
  context: ({ res }, driver, neo4jDatabase = process.env.NEO4J_DATABASE) => (
    res, driver, neo4jDatabase
  ),
  introspection: true,
  playground: true,
})

error: No Neo4j JavaScript driver instance provided.

I've already tried a lot of different ways of writing it and reading Apollo source code but no success.

1

There are 1 best solutions below

0
On

Solved it thanks to this comment https://github.com/grand-stack/graphql-auth-directives/issues/4#issuecomment-494620480

const server = new ApolloServer({
  schema,
  context: ({ req }) => {
    return {
      headers: req.headers, 
      driver
     };
  }
});

and it did work.