1
1
/*
2
- * Copyright 2020-2023 the original author or authors.
2
+ * Copyright 2020-2024 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
20
20
import java .util .Arrays ;
21
21
import java .util .List ;
22
22
import java .util .Map ;
23
+ import java .util .concurrent .CompletableFuture ;
24
+ import java .util .concurrent .ExecutionException ;
23
25
24
26
import jakarta .servlet .ServletException ;
25
27
import jakarta .servlet .http .Cookie ;
26
28
import org .apache .commons .logging .Log ;
27
29
import org .apache .commons .logging .LogFactory ;
28
- import reactor .core .publisher .Mono ;
29
30
30
31
import org .springframework .context .i18n .LocaleContextHolder ;
31
32
import org .springframework .core .ParameterizedTypeReference ;
44
45
import org .springframework .web .servlet .function .ServerResponse ;
45
46
46
47
/**
47
- * GraphQL handler to expose as a WebMvc.fn endpoint via
48
+ * GraphQL handler to expose as a WebMvc functional endpoint via
48
49
* {@link org.springframework.web.servlet.function.RouterFunctions}.
49
50
*
50
51
* @author Rossen Stoyanchev
@@ -56,7 +57,7 @@ public class GraphQlHttpHandler {
56
57
private static final Log logger = LogFactory .getLog (GraphQlHttpHandler .class );
57
58
58
59
private static final ParameterizedTypeReference <Map <String , Object >> MAP_PARAMETERIZED_TYPE_REF =
59
- new ParameterizedTypeReference <Map < String , Object > >() {};
60
+ new ParameterizedTypeReference <>() {};
60
61
61
62
// To be removed in favor of Framework's MediaType.APPLICATION_GRAPHQL_RESPONSE
62
63
private static final MediaType APPLICATION_GRAPHQL_RESPONSE =
@@ -97,7 +98,7 @@ public ServerResponse handleRequest(ServerRequest serverRequest) throws ServletE
97
98
logger .debug ("Executing: " + graphQlRequest );
98
99
}
99
100
100
- Mono <ServerResponse > responseMono = this .graphQlHandler .handleRequest (graphQlRequest )
101
+ CompletableFuture <ServerResponse > future = this .graphQlHandler .handleRequest (graphQlRequest )
101
102
.map (response -> {
102
103
if (logger .isDebugEnabled ()) {
103
104
logger .debug ("Execution complete" );
@@ -106,9 +107,22 @@ public ServerResponse handleRequest(ServerRequest serverRequest) throws ServletE
106
107
builder .headers (headers -> headers .putAll (response .getResponseHeaders ()));
107
108
builder .contentType (selectResponseMediaType (serverRequest ));
108
109
return builder .body (response .toMap ());
109
- });
110
+ })
111
+ .toFuture ();
110
112
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 );
112
126
}
113
127
114
128
private static MultiValueMap <String , HttpCookie > initCookies (ServerRequest serverRequest ) {
0 commit comments