-
Hello, I'm looking for some guidance on how to combine two of the examples listed in this repo, auth0 and api-routes-apollo-server-and-client. I have both of these working separately, but what I would like to do is get my session from auth0 and add my user to the apollo server context. When I attempted to do this, I saw that my session cookie was coming through on my graphql api route, but then it suddenly becomes an empty object. I've added my api route code below so that you can see what it is I'm attempting to do. Thank you in advance. pages/api/graphql.jsimport { ApolloServer } from 'apollo-server-micro';
import { schema } from 'apollo/schema';
import auth0 from 'lib/auth0';
export const config = {
api: {
bodyParser: false
}
};
const apolloServer = new ApolloServer({
schema,
// Tried to make this async and call auth0.getSession(req)
// but because the cookies are {} my session is null
context: async ({ req }) => {
const session = await auth0.getSession(req);
return session;
}
});
// Created this HOF so that I could log out the cookies
const handler = apollo => async (req, res) => {
// Works once, then is {}
console.log(req.cookies);
return apollo(req, res);
};
export default handler(apolloServer.createHandler({
path: '/api/graphql'
})); |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
I could be totally wrong here, but I think the I don't use auth0, but here's how I got cookies into my context object: |
Beta Was this translation helpful? Give feedback.
-
For anyone else looking to do something similar to what I am, I have figured out the missing setting on the apollo server. const apolloServer = new ApolloServer({
context: ctx => ctx,
/**
* Add introspection to view the schema / docs in playground after running `next build` and `next start`
* Without introspection, you'll get a message saying "Server cannot be reached"
*/
introspection: true,
playground: {
settings: {
'request.credentials': 'same-origin'
}
},
schema
}); Adding the playground settings did the trick for me. Now I have access to my session cookie with each of my resolvers. |
Beta Was this translation helpful? Give feedback.
For anyone else looking to do something similar to what I am, I have figured out the missing setting on the apollo server.
Adding the playground settings did the trick for me. Now I have access to my session cookie with each of my resolvers.