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

Async Subscription topic filters #470

Closed
glentakahashi opened this issue Nov 21, 2019 · 10 comments
Closed

Async Subscription topic filters #470

glentakahashi opened this issue Nov 21, 2019 · 10 comments
Assignees
Labels
Community 👨‍👧 Something initiated by a community Enhancement 🆕 New feature or request
Milestone

Comments

@glentakahashi
Copy link

Describe the solution you'd like
I'd like to be able to put authorization checks inside the subscription topics option so that I can dynamically block or allow users on subscriptions. Right now my auth checks are asynchronous but the SubscriptionTopicFunc doesn't accept Promises.

Describe alternatives you've considered
It's possible to put the authorization checks in the filter method or in the subscription logic itself, however this means that the authorization checks will fire for every event instead of just once at subscription time which is much more expensive.

Additional context
Add any other context or screenshots about the feature request here.

@MichalLytek
Copy link
Owner

Unfortunately, it's not possible. graphql-subscriptions requires that the topics function return the AsyncIterator synchronously (ResolverFn).
https://github.com/apollographql/graphql-subscriptions/blob/master/src/with-filter.ts#L7-L8

Please open an issue on their repo about adding support for async ResolverFn in withFilter helper - then after deps update it will available and working without any changes on TypeGraphQL side.

@MichalLytek MichalLytek added Blocked 🚫 Resolving this issue is blocked by other issue or 3rd party stuffs Community 👨‍👧 Something initiated by a community Enhancement 🆕 New feature or request labels Nov 22, 2019
@glentakahashi
Copy link
Author

Opened apollographql/graphql-subscriptions#213

@jan-wilhelm

This comment has been minimized.

@oliversalzburg
Copy link
Contributor

I'm currently looking into this limitation and the one from #617 myself and I don't quite understand the reliance on graphql-subscriptions here. They purely export the ResolverFn type, which is insufficient for the use case, as returning a Promise from the subscribe handler is generally supported.

When changing the definition for the ResolveFn in Subscription.ts, my code seems to behave as expected:

export declare type ResolverFn = (rootValue?: any, args?: any, context?: any, info?: any) => AsyncIterator<any> | Promise<AsyncIterator<any>>;

graphql-js will correctly await the provided handler.

The withFilter from their package is similarly limited. However, this can be worked around by simply providing a custom replacement.

As graphql-subscriptions is unmaintained and consumers like Apollo Server are moving to other libraries as well, I feel this should be addressed in type-graphql if at all possible.

Is there anything I'm missing here?

@MichalLytek
Copy link
Owner

Is there anything I'm missing here?

Yes.

I don't quite understand the reliance on graphql-subscriptions here

TypeGraphQL was created 3 years ago. graphql-subscriptions was the most popular lib for subscriptions back then.

Introducing some other replacement, especially custom one, requires a lot of effort and is a breaking change for all the users.

@oliversalzburg
Copy link
Contributor

That's fair, but the package seems to purely provide PubSub, withFilter and a few types. This issue seems to only exist due to an incomplete type and allowing an additional return type on ResolveFn shouldn't be a breaking change for any current consumers of type-graphql from what I can see.

Their PusbSub implementation and withFilter would still behave exactly the same way as before.

@MichalLytek
Copy link
Owner

In my project I just do as any because it's working with async.
But without upstream fix I can't update TypeGraphQL code.

@oliversalzburg
Copy link
Contributor

The library has been abandoned long ago. This is never going to happen. There is no reason to type subscribe in SubscribeOptions as ResolveFn, as graphql-subscriptions does not consume this function. Their ResolveFn type is only relevant for their consumption of the handler in withFilter. It may appear convenient to reuse it here, but what should dictate the correct type is graphql-js.

If that isn't compelling enough to change this behavior, then I'm probably out of luck. Depending on an abandoned library shouldn't be something to be happy about in the first place though.

@MichalLytek
Copy link
Owner

I have in plans for vNext to reimplement subscriptions from scratch, both on API (decorators) level and inside internals. We now don't need graphql-subscriptions at all with well-supported async-iterators:
apollographql/graphql-subscriptions#240 (comment)

But this is a breaking change as now you have put redis-based pubSub and it's working. That's the promise of v1.x which I can't break.

@MichalLytek
Copy link
Owner

Closing as implemented by #1578 🔒

@MichalLytek MichalLytek removed the Blocked 🚫 Resolving this issue is blocked by other issue or 3rd party stuffs label Jan 3, 2024
@MichalLytek MichalLytek self-assigned this Jan 3, 2024
@MichalLytek MichalLytek modified the milestones: 2.x versions, 2.0 release Jan 3, 2024
@github-project-automation github-project-automation bot moved this to Done in Board Aug 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Community 👨‍👧 Something initiated by a community Enhancement 🆕 New feature or request
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

4 participants