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

Add ClsInterceptor to avoid having to use enterWith with GraphQL #5

Closed
andreialecu opened this issue Oct 20, 2021 · 3 comments · Fixed by #6
Closed

Add ClsInterceptor to avoid having to use enterWith with GraphQL #5

andreialecu opened this issue Oct 20, 2021 · 3 comments · Fixed by #6
Assignees
Labels
enhancement New feature or request

Comments

@andreialecu
Copy link

andreialecu commented Oct 20, 2021

Hey there, just came across this library while investigating my own issues with AsyncLocalStorage losing context inside graphql interceptors.

I'm not (yet) using this library, I have a custom implementation, but I wanted to mention that I eventually got it working via this pattern in the interceptor:

  intercept(...) {
    return new Observable((observer) => {
      storage.run(myContext, () => {
        next
          .handle()
          .pipe(...)
          .subscribe({
            next: (res) => observer.next(res),
            error: (error) => observer.error(error),
            complete: () => observer.complete(),
          });
      });
    });
  }

Seems that wrapping the return in a cold observable is necessary, but this is all that I needed to do.

With this I can use run instead of enterWith, which I noticed was mentioned as a caveat in the readme about not being supported.

@Papooch
Copy link
Owner

Papooch commented Oct 20, 2021

Huh, that's very interesting, I'm going to look into it. I might add this as an alternative solution if you don't mind. Though setting up context in an interceptor would still make it unavailable in guards (which is where I'm mainly using it in my project) and I suspect this trick wouldn't be possible in a guard..

@andreialecu
Copy link
Author

andreialecu commented Oct 20, 2021

I might add this as an alternative solution if you don't mind

Of course. Just make sure you verify it first. I'm not sure if the reason it works is specific to my project or not.

Indeed, from what I saw, this method doesn't pass the context into guards. I don't currently need the AsyncLocalStorage context in guards though, I can use the regular Apollo/GraphQL context there instead.

@Papooch Papooch changed the title Usage with GraphQL Add ClsInterceptor to avoid having to use enterWith with GraphQL Oct 24, 2021
@Papooch Papooch added the enhancement New feature or request label Oct 24, 2021
@Papooch Papooch self-assigned this Oct 24, 2021
@Papooch Papooch linked a pull request Oct 24, 2021 that will close this issue
@Papooch
Copy link
Owner

Papooch commented Oct 24, 2021

I just implemented your solution and it works great, thanks a lot again! When testing, I found it strange, that setting up the context in an interceptor causes it to be lost in an Exception Filter but only for REST (which is a non issue as you can use middleware there), but it works fine with GQL.

@Papooch Papooch closed this as completed Oct 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants