From 4740696ebfb3e6b8b28870a179a4b32c72ed045a Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Fri, 2 Jul 2021 10:01:10 +0100 Subject: [PATCH] Move GraphiQl handlers to main spring-graphql module --- .../boot/GraphQlWebFluxAutoConfiguration.java | 5 +-- .../boot/GraphQlWebMvcAutoConfiguration.java | 7 ++-- .../graphql/web/webflux/GraphiQlHandler.java | 34 +++++++++++++------ .../graphql/web/webmvc/GraphiQlHandler.java | 33 ++++++++++++------ 4 files changed, 54 insertions(+), 25 deletions(-) rename graphql-spring-boot-starter/src/main/java/org/springframework/graphql/boot/GraphiQlWebFluxHandler.java => spring-graphql/src/main/java/org/springframework/graphql/web/webflux/GraphiQlHandler.java (55%) rename graphql-spring-boot-starter/src/main/java/org/springframework/graphql/boot/GraphiQlWebMvcHandler.java => spring-graphql/src/main/java/org/springframework/graphql/web/webmvc/GraphiQlHandler.java (54%) diff --git a/graphql-spring-boot-starter/src/main/java/org/springframework/graphql/boot/GraphQlWebFluxAutoConfiguration.java b/graphql-spring-boot-starter/src/main/java/org/springframework/graphql/boot/GraphQlWebFluxAutoConfiguration.java index b1bcc5ee..e9b9d6d6 100644 --- a/graphql-spring-boot-starter/src/main/java/org/springframework/graphql/boot/GraphQlWebFluxAutoConfiguration.java +++ b/graphql-spring-boot-starter/src/main/java/org/springframework/graphql/boot/GraphQlWebFluxAutoConfiguration.java @@ -42,6 +42,7 @@ import org.springframework.graphql.web.WebGraphQlHandler; import org.springframework.graphql.web.WebInterceptor; import org.springframework.graphql.web.webflux.GraphQlHttpHandler; import org.springframework.graphql.web.webflux.GraphQlWebSocketHandler; +import org.springframework.graphql.web.webflux.GraphiQlHandler; import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; @@ -106,8 +107,8 @@ public class GraphQlWebFluxAutoConfiguration { if (properties.getGraphiql().isEnabled()) { Resource resource = resourceLoader.getResource("classpath:graphiql/index.html"); - GraphiQlWebFluxHandler graphiQlHandler = new GraphiQlWebFluxHandler(graphQLPath, resource); - builder = builder.GET(properties.getGraphiql().getPath(), graphiQlHandler::showGraphiQlPage); + GraphiQlHandler graphiQlHandler = new GraphiQlHandler(graphQLPath, resource); + builder = builder.GET(properties.getGraphiql().getPath(), graphiQlHandler::handleRequest); } if (properties.getSchema().getPrinter().isEnabled()) { diff --git a/graphql-spring-boot-starter/src/main/java/org/springframework/graphql/boot/GraphQlWebMvcAutoConfiguration.java b/graphql-spring-boot-starter/src/main/java/org/springframework/graphql/boot/GraphQlWebMvcAutoConfiguration.java index 3220aabd..332db789 100644 --- a/graphql-spring-boot-starter/src/main/java/org/springframework/graphql/boot/GraphQlWebMvcAutoConfiguration.java +++ b/graphql-spring-boot-starter/src/main/java/org/springframework/graphql/boot/GraphQlWebMvcAutoConfiguration.java @@ -47,6 +47,7 @@ import org.springframework.graphql.web.WebGraphQlHandler; import org.springframework.graphql.web.WebInterceptor; import org.springframework.graphql.web.webmvc.GraphQlHttpHandler; import org.springframework.graphql.web.webmvc.GraphQlWebSocketHandler; +import org.springframework.graphql.web.webmvc.GraphiQlHandler; import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; @@ -116,14 +117,14 @@ public class GraphQlWebMvcAutoConfiguration { if (properties.getGraphiql().isEnabled()) { Resource resource = resourceLoader.getResource("classpath:graphiql/index.html"); - GraphiQlWebMvcHandler graphiQLHandler = new GraphiQlWebMvcHandler(graphQLPath, resource); - builder = builder.GET(properties.getGraphiql().getPath(), graphiQLHandler::showGraphiQlPage); + GraphiQlHandler graphiQLHandler = new GraphiQlHandler(graphQLPath, resource); + builder = builder.GET(properties.getGraphiql().getPath(), graphiQLHandler::handleRequest); } if (properties.getSchema().getPrinter().isEnabled()) { SchemaPrinter printer = new SchemaPrinter(); builder = builder.GET(graphQLPath + properties.getSchema().getPrinter().getPath(), - (req) -> ServerResponse.ok() + (request) -> ServerResponse.ok() .contentType(MediaType.TEXT_PLAIN) .body(printer.print(graphQlSource.schema()))); } diff --git a/graphql-spring-boot-starter/src/main/java/org/springframework/graphql/boot/GraphiQlWebFluxHandler.java b/spring-graphql/src/main/java/org/springframework/graphql/web/webflux/GraphiQlHandler.java similarity index 55% rename from graphql-spring-boot-starter/src/main/java/org/springframework/graphql/boot/GraphiQlWebFluxHandler.java rename to spring-graphql/src/main/java/org/springframework/graphql/web/webflux/GraphiQlHandler.java index 347efc16..00a26891 100644 --- a/graphql-spring-boot-starter/src/main/java/org/springframework/graphql/boot/GraphiQlWebFluxHandler.java +++ b/spring-graphql/src/main/java/org/springframework/graphql/web/webflux/GraphiQlHandler.java @@ -14,7 +14,9 @@ * limitations under the License. */ -package org.springframework.graphql.boot; +package org.springframework.graphql.web.webflux; + +import java.net.URI; import reactor.core.publisher.Mono; @@ -24,27 +26,39 @@ import org.springframework.web.reactive.function.server.ServerRequest; import org.springframework.web.reactive.function.server.ServerResponse; /** - * WebFlux functional handler for the GraphiQl UI. + * Spring WebFlux functional handler that renders a GraphiQl UI page. * * @author Brian Clozel + * @author Rossen Stoyanchev */ -class GraphiQlWebFluxHandler { +public class GraphiQlHandler { private final String graphQlPath; private final Resource graphiQlResource; - GraphiQlWebFluxHandler(String graphQlPath, Resource graphiQlResource) { + + /** + * Create an instance. + * @param graphQlPath the path to the GraphQL endpoint + * @param graphiQlResource the GraphiQL page + */ + public GraphiQlHandler(String graphQlPath, Resource graphiQlResource) { this.graphQlPath = graphQlPath; this.graphiQlResource = graphiQlResource; } - Mono showGraphiQlPage(ServerRequest request) { - if (request.queryParam("path").isPresent()) { - return ServerResponse.ok().contentType(MediaType.TEXT_HTML).bodyValue(this.graphiQlResource); - } - else { - return ServerResponse.temporaryRedirect(request.uriBuilder().queryParam("path", this.graphQlPath).build()).build(); + + /** + * Handle the request, serving the GraphiQL page as HTML or adding a "path" + * param and redirecting back to the same URL if needed. + */ + public Mono handleRequest(ServerRequest request) { + if (!request.queryParam("path").isPresent()) { + URI url = request.uriBuilder().queryParam("path", this.graphQlPath).build(); + return ServerResponse.temporaryRedirect(url).build(); } + return ServerResponse.ok().contentType(MediaType.TEXT_HTML).bodyValue(this.graphiQlResource); } + } diff --git a/graphql-spring-boot-starter/src/main/java/org/springframework/graphql/boot/GraphiQlWebMvcHandler.java b/spring-graphql/src/main/java/org/springframework/graphql/web/webmvc/GraphiQlHandler.java similarity index 54% rename from graphql-spring-boot-starter/src/main/java/org/springframework/graphql/boot/GraphiQlWebMvcHandler.java rename to spring-graphql/src/main/java/org/springframework/graphql/web/webmvc/GraphiQlHandler.java index 7104cf53..64605924 100644 --- a/graphql-spring-boot-starter/src/main/java/org/springframework/graphql/boot/GraphiQlWebMvcHandler.java +++ b/spring-graphql/src/main/java/org/springframework/graphql/web/webmvc/GraphiQlHandler.java @@ -14,7 +14,9 @@ * limitations under the License. */ -package org.springframework.graphql.boot; +package org.springframework.graphql.web.webmvc; + +import java.net.URI; import org.springframework.core.io.Resource; import org.springframework.http.MediaType; @@ -22,28 +24,39 @@ import org.springframework.web.servlet.function.ServerRequest; import org.springframework.web.servlet.function.ServerResponse; /** - * Servlet.fn handler for the GraphiQl UI. + * Spring MVC functional handler that renders a GraphiQl UI page. * * @author Brian Clozel + * @author Rossen Stoyanchev */ -class GraphiQlWebMvcHandler { +public class GraphiQlHandler { private final String graphQlPath; private final Resource graphiQlResource; - GraphiQlWebMvcHandler(String graphQlPath, Resource graphiQlResource) { + + /** + * Create an instance. + * @param graphQlPath the path to the GraphQL endpoint + * @param graphiQlResource the GraphiQL page + */ + public GraphiQlHandler(String graphQlPath, Resource graphiQlResource) { this.graphQlPath = graphQlPath; this.graphiQlResource = graphiQlResource; } - ServerResponse showGraphiQlPage(ServerRequest request) { - if (request.param("path").isPresent()) { - return ServerResponse.ok().contentType(MediaType.TEXT_HTML).body(this.graphiQlResource); - } - else { - return ServerResponse.temporaryRedirect(request.uriBuilder().queryParam("path", this.graphQlPath).build()).build(); + + /** + * Handle the request, serving the GraphiQL page as HTML or adding a "path" + * param and redirecting back to the same URL if needed. + */ + public ServerResponse handleRequest(ServerRequest request) { + if (!request.param("path").isPresent()) { + URI url = request.uriBuilder().queryParam("path", this.graphQlPath).build(); + return ServerResponse.temporaryRedirect(url).build(); } + return ServerResponse.ok().contentType(MediaType.TEXT_HTML).body(this.graphiQlResource); } }