Skip to content

Commit c8fcbc7

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 c8fcbc7

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ public ServerResponse handleRequest(ServerRequest serverRequest) throws ServletE
108108
return builder.body(response.toMap());
109109
});
110110

111+
if (responseMono.hasElement().block()) {
112+
return responseMono.block();
113+
}
111114
return ServerResponse.async(responseMono);
112115
}
113116

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)