Skip to content

Commit 4740696

Browse files
committed
Move GraphiQl handlers to main spring-graphql module
1 parent 9178075 commit 4740696

File tree

4 files changed

+54
-25
lines changed

4 files changed

+54
-25
lines changed

graphql-spring-boot-starter/src/main/java/org/springframework/graphql/boot/GraphQlWebFluxAutoConfiguration.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.springframework.graphql.web.WebInterceptor;
4343
import org.springframework.graphql.web.webflux.GraphQlHttpHandler;
4444
import org.springframework.graphql.web.webflux.GraphQlWebSocketHandler;
45+
import org.springframework.graphql.web.webflux.GraphiQlHandler;
4546
import org.springframework.http.HttpMethod;
4647
import org.springframework.http.HttpStatus;
4748
import org.springframework.http.MediaType;
@@ -106,8 +107,8 @@ public RouterFunction<ServerResponse> graphQlEndpoint(GraphQlHttpHandler handler
106107

107108
if (properties.getGraphiql().isEnabled()) {
108109
Resource resource = resourceLoader.getResource("classpath:graphiql/index.html");
109-
GraphiQlWebFluxHandler graphiQlHandler = new GraphiQlWebFluxHandler(graphQLPath, resource);
110-
builder = builder.GET(properties.getGraphiql().getPath(), graphiQlHandler::showGraphiQlPage);
110+
GraphiQlHandler graphiQlHandler = new GraphiQlHandler(graphQLPath, resource);
111+
builder = builder.GET(properties.getGraphiql().getPath(), graphiQlHandler::handleRequest);
111112
}
112113

113114
if (properties.getSchema().getPrinter().isEnabled()) {

graphql-spring-boot-starter/src/main/java/org/springframework/graphql/boot/GraphQlWebMvcAutoConfiguration.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.springframework.graphql.web.WebInterceptor;
4848
import org.springframework.graphql.web.webmvc.GraphQlHttpHandler;
4949
import org.springframework.graphql.web.webmvc.GraphQlWebSocketHandler;
50+
import org.springframework.graphql.web.webmvc.GraphiQlHandler;
5051
import org.springframework.http.HttpMethod;
5152
import org.springframework.http.HttpStatus;
5253
import org.springframework.http.MediaType;
@@ -116,14 +117,14 @@ public RouterFunction<ServerResponse> graphQlRouterFunction(GraphQlHttpHandler h
116117

117118
if (properties.getGraphiql().isEnabled()) {
118119
Resource resource = resourceLoader.getResource("classpath:graphiql/index.html");
119-
GraphiQlWebMvcHandler graphiQLHandler = new GraphiQlWebMvcHandler(graphQLPath, resource);
120-
builder = builder.GET(properties.getGraphiql().getPath(), graphiQLHandler::showGraphiQlPage);
120+
GraphiQlHandler graphiQLHandler = new GraphiQlHandler(graphQLPath, resource);
121+
builder = builder.GET(properties.getGraphiql().getPath(), graphiQLHandler::handleRequest);
121122
}
122123

123124
if (properties.getSchema().getPrinter().isEnabled()) {
124125
SchemaPrinter printer = new SchemaPrinter();
125126
builder = builder.GET(graphQLPath + properties.getSchema().getPrinter().getPath(),
126-
(req) -> ServerResponse.ok()
127+
(request) -> ServerResponse.ok()
127128
.contentType(MediaType.TEXT_PLAIN)
128129
.body(printer.print(graphQlSource.schema())));
129130
}
Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.graphql.boot;
17+
package org.springframework.graphql.web.webflux;
18+
19+
import java.net.URI;
1820

1921
import reactor.core.publisher.Mono;
2022

@@ -24,27 +26,39 @@
2426
import org.springframework.web.reactive.function.server.ServerResponse;
2527

2628
/**
27-
* WebFlux functional handler for the GraphiQl UI.
29+
* Spring WebFlux functional handler that renders a GraphiQl UI page.
2830
*
2931
* @author Brian Clozel
32+
* @author Rossen Stoyanchev
3033
*/
31-
class GraphiQlWebFluxHandler {
34+
public class GraphiQlHandler {
3235

3336
private final String graphQlPath;
3437

3538
private final Resource graphiQlResource;
3639

37-
GraphiQlWebFluxHandler(String graphQlPath, Resource graphiQlResource) {
40+
41+
/**
42+
* Create an instance.
43+
* @param graphQlPath the path to the GraphQL endpoint
44+
* @param graphiQlResource the GraphiQL page
45+
*/
46+
public GraphiQlHandler(String graphQlPath, Resource graphiQlResource) {
3847
this.graphQlPath = graphQlPath;
3948
this.graphiQlResource = graphiQlResource;
4049
}
4150

42-
Mono<ServerResponse> showGraphiQlPage(ServerRequest request) {
43-
if (request.queryParam("path").isPresent()) {
44-
return ServerResponse.ok().contentType(MediaType.TEXT_HTML).bodyValue(this.graphiQlResource);
45-
}
46-
else {
47-
return ServerResponse.temporaryRedirect(request.uriBuilder().queryParam("path", this.graphQlPath).build()).build();
51+
52+
/**
53+
* Handle the request, serving the GraphiQL page as HTML or adding a "path"
54+
* param and redirecting back to the same URL if needed.
55+
*/
56+
public Mono<ServerResponse> handleRequest(ServerRequest request) {
57+
if (!request.queryParam("path").isPresent()) {
58+
URI url = request.uriBuilder().queryParam("path", this.graphQlPath).build();
59+
return ServerResponse.temporaryRedirect(url).build();
4860
}
61+
return ServerResponse.ok().contentType(MediaType.TEXT_HTML).bodyValue(this.graphiQlResource);
4962
}
63+
5064
}
Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,36 +14,49 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.graphql.boot;
17+
package org.springframework.graphql.web.webmvc;
18+
19+
import java.net.URI;
1820

1921
import org.springframework.core.io.Resource;
2022
import org.springframework.http.MediaType;
2123
import org.springframework.web.servlet.function.ServerRequest;
2224
import org.springframework.web.servlet.function.ServerResponse;
2325

2426
/**
25-
* Servlet.fn handler for the GraphiQl UI.
27+
* Spring MVC functional handler that renders a GraphiQl UI page.
2628
*
2729
* @author Brian Clozel
30+
* @author Rossen Stoyanchev
2831
*/
29-
class GraphiQlWebMvcHandler {
32+
public class GraphiQlHandler {
3033

3134
private final String graphQlPath;
3235

3336
private final Resource graphiQlResource;
3437

35-
GraphiQlWebMvcHandler(String graphQlPath, Resource graphiQlResource) {
38+
39+
/**
40+
* Create an instance.
41+
* @param graphQlPath the path to the GraphQL endpoint
42+
* @param graphiQlResource the GraphiQL page
43+
*/
44+
public GraphiQlHandler(String graphQlPath, Resource graphiQlResource) {
3645
this.graphQlPath = graphQlPath;
3746
this.graphiQlResource = graphiQlResource;
3847
}
3948

40-
ServerResponse showGraphiQlPage(ServerRequest request) {
41-
if (request.param("path").isPresent()) {
42-
return ServerResponse.ok().contentType(MediaType.TEXT_HTML).body(this.graphiQlResource);
43-
}
44-
else {
45-
return ServerResponse.temporaryRedirect(request.uriBuilder().queryParam("path", this.graphQlPath).build()).build();
49+
50+
/**
51+
* Handle the request, serving the GraphiQL page as HTML or adding a "path"
52+
* param and redirecting back to the same URL if needed.
53+
*/
54+
public ServerResponse handleRequest(ServerRequest request) {
55+
if (!request.param("path").isPresent()) {
56+
URI url = request.uriBuilder().queryParam("path", this.graphQlPath).build();
57+
return ServerResponse.temporaryRedirect(url).build();
4658
}
59+
return ServerResponse.ok().contentType(MediaType.TEXT_HTML).body(this.graphiQlResource);
4760
}
4861

4962
}

0 commit comments

Comments
 (0)