0

I am trying to get the user ip address.

My current setup is like so

const app = express()
const port = 5100

const server = new ApolloServer({
    typeDefs,
    resolvers,
});

await server.start()
server.applyMiddleware({app})


app.listen(port, () => {
  console.log(` server started at http://localhost:${port}`)
})


I tried to follow this with no luck How to get client ip address on an apollo subscriptions server?

Jonathan Coletti
  • 448
  • 4
  • 13

1 Answers1

0

Passing request into Apollo Server context generated in function

Ok so basically you have to add

const server = new ApolloServer({
    typeDefs,
    resolvers,
    context: createContext
});

createContext

export const createContext = (req: Request) => {
    return {request: req}
}

access in resolvers

userauth: async (parent: any, args: {}, context: any, info: any) => {
            console.log(context.request.req.headers);
            return await resolveUserAuth()
        },
Jonathan Coletti
  • 448
  • 4
  • 13