Skip to content

Commit bf2d006

Browse files
rstoyanchevbclozel
authored andcommitted
WebMvc handler skips ASYNC if possible
Closes spring-projectsgh-904
1 parent d836f68 commit bf2d006

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

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

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2023 the original author or authors.
2+
* Copyright 2020-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,12 +20,13 @@
2020
import java.util.Arrays;
2121
import java.util.List;
2222
import java.util.Map;
23+
import java.util.concurrent.CompletableFuture;
24+
import java.util.concurrent.ExecutionException;
2325

2426
import jakarta.servlet.ServletException;
2527
import jakarta.servlet.http.Cookie;
2628
import org.apache.commons.logging.Log;
2729
import org.apache.commons.logging.LogFactory;
28-
import reactor.core.publisher.Mono;
2930

3031
import org.springframework.context.i18n.LocaleContextHolder;
3132
import org.springframework.core.ParameterizedTypeReference;
@@ -44,7 +45,7 @@
4445
import org.springframework.web.servlet.function.ServerResponse;
4546

4647
/**
47-
* GraphQL handler to expose as a WebMvc.fn endpoint via
48+
* GraphQL handler to expose as a WebMvc functional endpoint via
4849
* {@link org.springframework.web.servlet.function.RouterFunctions}.
4950
*
5051
* @author Rossen Stoyanchev
@@ -56,7 +57,7 @@ public class GraphQlHttpHandler {
5657
private static final Log logger = LogFactory.getLog(GraphQlHttpHandler.class);
5758

5859
private static final ParameterizedTypeReference<Map<String, Object>> MAP_PARAMETERIZED_TYPE_REF =
59-
new ParameterizedTypeReference<Map<String, Object>>() {};
60+
new ParameterizedTypeReference<>() {};
6061

6162
// To be removed in favor of Framework's MediaType.APPLICATION_GRAPHQL_RESPONSE
6263
private static final MediaType APPLICATION_GRAPHQL_RESPONSE =
@@ -97,7 +98,7 @@ public ServerResponse handleRequest(ServerRequest serverRequest) throws ServletE
9798
logger.debug("Executing: " + graphQlRequest);
9899
}
99100

100-
Mono<ServerResponse> responseMono = this.graphQlHandler.handleRequest(graphQlRequest)
101+
CompletableFuture<ServerResponse> future = this.graphQlHandler.handleRequest(graphQlRequest)
101102
.map(response -> {
102103
if (logger.isDebugEnabled()) {
103104
logger.debug("Execution complete");
@@ -106,9 +107,22 @@ public ServerResponse handleRequest(ServerRequest serverRequest) throws ServletE
106107
builder.headers(headers -> headers.putAll(response.getResponseHeaders()));
107108
builder.contentType(selectResponseMediaType(serverRequest));
108109
return builder.body(response.toMap());
109-
});
110+
})
111+
.toFuture();
110112

111-
return ServerResponse.async(responseMono);
113+
if (future.isDone()) {
114+
try {
115+
return future.get();
116+
}
117+
catch (ExecutionException ex) {
118+
throw new ServletException(ex.getCause());
119+
}
120+
catch (InterruptedException ex) {
121+
throw new ServletException(ex);
122+
}
123+
}
124+
125+
return ServerResponse.async(future);
112126
}
113127

114128
private static MultiValueMap<String, HttpCookie> initCookies(ServerRequest serverRequest) {

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -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 asyncResponse) {
127+
asyncResponse.block();
128+
}
126129

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

0 commit comments

Comments
 (0)