Extract schema handling and move into main spring-graphql module

This commit is contained in:
Rossen Stoyanchev
2021-07-02 10:25:18 +01:00
parent 4740696ebf
commit f17ae9307c
5 changed files with 116 additions and 14 deletions

View File

@@ -43,6 +43,7 @@ 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.graphql.web.webflux.SchemaHandler;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
@@ -104,6 +105,7 @@ public class GraphQlWebFluxAutoConfiguration {
.POST(graphQLPath,
accept(MediaType.APPLICATION_JSON).and(contentType(MediaType.APPLICATION_JSON)),
handler::handleRequest);
// @formatter:on
if (properties.getGraphiql().isEnabled()) {
Resource resource = resourceLoader.getResource("classpath:graphiql/index.html");
@@ -112,13 +114,11 @@ public class GraphQlWebFluxAutoConfiguration {
}
if (properties.getSchema().getPrinter().isEnabled()) {
SchemaPrinter printer = new SchemaPrinter();
builder = builder.GET(graphQLPath + properties.getSchema().getPrinter().getPath(),
(req) -> ServerResponse.ok()
.contentType(MediaType.TEXT_PLAIN)
.bodyValue(printer.print(graphQlSource.schema())));
SchemaHandler schemaHandler = new SchemaHandler(graphQlSource);
String schemaPath = properties.getSchema().getPrinter().getPath();
builder = builder.GET(graphQLPath + schemaPath, schemaHandler::handleRequest);
}
// @formatter:on
return builder.build();
}

View File

@@ -23,7 +23,6 @@ import java.util.stream.Collectors;
import javax.websocket.server.ServerContainer;
import graphql.GraphQL;
import graphql.schema.idl.SchemaPrinter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -48,6 +47,7 @@ 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.graphql.web.webmvc.SchemaHandler;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
@@ -114,6 +114,7 @@ public class GraphQlWebMvcAutoConfiguration {
.POST(graphQLPath,
contentType(MediaType.APPLICATION_JSON).and(accept(MediaType.APPLICATION_JSON)),
handler::handleRequest);
// @formatter:on
if (properties.getGraphiql().isEnabled()) {
Resource resource = resourceLoader.getResource("classpath:graphiql/index.html");
@@ -122,13 +123,11 @@ public class GraphQlWebMvcAutoConfiguration {
}
if (properties.getSchema().getPrinter().isEnabled()) {
SchemaPrinter printer = new SchemaPrinter();
builder = builder.GET(graphQLPath + properties.getSchema().getPrinter().getPath(),
(request) -> ServerResponse.ok()
.contentType(MediaType.TEXT_PLAIN)
.body(printer.print(graphQlSource.schema())));
SchemaHandler schemaHandler = new SchemaHandler(graphQlSource);
String schemaPath = properties.getSchema().getPrinter().getPath();
builder = builder.GET(graphQLPath + schemaPath, schemaHandler::handleRequest);
}
// @formatter:on
return builder.build();
}

View File

@@ -1,6 +1,9 @@
spring.graphql.websocket.path=/graphql
spring.graphql.schema.printer.enabled=true
management.endpoints.web.exposure.include=health,metrics,info
logging.level.org.springframework.web=debug
logging.level.org.springframework.http=debug
logging.level.org.springframework.graphql=debug
logging.level.reactor.netty=debug
logging.level.reactor.netty=debug

View File

@@ -0,0 +1,51 @@
/*
* 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.web.webflux;
import graphql.schema.idl.SchemaPrinter;
import reactor.core.publisher.Mono;
import org.springframework.graphql.execution.GraphQlSource;
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 the
* {@link graphql.schema.GraphQLSchema} printed via {@link SchemaPrinter}.
*
* @author Rossen Stoyanchev
*/
public class SchemaHandler {
private final GraphQlSource graphQlSource;
private final SchemaPrinter printer = new SchemaPrinter();
public SchemaHandler(GraphQlSource graphQlSource) {
this.graphQlSource = graphQlSource;
}
public Mono<ServerResponse> handleRequest(ServerRequest request) {
return ServerResponse.ok()
.contentType(MediaType.TEXT_PLAIN)
.bodyValue(this.printer.print(graphQlSource.schema()));
}
}

View File

@@ -0,0 +1,49 @@
/*
* Copyright 2002-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.web.webmvc;
import graphql.schema.idl.SchemaPrinter;
import org.springframework.graphql.execution.GraphQlSource;
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 the
* {@link graphql.schema.GraphQLSchema} printed via {@link SchemaPrinter}.
*
* @author Rossen Stoyanchev
*/
public class SchemaHandler {
private final GraphQlSource graphQlSource;
private final SchemaPrinter printer = new SchemaPrinter();
public SchemaHandler(GraphQlSource graphQlSource) {
this.graphQlSource = graphQlSource;
}
public ServerResponse handleRequest(ServerRequest request) {
return ServerResponse.ok()
.contentType(MediaType.TEXT_PLAIN)
.body(this.printer.print(graphQlSource.schema()));
}
}