Apollo-server: Context undefined issue in GraphQL resolver

Created on 16 Nov 2019  Â·  6Comments  Â·  Source: apollographql/apollo-server

Team,
I am facing an issue on context undefined with the graphql resolver,when using with the apollo server .This is the code used.
return new ApolloServer({
schema,
context: ({ req}) => ({
req
}),
playground:true,
introspection: true
});
Thanks and appreciate your help...

All 6 comments

Hi @manojkumaraut,

Perhaps you already found it, but you can directly return the request data like this:
context: ({ req }) => req

Did you figure this out @manojkumaraut ? same problem here.

@dep-dt
It's been some time, but if I remember correctly the code below worked for me

ApolloServer({
    schema,
    context: ({ req }) => req,
    playground:true,
    introspection: true
});

Ran into a similar issue. Restarting the server worked for me

Still struggling with this - anyone got a solution?

My issue was how I was forming the actual context.

Bad, doesn't work for me:

const server = new ApolloServer({
    schema,
    dataSources,
    context: ({ req }) => {
        console.log(req);
    },
});

Good:

const server = new ApolloServer({
    schema,
    dataSources,
    context: req => {
        console.log(req);
    },
});

And then you should be able to test this using something like curl from the command line.

â–¶ curl \
  -X POST \
  --data '{ "query": "{ foo { bar } }" }' \
http://localhost:4000/graphql

Good luck out there!

Was this page helpful?
0 / 5 - 0 ratings