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 native subscription support for coroutine Flows #629

Merged
merged 1 commit into from
Mar 23, 2020

Conversation

josephlbarnett
Copy link
Contributor

Implement a SubscriptionExecutionStrategy that allows
for Flows and Publishers to be returned from
graphql schema elements, and can be processed as a
Flow by subscription consumers. Relax restrictions
that look for Publishers to also allow Flows.

Fixes #358

* that is a [Flow] instead of a [Publisher], and allows schema subscription functions
* to return either a [Flow] or a [Publisher].
*/
class FlowSubscriptionExecutionStrategy(dfe: DataFetcherExceptionHandler) : ExecutionStrategy(dfe) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can make it much simpler -> extend CompletionStageMappingPublisher and then you only need to create createSourceEventStream that returns CompletableFuture<Publisher<Object>> (i.e. convert from Flow to Publisher). Unfortunately createSourceEventStream is private so you would also need to override the execute method but it would basically be copy and paste.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this class is mostly copy paste already. I think what you're suggesting would work for allowing schemas to specify Flow and then consuming a Publisher from the ExecutionResult's data. I think an execution strategy that did that could make sense for some cases. In our case we want to consume a Flow out of the ExecutionResult's data and avoid the Publisher conversion due to Kotlin/kotlinx.coroutines#1825

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dariuszkuc What do you think about this implementation then? Do we want to have this copy paste execution strategy in the library or just provide more hooks for other developers to customize the supported return types?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm leaning towards both - @josephlbarnett can you update FlowSubscriptionExecutionStrategy kdoc to indicate this is a copy of SubscriptionExecutionStrategy that adds support for Flow?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 updated KDoc.

@josephlbarnett
Copy link
Contributor Author

Also, if this ExecutionStrategy doesn't make sense for graphql-kotlin, would appreciate a way to implement it externally, either by the rest of the changes in this PR or some other/pluggable means.

Right now I don't think the check in generateSubscription.kt is overridable, although I think the functionReturnTypes.kt diff can be implemented via schemaHooks.willResolveMonad()

@smyrick
Copy link
Contributor

smyrick commented Mar 20, 2020

@josephlbarnett This code changes in this PR looks good to me, I think if you wanted to provide a custom subscription execution strategy you can just provide your own GraphQL bean and that will be picked up

As far as checking the types, we can add more hooks that would allow you to do this without merging a execution strategy for the entire library.

@smyrick smyrick added changes: minor Changes require a minor version type: enhancement New feature or request labels Mar 21, 2020
@josephlbarnett josephlbarnett force-pushed the subscription-flow branch 4 times, most recently from 7b732f9 to d5269d6 Compare March 23, 2020 17:22
Implement a SubscriptionExecutionStrategy that allows
for `Flow`s and `Publisher`s to be returned from
graphql schema elements, and can be processed as a
`Flow` by subscription consumers.  Relax restrictions
that look for `Publisher`s to also allow `Flow`s.

Fixes ExpediaGroup#358
@josephlbarnett
Copy link
Contributor Author

Rebased off master to use the new subscription return type hooks, thanks those look good!

@smyrick smyrick merged commit 2806cd8 into ExpediaGroup:master Mar 23, 2020
@josephlbarnett josephlbarnett deleted the subscription-flow branch February 2, 2021 15:48
dariuszkuc pushed a commit to dariuszkuc/graphql-kotlin that referenced this pull request Aug 5, 2022
Implement a SubscriptionExecutionStrategy that allows
for `Flow`s and `Publisher`s to be returned from
graphql schema elements, and can be processed as a
`Flow` by subscription consumers.  Relax restrictions
that look for `Publisher`s to also allow `Flow`s.

Fixes ExpediaGroup#358
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changes: minor Changes require a minor version type: enhancement New feature or request
Development

Successfully merging this pull request may close these issues.

native subscription support for Kotlin Flow
3 participants