Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use express-session with nest/graphql #4527

Closed
gaurav-bothra opened this issue Sep 2, 2020 · 3 comments
Closed

How to use express-session with nest/graphql #4527

gaurav-bothra opened this issue Sep 2, 2020 · 3 comments

Comments

@gaurav-bothra
Copy link

gaurav-bothra commented Sep 2, 2020

I'm submitting a...


[ ] Regression 
[ ] Bug report
[ ] Feature request
[ x] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

Currently I created simple boilerplate of nestjs/graphql with express-session middleware. I hooked session middleware in main.ts file. with saveUninitialized flag true... my nestjs server breaks with following error

(node:67816) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client at ServerResponse.setHeader (_http_outgoing.js:485:11).

Also when I tries to create session on server by using ctx.req.session.userId. same error occur I find similar issue on github
issue link. But I am unaware how to do it in nestjs fashion. I am new with nestjs.

Expected behavior

Minimal reproduction of the problem with instructions

app.use(session({ name: SESSION_NAME, store: new RedisStore({ host:REDIS_HOST, port:REDIS_PORT, client: client, disableTouch: true, }), cookie: { maxAge: 1000 * 60 * 60 * 24 * 365 * 10, // 10 years httpOnly: true, sameSite: "lax", // csrf secure: __PROD__, // cookie only works in https domain: __PROD__ ? ".local" : undefined, }, saveUninitialized: true, secret: SESSION_SECRET, resave: true }))

What is the motivation / use case for changing the behavior?

(node:45556) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client at ServerResponse.setHeader (_http_outgoing.js:485:11) at ServerResponse.header (C:\Users\gaura\Desktop\Teaching Ninjas\server\node_modules\express\lib\response.js:771:10) at ServerResponse.send (C:\Users\gaura\Desktop\Teaching Ninjas\server\node_modules\express\lib\response.js:170:12) at ServerResponse.json (C:\Users\gaura\Desktop\Teaching Ninjas\server\node_modules\express\lib\response.js:267:15) at ExpressAdapter.reply (C:\Users\gaura\Desktop\Teaching Ninjas\server\node_modules\@nestjs\platform-express\adapters\express-adapter.js:24:57) at ExceptionsHandler.handleUnknownError (C:\Users\gaura\Desktop\Teaching Ninjas\server\node_modules\@nestjs\core\exceptions\base-exception-filter.js:33:24) at ExceptionsHandler.catch (C:\Users\gaura\Desktop\Teaching Ninjas\server\node_modules\@nestjs\core\exceptions\base-exception-filter.js:17:25) at ExceptionsHandler.next (C:\Users\gaura\Desktop\Teaching Ninjas\server\node_modules\@nestjs\core\exceptions\exceptions-handler.js:16:20) at C:\Users\gaura\Desktop\Teaching Ninjas\server\node_modules\@nestjs\core\router\router-proxy.js:24:35 at Layer.handle_error (C:\Users\gaura\Desktop\Teaching Ninjas\server\node_modules\express\lib\router\layer.js:71:5) at trim_prefix (C:\Users\gaura\Desktop\Teaching Ninjas\server\node_modules\express\lib\router\index.js:315:13) at C:\Users\gaura\Desktop\Teaching Ninjas\server\node_modules\express\lib\router\index.js:284:7 at Function.process_params (C:\Users\gaura\Desktop\Teaching Ninjas\server\node_modules\express\lib\router\index.js:335:12) at next (C:\Users\gaura\Desktop\Teaching Ninjas\server\node_modules\express\lib\router\index.js:275:10) at Layer.handle_error (C:\Users\gaura\Desktop\Teaching Ninjas\server\node_modules\express\lib\router\layer.js:67:12) at trim_prefix (C:\Users\gaura\Desktop\Teaching Ninjas\server\node_modules\express\lib\router\index.js:315:13) at C:\Users\gaura\Desktop\Teaching Ninjas\server\node_modules\express\lib\router\index.js:284:7 at Function.process_params (C:\Users\gaura\Desktop\Teaching Ninjas\server\node_modules\express\lib\router\index.js:335:12) at Immediate.next [as _onImmediate] (C:\Users\gaura\Desktop\Teaching Ninjas\server\node_modules\express\lib\router\index.js:275:10) at processImmediate (internal/timers.js:441:21) (node:45556) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:45556) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code

@smolinari
Copy link

I'm getting this error too.

Scott

@jeromemeichelbeck
Copy link

Same here.

It's a shame because it works great with REST :/

@glasser
Copy link
Member

glasser commented Feb 6, 2021

This is the Apollo Server repository. This issue appears to be reporting a bug in combining something called express-session with something called nest/graphql (which I think means @nestjs/graphql)? It seems like this issue should be reported to one of those projects?

It looks like @nestjs/graphql is built on top of Apollo Server. If @nestjs/graphql needs some improvement to Apollo Server to work properly then I'm definitely interested in hearing that! But I don't see any references to this project in this issue, unless I'm entirely confused.

@glasser glasser closed this as completed Feb 6, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants