Skip to content

Commit 24a7be2

Browse files
committed
Avoid unnecessary async handling in GraphQlHttpHandler
In the webmvc GraphQlHttpHandler, return the server response directly if the WebGraphQlHandler returns an already completed Mono. This avoids some extra overhead incurred with async processing.
1 parent d836f68 commit 24a7be2

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

spring-graphql/src/main/java/org/springframework/graphql/server/webmvc/GraphQlHttpHandler.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,12 @@ public ServerResponse handleRequest(ServerRequest serverRequest) throws ServletE
106106
builder.headers(headers -> headers.putAll(response.getResponseHeaders()));
107107
builder.contentType(selectResponseMediaType(serverRequest));
108108
return builder.body(response.toMap());
109-
});
109+
})
110+
.cache();
110111

112+
if (responseMono.hasElement().block()) {
113+
return responseMono.block();
114+
}
111115
return ServerResponse.async(responseMono);
112116
}
113117

@@ -138,5 +142,4 @@ private static MediaType selectResponseMediaType(ServerRequest serverRequest) {
138142
}
139143
return MediaType.APPLICATION_JSON;
140144
}
141-
142145
}

spring-graphql/src/test/java/org/springframework/graphql/server/webmvc/GraphQlHttpHandlerTests.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,10 @@ private MockHttpServletResponse handleRequest(
122122
MockHttpServletRequest servletRequest, GraphQlHttpHandler handler) throws ServletException, IOException {
123123

124124
ServerRequest request = ServerRequest.create(servletRequest, MESSAGE_READERS);
125-
ServerResponse response = ((AsyncServerResponse) handler.handleRequest(request)).block();
125+
ServerResponse response = handler.handleRequest(request);
126+
if (response instanceof AsyncServerResponse asyncServerResponse) {
127+
asyncServerResponse.block();
128+
}
126129

127130
MockHttpServletResponse servletResponse = new MockHttpServletResponse();
128131
response.writeTo(servletRequest, servletResponse, new DefaultContext());

0 commit comments

Comments
 (0)