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
Conversation
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 |
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this 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 !
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.