Move GraphiQl handlers to main spring-graphql module
This commit is contained in:
@@ -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()) {
|
||||
|
||||
@@ -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())));
|
||||
}
|
||||
|
||||
@@ -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<ServerResponse> 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<ServerResponse> 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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user