Skip to content

Commit 9d4ebfe

Browse files
snicollbclozel
authored andcommitted
Polish
1 parent 4a1ee14 commit 9d4ebfe

File tree

16 files changed

+91
-100
lines changed

16 files changed

+91
-100
lines changed

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
3232
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
3333
import org.springframework.boot.context.properties.EnableConfigurationProperties;
34-
import org.springframework.context.ApplicationContext;
3534
import org.springframework.context.annotation.Bean;
3635
import org.springframework.context.annotation.Configuration;
3736
import org.springframework.core.io.Resource;
@@ -65,18 +64,18 @@ public static class GraphQlSourceConfiguration {
6564

6665
@Bean
6766
@ConditionalOnMissingBean
68-
public RuntimeWiring runtimeWiring(ObjectProvider<RuntimeWiringCustomizer> customizers) {
67+
public RuntimeWiring runtimeWiring(ObjectProvider<RuntimeWiringBuilderCustomizer> customizers) {
6968
RuntimeWiring.Builder builder = RuntimeWiring.newRuntimeWiring();
7069
customizers.orderedStream().forEach((customizer) -> customizer.customize(builder));
7170
return builder.build();
7271
}
7372

7473
@Bean
75-
public GraphQlSource.Builder graphQlSourceBuilder(ApplicationContext applicationContext, GraphQlProperties properties,
74+
public GraphQlSource.Builder graphQlSourceBuilder(ResourcePatternResolver resourcePatternResolver, GraphQlProperties properties,
7675
RuntimeWiring runtimeWiring, ObjectProvider<DataFetcherExceptionResolver> exceptionResolversProvider,
7776
ObjectProvider<Instrumentation> instrumentationsProvider) throws IOException {
7877

79-
List<Resource> schemaResources = resolveSchemaResources(applicationContext, properties.getSchema().getLocations());
78+
List<Resource> schemaResources = resolveSchemaResources(resourcePatternResolver, properties.getSchema().getLocations());
8079
return GraphQlSource.builder().schemaResources(schemaResources.toArray(new Resource[0]))
8180
.runtimeWiring(runtimeWiring)
8281
.exceptionResolvers(exceptionResolversProvider.orderedStream().collect(Collectors.toList()))

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class GraphQlProperties {
4242

4343
private final GraphiQL graphiql = new GraphiQL();
4444

45-
private final WebSocket websocket = new WebSocket();
45+
private final Websocket websocket = new Websocket();
4646

4747
public String getPath() {
4848
return this.path;
@@ -60,7 +60,7 @@ public GraphiQL getGraphiql() {
6060
return this.graphiql;
6161
}
6262

63-
public WebSocket getWebsocket() {
63+
public Websocket getWebsocket() {
6464
return this.websocket;
6565
}
6666

@@ -152,7 +152,7 @@ public void setEnabled(boolean enabled) {
152152
}
153153
}
154154

155-
public static class WebSocket {
155+
public static class Websocket {
156156

157157
/**
158158
* Path of the GraphQL WebSocket subscription endpoint.
Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,15 @@
6565
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE)
6666
@ConditionalOnClass(GraphQL.class)
6767
@ConditionalOnBean(GraphQlSource.class)
68-
@AutoConfigureAfter(GraphQlAutoConfiguration.class)
69-
public class WebFluxGraphQlAutoConfiguration {
68+
@AutoConfigureAfter(GraphQlServiceAutoConfiguration.class)
69+
public class GraphQlWebFluxAutoConfiguration {
7070

71-
private static final Log logger = LogFactory.getLog(WebFluxGraphQlAutoConfiguration.class);
71+
private static final Log logger = LogFactory.getLog(GraphQlWebFluxAutoConfiguration.class);
7272

7373
@Bean
74+
@ConditionalOnBean(GraphQlService.class)
7475
@ConditionalOnMissingBean
75-
public WebGraphQlHandler webGraphQlHandler(ObjectProvider<WebInterceptor> interceptors, GraphQlService service) {
76+
public WebGraphQlHandler webGraphQlHandler(GraphQlService service, ObjectProvider<WebInterceptor> interceptors) {
7677
return WebGraphQlHandler.builder(service)
7778
.interceptors(interceptors.orderedStream().collect(Collectors.toList())).build();
7879
}
@@ -86,7 +87,6 @@ public GraphQlHttpHandler graphQlHttpHandler(WebGraphQlHandler webGraphQlHandler
8687
@Bean
8788
public RouterFunction<ServerResponse> graphQlEndpoint(GraphQlHttpHandler handler, GraphQlSource graphQlSource,
8889
GraphQlProperties properties, ResourceLoader resourceLoader) {
89-
9090
String graphQLPath = properties.getPath();
9191
if (logger.isInfoEnabled()) {
9292
logger.info("GraphQL endpoint HTTP POST " + graphQLPath);
@@ -96,7 +96,7 @@ public RouterFunction<ServerResponse> graphQlEndpoint(GraphQlHttpHandler handler
9696
.POST(graphQLPath, accept(MediaType.APPLICATION_JSON).and(contentType(MediaType.APPLICATION_JSON)), handler::handleRequest);
9797
if (properties.getGraphiql().isEnabled()) {
9898
Resource resource = resourceLoader.getResource("classpath:graphiql/index.html");
99-
WebFluxGraphiQlHandler graphiQlHandler = new WebFluxGraphiQlHandler(graphQLPath, resource);
99+
GraphiQlWebFluxHandler graphiQlHandler = new GraphiQlWebFluxHandler(graphQLPath, resource);
100100
builder = builder.GET(properties.getGraphiql().getPath(), graphiQlHandler::showGraphiQlPage);
101101
}
102102
if (properties.getSchema().getPrinter().isEnabled()) {
@@ -118,15 +118,13 @@ public static class WebSocketConfiguration {
118118
@ConditionalOnMissingBean
119119
public GraphQlWebSocketHandler graphQlWebSocketHandler(WebGraphQlHandler webGraphQlHandler,
120120
GraphQlProperties properties, ServerCodecConfigurer configurer) {
121-
122121
return new GraphQlWebSocketHandler(webGraphQlHandler, configurer,
123122
properties.getWebsocket().getConnectionInitTimeout());
124123
}
125124

126125
@Bean
127126
public HandlerMapping graphQlWebSocketEndpoint(GraphQlWebSocketHandler graphQlWebSocketHandler,
128127
GraphQlProperties properties) {
129-
130128
String path = properties.getWebsocket().getPath();
131129
if (logger.isInfoEnabled()) {
132130
logger.info("GraphQL endpoint WebSocket " + path);
Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,16 @@
7272
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
7373
@ConditionalOnClass(GraphQL.class)
7474
@ConditionalOnBean(GraphQlSource.class)
75-
@AutoConfigureAfter(GraphQlAutoConfiguration.class)
76-
public class WebMvcGraphQlAutoConfiguration {
75+
@AutoConfigureAfter(GraphQlServiceAutoConfiguration.class)
76+
public class GraphQlWebMvcAutoConfiguration {
7777

78-
private static final Log logger = LogFactory.getLog(WebMvcGraphQlAutoConfiguration.class);
78+
private static final Log logger = LogFactory.getLog(GraphQlWebMvcAutoConfiguration.class);
7979

8080
@Bean
81+
@ConditionalOnBean(GraphQlService.class)
8182
@ConditionalOnMissingBean
82-
public WebGraphQlHandler webGraphQlHandler(ObjectProvider<WebInterceptor> interceptorsProvider,
83-
GraphQlService service, ObjectProvider<ThreadLocalAccessor> accessorsProvider) {
84-
83+
public WebGraphQlHandler webGraphQlHandler(GraphQlService service, ObjectProvider<WebInterceptor> interceptorsProvider,
84+
ObjectProvider<ThreadLocalAccessor> accessorsProvider) {
8585
return WebGraphQlHandler.builder(service)
8686
.interceptors(interceptorsProvider.orderedStream().collect(Collectors.toList()))
8787
.threadLocalAccessors(accessorsProvider.orderedStream().collect(Collectors.toList())).build();
@@ -96,7 +96,6 @@ public GraphQlHttpHandler graphQlHttpHandler(WebGraphQlHandler webGraphQlHandler
9696
@Bean
9797
public RouterFunction<ServerResponse> graphQlRouterFunction(GraphQlHttpHandler handler, GraphQlSource graphQlSource,
9898
GraphQlProperties properties, ResourceLoader resourceLoader) {
99-
10099
String graphQLPath = properties.getPath();
101100
if (logger.isInfoEnabled()) {
102101
logger.info("GraphQL endpoint HTTP POST " + graphQLPath);
@@ -106,7 +105,7 @@ public RouterFunction<ServerResponse> graphQlRouterFunction(GraphQlHttpHandler h
106105
.POST(graphQLPath, contentType(MediaType.APPLICATION_JSON).and(accept(MediaType.APPLICATION_JSON)), handler::handleRequest);
107106
if (properties.getGraphiql().isEnabled()) {
108107
Resource resource = resourceLoader.getResource("classpath:graphiql/index.html");
109-
WebMvcGraphiQlHandler graphiQLHandler = new WebMvcGraphiQlHandler(graphQLPath, resource);
108+
GraphiQlWebMvcHandler graphiQLHandler = new GraphiQlWebMvcHandler(graphQLPath, resource);
110109
builder = builder.GET(properties.getGraphiql().getPath(), graphiQLHandler::showGraphiQlPage);
111110
}
112111
if (properties.getSchema().getPrinter().isEnabled()) {
@@ -121,15 +120,14 @@ public RouterFunction<ServerResponse> graphQlRouterFunction(GraphQlHttpHandler h
121120
}
122121

123122
@Configuration(proxyBeanMethods = false)
124-
@ConditionalOnClass({ServerContainer.class, WebSocketHandler.class})
123+
@ConditionalOnClass({ ServerContainer.class, WebSocketHandler.class })
125124
@ConditionalOnProperty(prefix = "spring.graphql.websocket", name = "path")
126125
public static class WebSocketConfiguration {
127126

128127
@Bean
129128
@ConditionalOnMissingBean
130129
public GraphQlWebSocketHandler graphQlWebSocketHandler(WebGraphQlHandler webGraphQlHandler,
131130
GraphQlProperties properties, HttpMessageConverters converters) {
132-
133131
// @formatter:off
134132
HttpMessageConverter<?> converter = converters.getConverters().stream()
135133
.filter((candidate) -> candidate.canRead(Map.class, MediaType.APPLICATION_JSON))
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,21 @@
2525

2626
/**
2727
* WebFlux functional handler for the GraphiQl UI.
28+
*
2829
* @author Brian Clozel
2930
*/
30-
class WebFluxGraphiQlHandler {
31+
class GraphiQlWebFluxHandler {
3132

3233
private final String graphQlPath;
3334

3435
private final Resource graphiQlResource;
3536

36-
public WebFluxGraphiQlHandler(String graphQlPath, Resource graphiQlResource) {
37+
GraphiQlWebFluxHandler(String graphQlPath, Resource graphiQlResource) {
3738
this.graphQlPath = graphQlPath;
3839
this.graphiQlResource = graphiQlResource;
3940
}
4041

41-
public Mono<ServerResponse> showGraphiQlPage(ServerRequest request) {
42+
Mono<ServerResponse> showGraphiQlPage(ServerRequest request) {
4243
if (request.queryParam("path").isPresent()) {
4344
return ServerResponse.ok().contentType(MediaType.TEXT_HTML).bodyValue(this.graphiQlResource);
4445
}
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,27 @@
2323

2424
/**
2525
* Servlet.fn handler for the GraphiQl UI.
26+
*
2627
* @author Brian Clozel
2728
*/
28-
class WebMvcGraphiQlHandler {
29+
class GraphiQlWebMvcHandler {
2930

3031
private final String graphQlPath;
3132

3233
private final Resource graphiQlResource;
3334

34-
public WebMvcGraphiQlHandler(String graphQlPath, Resource graphiQlResource) {
35+
GraphiQlWebMvcHandler(String graphQlPath, Resource graphiQlResource) {
3536
this.graphQlPath = graphQlPath;
3637
this.graphiQlResource = graphiQlResource;
3738
}
3839

39-
public ServerResponse showGraphiQlPage(ServerRequest request) {
40+
ServerResponse showGraphiQlPage(ServerRequest request) {
4041
if (request.param("path").isPresent()) {
4142
return ServerResponse.ok().contentType(MediaType.TEXT_HTML).body(this.graphiQlResource);
4243
}
4344
else {
4445
return ServerResponse.temporaryRedirect(request.uriBuilder().queryParam("path", this.graphQlPath).build()).build();
4546
}
4647
}
48+
4749
}

graphql-spring-boot-starter/src/main/java/org/springframework/graphql/boot/RuntimeWiringCustomizer.java renamed to graphql-spring-boot-starter/src/main/java/org/springframework/graphql/boot/RuntimeWiringBuilderCustomizer.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,19 @@
1919
import graphql.schema.idl.RuntimeWiring;
2020

2121
/**
22-
* Callback interface that can be used to customize the GraphQL
23-
* {@link RuntimeWiring.Builder}.
22+
* Callback interface that can be implemented by beans wishing to customize the
23+
* {@link RuntimeWiring} via a {@link RuntimeWiring.Builder} whilst retaining default
24+
* auto-configuration.
2425
*
2526
* @author Brian Clozel
2627
* @since 1.0.0
2728
*/
2829
@FunctionalInterface
29-
public interface RuntimeWiringCustomizer {
30+
public interface RuntimeWiringBuilderCustomizer {
3031

3132
/**
32-
* Callback to customize a {@link RuntimeWiring.Builder} instance.
33-
* @param builder builder instance to customize
33+
* Customize the {@link RuntimeWiring.Builder} instance.
34+
* @param builder builder the builder to customize
3435
*/
3536
void customize(RuntimeWiring.Builder builder);
3637

graphql-spring-boot-starter/src/main/resources/META-INF/spring.factories

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
22
org.springframework.graphql.boot.actuate.metrics.GraphQlMetricsAutoConfiguration,\
33
org.springframework.graphql.boot.GraphQlAutoConfiguration,\
44
org.springframework.graphql.boot.GraphQlServiceAutoConfiguration,\
5-
org.springframework.graphql.boot.WebFluxGraphQlAutoConfiguration,\
6-
org.springframework.graphql.boot.WebMvcGraphQlAutoConfiguration
5+
org.springframework.graphql.boot.GraphQlWebFluxAutoConfiguration,\
6+
org.springframework.graphql.boot.GraphQlWebMvcAutoConfiguration
7+
78
# Spring Test @AutoConfigureGraphQlTester
89
org.springframework.graphql.boot.test.tester.AutoConfigureGraphQlTester=\
910
org.springframework.graphql.boot.test.tester.WebTestClientMockMvcAutoConfiguration,\
1011
org.springframework.graphql.boot.test.tester.GraphQlTesterAutoConfiguration
12+
1113
# Spring Test ContextCustomizerFactories
1214
org.springframework.test.context.ContextCustomizerFactory=\
1315
org.springframework.graphql.boot.test.tester.GraphQlTesterContextCustomizerFactory
Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@
2727
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
2828
import org.springframework.boot.autoconfigure.web.reactive.HttpHandlerAutoConfiguration;
2929
import org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration;
30-
import org.springframework.boot.test.context.runner.ContextConsumer;
3130
import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner;
32-
import org.springframework.context.ApplicationContext;
3331
import org.springframework.context.annotation.Bean;
3432
import org.springframework.context.annotation.Configuration;
3533
import org.springframework.graphql.web.WebInterceptor;
@@ -40,23 +38,28 @@
4038

4139
// @formatter:off
4240

43-
class WebFluxApplicationContextTests {
44-
45-
private static final AutoConfigurations AUTO_CONFIGURATIONS = AutoConfigurations.of(
46-
HttpHandlerAutoConfiguration.class, WebFluxAutoConfiguration.class, CodecsAutoConfiguration.class,
47-
JacksonAutoConfiguration.class, GraphQlAutoConfiguration.class, GraphQlServiceAutoConfiguration.class,
48-
WebFluxGraphQlAutoConfiguration.class);
41+
class GraphQlWebFluxAutoConfigurationTests {
4942

5043
private static final String BASE_URL = "https://spring.example.org/graphql";
5144

45+
private final ReactiveWebApplicationContextRunner contextRunner = new ReactiveWebApplicationContextRunner()
46+
.withConfiguration(AutoConfigurations.of(HttpHandlerAutoConfiguration.class, WebFluxAutoConfiguration.class,
47+
CodecsAutoConfiguration.class, JacksonAutoConfiguration.class, GraphQlAutoConfiguration.class,
48+
GraphQlServiceAutoConfiguration.class, GraphQlWebFluxAutoConfiguration.class))
49+
.withUserConfiguration(DataFetchersConfiguration.class, CustomWebInterceptor.class)
50+
.withPropertyValues(
51+
"spring.main.web-application-type=reactive",
52+
"spring.graphql.schema.printer.enabled=true",
53+
"spring.graphql.schema.locations=classpath:books/");
54+
5255
@Test
5356
void query() {
5457
testWithWebClient((client) -> {
5558
String query = "{" +
5659
" bookById(id: \\\"book-1\\\"){ " +
5760
" id" +
5861
" name" +
59-
" pageCount" +
62+
" pageCount" +
6063
" author" +
6164
" }" +
6265
"}";
@@ -124,7 +127,7 @@ void schemaEndpoint() {
124127
}
125128

126129
private void testWithWebClient(Consumer<WebTestClient> consumer) {
127-
testWithApplicationContext((context) -> {
130+
this.contextRunner.run((context) -> {
128131
WebTestClient client = WebTestClient.bindToApplicationContext(context)
129132
.configureClient()
130133
.defaultHeaders((headers) -> {
@@ -137,22 +140,11 @@ private void testWithWebClient(Consumer<WebTestClient> consumer) {
137140
});
138141
}
139142

140-
private void testWithApplicationContext(ContextConsumer<ApplicationContext> consumer) {
141-
new ReactiveWebApplicationContextRunner()
142-
.withConfiguration(AUTO_CONFIGURATIONS)
143-
.withUserConfiguration(DataFetchersConfiguration.class, CustomWebInterceptor.class)
144-
.withPropertyValues(
145-
"spring.main.web-application-type=reactive",
146-
"spring.graphql.schema.printer.enabled=true",
147-
"spring.graphql.schema.locations=classpath:books/")
148-
.run(consumer);
149-
}
150-
151143
@Configuration(proxyBeanMethods = false)
152-
public static class DataFetchersConfiguration {
144+
static class DataFetchersConfiguration {
153145

154146
@Bean
155-
public RuntimeWiringCustomizer bookDataFetcher() {
147+
RuntimeWiringBuilderCustomizer bookDataFetcher() {
156148
return (runtimeWiring) ->
157149
runtimeWiring.type(TypeRuntimeWiring.newTypeWiring("Query")
158150
.dataFetcher("bookById", GraphQlDataFetchers.getBookByIdDataFetcher()));
@@ -161,10 +153,10 @@ public RuntimeWiringCustomizer bookDataFetcher() {
161153
}
162154

163155
@Configuration(proxyBeanMethods = false)
164-
public static class CustomWebInterceptor {
156+
static class CustomWebInterceptor {
165157

166158
@Bean
167-
public WebInterceptor customWebInterceptor() {
159+
WebInterceptor customWebInterceptor() {
168160
return (input, next) -> next.handle(input).map((output) ->
169161
output.transform((builder) -> builder.responseHeader("X-Custom-Header", "42")));
170162
}

0 commit comments

Comments
 (0)