From f17ae9307c1253b0859743d49745531383d55255 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Fri, 2 Jul 2021 10:25:18 +0100 Subject: [PATCH] Extract schema handling and move into main spring-graphql module --- .../boot/GraphQlWebFluxAutoConfiguration.java | 12 ++--- .../boot/GraphQlWebMvcAutoConfiguration.java | 13 +++-- .../src/main/resources/application.properties | 5 +- .../graphql/web/webflux/SchemaHandler.java | 51 +++++++++++++++++++ .../graphql/web/webmvc/SchemaHandler.java | 49 ++++++++++++++++++ 5 files changed, 116 insertions(+), 14 deletions(-) create mode 100644 spring-graphql/src/main/java/org/springframework/graphql/web/webflux/SchemaHandler.java create mode 100644 spring-graphql/src/main/java/org/springframework/graphql/web/webmvc/SchemaHandler.java 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 e9b9d6d6..5d546f11 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 @@ -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(); } 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 332db789..9732c7e0 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 @@ -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(); } diff --git a/samples/webflux-websocket/src/main/resources/application.properties b/samples/webflux-websocket/src/main/resources/application.properties index 648982fe..4aabf3df 100644 --- a/samples/webflux-websocket/src/main/resources/application.properties +++ b/samples/webflux-websocket/src/main/resources/application.properties @@ -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 \ No newline at end of file +logging.level.reactor.netty=debug diff --git a/spring-graphql/src/main/java/org/springframework/graphql/web/webflux/SchemaHandler.java b/spring-graphql/src/main/java/org/springframework/graphql/web/webflux/SchemaHandler.java new file mode 100644 index 00000000..19377d44 --- /dev/null +++ b/spring-graphql/src/main/java/org/springframework/graphql/web/webflux/SchemaHandler.java @@ -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 handleRequest(ServerRequest request) { + return ServerResponse.ok() + .contentType(MediaType.TEXT_PLAIN) + .bodyValue(this.printer.print(graphQlSource.schema())); + } + +} diff --git a/spring-graphql/src/main/java/org/springframework/graphql/web/webmvc/SchemaHandler.java b/spring-graphql/src/main/java/org/springframework/graphql/web/webmvc/SchemaHandler.java new file mode 100644 index 00000000..41ef9972 --- /dev/null +++ b/spring-graphql/src/main/java/org/springframework/graphql/web/webmvc/SchemaHandler.java @@ -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())); + } + +}