From 6b1d5ebf64dd1cef8955dcbb87d0de5b0a528e2a Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Thu, 2 Dec 2021 15:53:36 +0000 Subject: [PATCH] Move graphiql index.html to spring-graphql Closes gh-209 --- .../boot/GraphQlWebFluxAutoConfiguration.java | 3 +- .../boot/GraphQlWebMvcAutoConfiguration.java | 3 +- .../graphql/web/webflux/GraphiQlHandler.java | 39 ++++++++++++------- .../graphql/web/webmvc/GraphiQlHandler.java | 39 ++++++++++++------- .../src/main/resources/graphiql/index.html | 0 5 files changed, 54 insertions(+), 30 deletions(-) rename {graphql-spring-boot-starter => spring-graphql}/src/main/resources/graphiql/index.html (100%) 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 26a7ef6d..4566c9cb 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 @@ -108,8 +108,7 @@ public class GraphQlWebFluxAutoConfiguration { handler::handleRequest); if (properties.getGraphiql().isEnabled()) { - Resource resource = resourceLoader.getResource("classpath:graphiql/index.html"); - GraphiQlHandler graphiQlHandler = new GraphiQlHandler(graphQLPath, resource); + GraphiQlHandler graphiQlHandler = new GraphiQlHandler(graphQLPath); builder = builder.GET(properties.getGraphiql().getPath(), graphiQlHandler::handleRequest); } 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 60f28796..321e0071 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 @@ -117,8 +117,7 @@ public class GraphQlWebMvcAutoConfiguration { handler::handleRequest); if (properties.getGraphiql().isEnabled()) { - Resource resource = resourceLoader.getResource("classpath:graphiql/index.html"); - GraphiQlHandler graphiQLHandler = new GraphiQlHandler(graphQLPath, resource); + GraphiQlHandler graphiQLHandler = new GraphiQlHandler(graphQLPath); builder = builder.GET(properties.getGraphiql().getPath(), graphiQLHandler::handleRequest); } diff --git a/spring-graphql/src/main/java/org/springframework/graphql/web/webflux/GraphiQlHandler.java b/spring-graphql/src/main/java/org/springframework/graphql/web/webflux/GraphiQlHandler.java index 00a26891..820b4213 100644 --- a/spring-graphql/src/main/java/org/springframework/graphql/web/webflux/GraphiQlHandler.java +++ b/spring-graphql/src/main/java/org/springframework/graphql/web/webflux/GraphiQlHandler.java @@ -20,45 +20,58 @@ import java.net.URI; import reactor.core.publisher.Mono; +import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.http.MediaType; import org.springframework.web.reactive.function.server.ServerRequest; import org.springframework.web.reactive.function.server.ServerResponse; /** - * Spring WebFlux functional handler that renders a GraphiQl UI page. + * Spring WebFlux handler to serve a GraphiQl UI page. * * @author Brian Clozel * @author Rossen Stoyanchev + * @since 1.0.0 */ public class GraphiQlHandler { private final String graphQlPath; - private final Resource graphiQlResource; + private final Resource htmlResource; /** - * Create an instance. + * Constructor that serves the default {@code graphiql/index.html} included + * in the {@code spring-graphql} module. * @param graphQlPath the path to the GraphQL endpoint - * @param graphiQlResource the GraphiQL page */ - public GraphiQlHandler(String graphQlPath, Resource graphiQlResource) { + public GraphiQlHandler(String graphQlPath) { + this(graphQlPath, new ClassPathResource("graphiql/index.html")); + } + + /** + * Constructor with the HTML page to serve. + * @param graphQlPath the path to the GraphQL endpoint + * @param htmlResource the GraphiQL page to serve + */ + public GraphiQlHandler(String graphQlPath, Resource htmlResource) { this.graphQlPath = graphQlPath; - this.graphiQlResource = graphiQlResource; + this.htmlResource = htmlResource; } /** - * Handle the request, serving the GraphiQL page as HTML or adding a "path" - * param and redirecting back to the same URL if needed. + * Render the GraphiQL page as "text/html", or if the "path" query parameter + * is missing, add it and redirect back to the same URL. */ 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); + return (request.queryParam("path").isPresent() ? + ServerResponse.ok().contentType(MediaType.TEXT_HTML).bodyValue(this.htmlResource) : + ServerResponse.temporaryRedirect(getRedirectUrl(request)).build()); + } + + private URI getRedirectUrl(ServerRequest request) { + return request.uriBuilder().queryParam("path", this.graphQlPath).build(); } } diff --git a/spring-graphql/src/main/java/org/springframework/graphql/web/webmvc/GraphiQlHandler.java b/spring-graphql/src/main/java/org/springframework/graphql/web/webmvc/GraphiQlHandler.java index 64605924..441932cf 100644 --- a/spring-graphql/src/main/java/org/springframework/graphql/web/webmvc/GraphiQlHandler.java +++ b/spring-graphql/src/main/java/org/springframework/graphql/web/webmvc/GraphiQlHandler.java @@ -18,45 +18,58 @@ package org.springframework.graphql.web.webmvc; import java.net.URI; +import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.http.MediaType; import org.springframework.web.servlet.function.ServerRequest; import org.springframework.web.servlet.function.ServerResponse; /** - * Spring MVC functional handler that renders a GraphiQl UI page. + * Spring MVC handler to serve a GraphiQl UI page. * * @author Brian Clozel * @author Rossen Stoyanchev + * @since 1.0.0 */ public class GraphiQlHandler { private final String graphQlPath; - private final Resource graphiQlResource; + private final Resource htmlResource; /** - * Create an instance. + * Constructor that serves the default {@code graphiql/index.html} included + * in the {@code spring-graphql} module. * @param graphQlPath the path to the GraphQL endpoint - * @param graphiQlResource the GraphiQL page */ - public GraphiQlHandler(String graphQlPath, Resource graphiQlResource) { + public GraphiQlHandler(String graphQlPath) { + this(graphQlPath, new ClassPathResource("graphiql/index.html")); + } + + /** + * Constructor with the HTML page to serve. + * @param graphQlPath the path to the GraphQL endpoint + * @param htmlResource the GraphiQL page to serve + */ + public GraphiQlHandler(String graphQlPath, Resource htmlResource) { this.graphQlPath = graphQlPath; - this.graphiQlResource = graphiQlResource; + this.htmlResource = htmlResource; } /** - * Handle the request, serving the GraphiQL page as HTML or adding a "path" - * param and redirecting back to the same URL if needed. + * Render the GraphiQL page as "text/html", or if the "path" query parameter + * is missing, add it and redirect back to the same URL. */ 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); + return (request.param("path").isPresent() ? + ServerResponse.ok().contentType(MediaType.TEXT_HTML).body(this.htmlResource) : + ServerResponse.temporaryRedirect(getRedirectUrl(request)).build()); + } + + private URI getRedirectUrl(ServerRequest request) { + return request.uriBuilder().queryParam("path", this.graphQlPath).build(); } } diff --git a/graphql-spring-boot-starter/src/main/resources/graphiql/index.html b/spring-graphql/src/main/resources/graphiql/index.html similarity index 100% rename from graphql-spring-boot-starter/src/main/resources/graphiql/index.html rename to spring-graphql/src/main/resources/graphiql/index.html