Move GraphiQl handlers to main spring-graphql module

This commit is contained in:
Rossen Stoyanchev
2021-07-02 10:01:10 +01:00
parent 9178075da5
commit 4740696ebf
4 changed files with 54 additions and 25 deletions

View File

@@ -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()) {

View File

@@ -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())));
}

View File

@@ -1,50 +0,0 @@
/*
* Copyright 2020-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.graphql.boot;
import reactor.core.publisher.Mono;
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;
/**
* WebFlux functional handler for the GraphiQl UI.
*
* @author Brian Clozel
*/
class GraphiQlWebFluxHandler {
private final String graphQlPath;
private final Resource graphiQlResource;
GraphiQlWebFluxHandler(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();
}
}
}

View File

@@ -1,49 +0,0 @@
/*
* Copyright 2020-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.graphql.boot;
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;
/**
* Servlet.fn handler for the GraphiQl UI.
*
* @author Brian Clozel
*/
class GraphiQlWebMvcHandler {
private final String graphQlPath;
private final Resource graphiQlResource;
GraphiQlWebMvcHandler(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();
}
}
}