Skip to content

Commit

Permalink
Add empty "data:" field for SSE complete event
Browse files Browse the repository at this point in the history
Prior to this commit, the "event:complete" event would have no "data:"
field when completing the stream of data. This can cause problems with
browsers, as the `EventSource` callback might not get called without it.

This commit ensures that an empty data field is present in all cases.

Fixes gh-940
  • Loading branch information
bclozel committed Mar 29, 2024
1 parent 8632199 commit 52202d4
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class GraphQlSseHandler {

private static final Log logger = LogFactory.getLog(GraphQlSseHandler.class);

private static final Mono<ServerSentEvent<Map<String, Object>>> COMPLETE_EVENT = Mono.just(ServerSentEvent.<Map<String, Object>>builder().event("complete").build());
private static final Mono<ServerSentEvent<Map<String, Object>>> COMPLETE_EVENT = Mono.just(ServerSentEvent.<Map<String, Object>>builder(Collections.emptyMap()).event("complete").build());

private final WebGraphQlHandler graphQlHandler;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ private void writeNext(Map<String, Object> value) {
@Override
protected void hookOnComplete() {
try {
this.sseBuilder.event("complete").send();
this.sseBuilder.event("complete").data("");
} catch (IOException exc) {
throw new RuntimeException(exc);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ void shouldRejectQueryOperations() {
data:{"errors":[{"message":"SSE transport only supports Subscription operations","locations":[],"extensions":{"classification":"OperationNotSupported"}}]}
event:complete
data:{}
""");
}
Expand All @@ -96,7 +97,8 @@ void shouldWriteMultipleEventsForSubscription() {
data:{"data":{"bookSearch":{"id":"5","name":"Animal Farm"}}}
event:complete
data:{}
""");
}

Expand All @@ -118,6 +120,7 @@ void shouldWriteEventsAndTerminalError() {
data:{"errors":[{"message":"Subscription error","locations":[],"extensions":{"classification":"INTERNAL_ERROR"}}]}
event:complete
data:{}
""");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ void shouldRejectQueryOperations() throws Exception {
data:{"errors":[{"message":"SSE transport only supports Subscription operations","locations":[],"extensions":{"classification":"OperationNotSupported"}}]}
event:complete
data:
""");
}
Expand All @@ -95,7 +96,8 @@ void shouldWriteMultipleEventsForSubscription() throws Exception {
data:{"data":{"bookSearch":{"id":"5","name":"Animal Farm"}}}
event:complete
data:
""");
}

Expand All @@ -121,7 +123,8 @@ void shouldWriteEventsAndTerminalError() throws Exception {
data:{"errors":[{"message":"Subscription error","locations":[],"extensions":{"classification":"INTERNAL_ERROR"}}]}
event:complete
data:
""");
}

Expand Down

0 comments on commit 52202d4

Please sign in to comment.