You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm using Apollo Server 2 and Express.js vanilla (with apollo-server-express).
Everything works good also with Subscriptions except the Express session mechanism.
The problem:
I'm using cookie-session (https://github.com/expressjs/cookie-session, but I think this is the same for express-session middleware) and when my browser start a new connection with my server the ApolloServer onConnect hook doesn't have the req attribute and neither req.session and so on...
What I can do is to parse the cookies from webSocket.upgradeReq.headers.cookie in onConnect lifecycle hook, but it seems to me very hacky.
The code:
const{ ApolloServer }=require('apollo-server-express')consttypeDefs=require('../src/graphql/types')constresolvers=require('../src/graphql/resolvers')constmodels=require('../src/models')constapolloServer=newApolloServer({
typeDefs,
resolvers,context: ({ req, connection })=>{// connection exists only on webSocket connectionif(connection){return{currentUser: connection.context.currentUser// <-- I NEED THIS!}}// if not a (webSocket) connection it is a "default" HTTP callreturn{
models,currentUser: {id: req.user.id}}},subscriptions: {onConnect: (connectionParams,webSocket)=>{// "connectionParams" is from the client but I cannot use it because cookies are HTTP-Only// I can retrieve cookies from here: "webSocket.upgradeReq.headers.cookie" but then I need to parse them which seems a bit hacky to me// return { currentUser: req.user.id } // <-- I NEED THIS (req.user.id doesn't exists)!}}})module.exports=apolloServer
I can't find anything on Apollo Server Docs site (for other topics very well documented!).
I'm using Apollo Server 2 and Express.js vanilla (with
apollo-server-express
).Everything works good also with Subscriptions except the Express session mechanism.
The problem:
I'm using cookie-session (https://github.com/expressjs/cookie-session, but I think this is the same for express-session middleware) and when my browser start a new connection with my server the ApolloServer
onConnect
hook doesn't have thereq
attribute and neitherreq.session
and so on...What I can do is to parse the cookies from
webSocket.upgradeReq.headers.cookie
inonConnect
lifecycle hook, but it seems to me very hacky.The code:
I can't find anything on Apollo Server Docs site (for other topics very well documented!).
Where am I doing wrong?
StackOverflow question: https://stackoverflow.com/questions/52280481/graphql-subscription-websocket-nodejs-express-session-with-apollo-server-2
The text was updated successfully, but these errors were encountered: