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

Simplify DgsSSESubscriptionHandler by returning Flux #1001

Merged
merged 1 commit into from Apr 22, 2022

Conversation

kilink
Copy link
Member

@kilink kilink commented Apr 22, 2022

DgsSSESubscriptionHandler was rolling its own Subscriber implementation in order
to adapt the Publisher returned by the GraphQL query to the SseEmitter; however,
Spring itself will handle this when the content type is "text/event-stream" and
we simply return a reactive type, such as Flux. Doing so greatly simplifies the
code in the controller, and allows for future enhancements such as propagating
context to the publisher.

Another change is that any error condition occurring before the GraphQL query is
executed successfully (e.g., a Publisher is returned) will result in a non-SSE response;
that is, the status code of the response will be non-200, and the error message
will no longer be written as a single server-sent event.

Finally, the way errors are written has been fixed. Previously the entire stack trace
was accidentally written to the event, now only the exception message is propagated.

DgsSSESubscriptionHandler was rolling its own Subscriber implementation in order
to adapt the Publisher returned by the GraphQL query to the SseEmitter; however,
Spring itself will handle this when the content type is "text/event-stream" and
we simply return a reactive type, such as Flux. Doing so greatly simplifies the
code in the controller, and allows for future enhancements such as propagating
context to the publisher.

Another change is that any error condition occurring before the GraphQL query is
executed successfully (e.g., a Publisher is returned) will result in a non-SSE response;
that is, the status code of the response will be non-200, and the error message
will no longer be written as a single server-sent event.

Finally, the way errors are written has been fixed. Previously the entire stack trace
was accidentally written to the event, now only the exception message is propagated.
}

val subscriber = object : Subscriber<ExecutionResult> {
lateinit var subscription: Subscription
Copy link
Member Author

Choose a reason for hiding this comment

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

I think this actually wasn't thread safe, since the property wasn't volatile / access wasn't synchronized, although in practice I never saw any actual issues.

}

publisher.subscribe(subscriber)
val subscriptionId = UUID.randomUUID().toString()
Copy link
Contributor

Choose a reason for hiding this comment

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

How does the execution Id get set here?

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we can get the ExecutionId here.

Copy link
Contributor

@berngp berngp left a comment

Choose a reason for hiding this comment

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

Thanks for all these work @kilink !

@berngp berngp merged commit 62f053e into master Apr 22, 2022
@berngp berngp deleted the subscription-refactor branch April 22, 2022 16:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants