Skip to content

Commit f17ae93

Browse files
committed
Extract schema handling and move into main spring-graphql module
1 parent 4740696 commit f17ae93

File tree

5 files changed

+116
-14
lines changed

5 files changed

+116
-14
lines changed

graphql-spring-boot-starter/src/main/java/org/springframework/graphql/boot/GraphQlWebFluxAutoConfiguration.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.springframework.graphql.web.webflux.GraphQlHttpHandler;
4444
import org.springframework.graphql.web.webflux.GraphQlWebSocketHandler;
4545
import org.springframework.graphql.web.webflux.GraphiQlHandler;
46+
import org.springframework.graphql.web.webflux.SchemaHandler;
4647
import org.springframework.http.HttpMethod;
4748
import org.springframework.http.HttpStatus;
4849
import org.springframework.http.MediaType;
@@ -104,6 +105,7 @@ public RouterFunction<ServerResponse> graphQlEndpoint(GraphQlHttpHandler handler
104105
.POST(graphQLPath,
105106
accept(MediaType.APPLICATION_JSON).and(contentType(MediaType.APPLICATION_JSON)),
106107
handler::handleRequest);
108+
// @formatter:on
107109

108110
if (properties.getGraphiql().isEnabled()) {
109111
Resource resource = resourceLoader.getResource("classpath:graphiql/index.html");
@@ -112,13 +114,11 @@ public RouterFunction<ServerResponse> graphQlEndpoint(GraphQlHttpHandler handler
112114
}
113115

114116
if (properties.getSchema().getPrinter().isEnabled()) {
115-
SchemaPrinter printer = new SchemaPrinter();
116-
builder = builder.GET(graphQLPath + properties.getSchema().getPrinter().getPath(),
117-
(req) -> ServerResponse.ok()
118-
.contentType(MediaType.TEXT_PLAIN)
119-
.bodyValue(printer.print(graphQlSource.schema())));
117+
SchemaHandler schemaHandler = new SchemaHandler(graphQlSource);
118+
String schemaPath = properties.getSchema().getPrinter().getPath();
119+
builder = builder.GET(graphQLPath + schemaPath, schemaHandler::handleRequest);
120120
}
121-
// @formatter:on
121+
122122
return builder.build();
123123
}
124124

graphql-spring-boot-starter/src/main/java/org/springframework/graphql/boot/GraphQlWebMvcAutoConfiguration.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import javax.websocket.server.ServerContainer;
2424

2525
import graphql.GraphQL;
26-
import graphql.schema.idl.SchemaPrinter;
2726
import org.apache.commons.logging.Log;
2827
import org.apache.commons.logging.LogFactory;
2928

@@ -48,6 +47,7 @@
4847
import org.springframework.graphql.web.webmvc.GraphQlHttpHandler;
4948
import org.springframework.graphql.web.webmvc.GraphQlWebSocketHandler;
5049
import org.springframework.graphql.web.webmvc.GraphiQlHandler;
50+
import org.springframework.graphql.web.webmvc.SchemaHandler;
5151
import org.springframework.http.HttpMethod;
5252
import org.springframework.http.HttpStatus;
5353
import org.springframework.http.MediaType;
@@ -114,6 +114,7 @@ public RouterFunction<ServerResponse> graphQlRouterFunction(GraphQlHttpHandler h
114114
.POST(graphQLPath,
115115
contentType(MediaType.APPLICATION_JSON).and(accept(MediaType.APPLICATION_JSON)),
116116
handler::handleRequest);
117+
// @formatter:on
117118

118119
if (properties.getGraphiql().isEnabled()) {
119120
Resource resource = resourceLoader.getResource("classpath:graphiql/index.html");
@@ -122,13 +123,11 @@ public RouterFunction<ServerResponse> graphQlRouterFunction(GraphQlHttpHandler h
122123
}
123124

124125
if (properties.getSchema().getPrinter().isEnabled()) {
125-
SchemaPrinter printer = new SchemaPrinter();
126-
builder = builder.GET(graphQLPath + properties.getSchema().getPrinter().getPath(),
127-
(request) -> ServerResponse.ok()
128-
.contentType(MediaType.TEXT_PLAIN)
129-
.body(printer.print(graphQlSource.schema())));
126+
SchemaHandler schemaHandler = new SchemaHandler(graphQlSource);
127+
String schemaPath = properties.getSchema().getPrinter().getPath();
128+
builder = builder.GET(graphQLPath + schemaPath, schemaHandler::handleRequest);
130129
}
131-
// @formatter:on
130+
132131
return builder.build();
133132
}
134133

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
spring.graphql.websocket.path=/graphql
2+
spring.graphql.schema.printer.enabled=true
3+
24
management.endpoints.web.exposure.include=health,metrics,info
5+
36
logging.level.org.springframework.web=debug
47
logging.level.org.springframework.http=debug
58
logging.level.org.springframework.graphql=debug
6-
logging.level.reactor.netty=debug
9+
logging.level.reactor.netty=debug
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2020-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.graphql.web.webflux;
18+
19+
import graphql.schema.idl.SchemaPrinter;
20+
import reactor.core.publisher.Mono;
21+
22+
import org.springframework.graphql.execution.GraphQlSource;
23+
import org.springframework.http.MediaType;
24+
import org.springframework.web.reactive.function.server.ServerRequest;
25+
import org.springframework.web.reactive.function.server.ServerResponse;
26+
27+
/**
28+
* Spring WebFlux functional handler that renders the
29+
* {@link graphql.schema.GraphQLSchema} printed via {@link SchemaPrinter}.
30+
*
31+
* @author Rossen Stoyanchev
32+
*/
33+
public class SchemaHandler {
34+
35+
private final GraphQlSource graphQlSource;
36+
37+
private final SchemaPrinter printer = new SchemaPrinter();
38+
39+
40+
public SchemaHandler(GraphQlSource graphQlSource) {
41+
this.graphQlSource = graphQlSource;
42+
}
43+
44+
45+
public Mono<ServerResponse> handleRequest(ServerRequest request) {
46+
return ServerResponse.ok()
47+
.contentType(MediaType.TEXT_PLAIN)
48+
.bodyValue(this.printer.print(graphQlSource.schema()));
49+
}
50+
51+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2002-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.graphql.web.webmvc;
17+
18+
import graphql.schema.idl.SchemaPrinter;
19+
20+
import org.springframework.graphql.execution.GraphQlSource;
21+
import org.springframework.http.MediaType;
22+
import org.springframework.web.servlet.function.ServerRequest;
23+
import org.springframework.web.servlet.function.ServerResponse;
24+
25+
/**
26+
* Spring MVC functional handler that renders the
27+
* {@link graphql.schema.GraphQLSchema} printed via {@link SchemaPrinter}.
28+
*
29+
* @author Rossen Stoyanchev
30+
*/
31+
public class SchemaHandler {
32+
33+
private final GraphQlSource graphQlSource;
34+
35+
private final SchemaPrinter printer = new SchemaPrinter();
36+
37+
38+
public SchemaHandler(GraphQlSource graphQlSource) {
39+
this.graphQlSource = graphQlSource;
40+
}
41+
42+
43+
public ServerResponse handleRequest(ServerRequest request) {
44+
return ServerResponse.ok()
45+
.contentType(MediaType.TEXT_PLAIN)
46+
.body(this.printer.print(graphQlSource.schema()));
47+
}
48+
49+
}

0 commit comments

Comments
 (0)