Add empty "data:" field for SSE complete event

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
This commit is contained in:
Brian Clozel
2024-03-29 16:19:47 +01:00
parent 8632199abf
commit 52202d40d1
4 changed files with 11 additions and 5 deletions

View File

@@ -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;

View File

@@ -147,7 +147,7 @@ public class GraphQlSseHandler extends AbstractGraphQlHttpHandler {
@Override
protected void hookOnComplete() {
try {
this.sseBuilder.event("complete").send();
this.sseBuilder.event("complete").data("");
} catch (IOException exc) {
throw new RuntimeException(exc);
}

View File

@@ -76,6 +76,7 @@ class GraphQlSseHandlerTests {
data:{"errors":[{"message":"SSE transport only supports Subscription operations","locations":[],"extensions":{"classification":"OperationNotSupported"}}]}
event:complete
data:{}
""");
}
@@ -96,7 +97,8 @@ class GraphQlSseHandlerTests {
data:{"data":{"bookSearch":{"id":"5","name":"Animal Farm"}}}
event:complete
data:{}
""");
}
@@ -118,6 +120,7 @@ class GraphQlSseHandlerTests {
data:{"errors":[{"message":"Subscription error","locations":[],"extensions":{"classification":"INTERNAL_ERROR"}}]}
event:complete
data:{}
""");
}

View File

@@ -71,6 +71,7 @@ class GraphQlSseHandlerTests {
data:{"errors":[{"message":"SSE transport only supports Subscription operations","locations":[],"extensions":{"classification":"OperationNotSupported"}}]}
event:complete
data:
""");
}
@@ -95,7 +96,8 @@ class GraphQlSseHandlerTests {
data:{"data":{"bookSearch":{"id":"5","name":"Animal Farm"}}}
event:complete
data:
""");
}
@@ -121,7 +123,8 @@ class GraphQlSseHandlerTests {
data:{"errors":[{"message":"Subscription error","locations":[],"extensions":{"classification":"INTERNAL_ERROR"}}]}
event:complete
data:
""");
}