Papooch / nestjs-cls

A continuation-local storage (async context) module compatible with NestJS's dependency injection.
https://papooch.github.io/nestjs-cls/
MIT License
390 stars 23 forks source link

Setup in a different enhancer than mounting? #75

Closed Faithfinder closed 1 year ago

Faithfinder commented 1 year ago

I would like to mount CLS in middleware, since it guarantees one mount per request context (in graphql if you use interceptor, the context will be per resolver instead).

However, middleware doesn't yet have access to user and stuff, since that gets resolved in Guards. So I would like to add that in an interceptor (and not directly in a guard, for separation of concerns). Setting up such an interceptor myself would be kinda trivial, but I wonder if it's a good enough case to include in the library anyway?

UPD: not that trivial, since a global interceptor for setup can't inject ClsService, lol. Oh well, there are workarounds

Papooch commented 1 year ago

Actually, there's a system in place that guarantees common CLS context across all resolvers within one request if you use the ClsInterceptor with GQL. It got implemented just recently in reaction to this issue: https://github.com/Papooch/nestjs-cls/issues/53