From 043e5ff0f52ab0b2c3400ae9f265806e5e24713a Mon Sep 17 00:00:00 2001 From: dugenkui03 Date: Mon, 21 Feb 2022 22:45:41 +0800 Subject: [PATCH 1/2] Reuse BatchLoaderRegistry in GraphQlService See gh-29929 --- .../graphql/GraphQlAutoConfiguration.java | 4 ++-- .../graphql/GraphQlAutoConfigurationTests.java | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/graphql/GraphQlAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/graphql/GraphQlAutoConfiguration.java index d2348a4d22..8597b6f713 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/graphql/GraphQlAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/graphql/GraphQlAutoConfiguration.java @@ -123,9 +123,9 @@ public class GraphQlAutoConfiguration { @Bean @ConditionalOnMissingBean - public GraphQlService graphQlService(GraphQlSource graphQlSource) { + public GraphQlService graphQlService(GraphQlSource graphQlSource, BatchLoaderRegistry batchLoaderRegistry) { ExecutionGraphQlService service = new ExecutionGraphQlService(graphQlSource); - service.addDataLoaderRegistrar(this.batchLoaderRegistry); + service.addDataLoaderRegistrar(batchLoaderRegistry); return service; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/graphql/GraphQlAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/graphql/GraphQlAutoConfigurationTests.java index e3c58bc478..14656cc3d6 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/graphql/GraphQlAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/graphql/GraphQlAutoConfigurationTests.java @@ -169,6 +169,14 @@ class GraphQlAutoConfigurationTests { }); } + @Test + void shouldConfigCustomBatchLoaderRegistry() { + this.contextRunner.withUserConfiguration(CustomBatchLoaderRegistryConfiguration.class).run((context) -> { + assertThat(context).getBeanNames(BatchLoaderRegistry.class).containsOnly("customBatchLoaderRegistry"); + assertThat(context).hasSingleBean(BatchLoaderRegistry.class); + }); + } + @Configuration(proxyBeanMethods = false) static class CustomGraphQlBuilderConfiguration { @@ -253,4 +261,14 @@ class GraphQlAutoConfigurationTests { } + @Configuration(proxyBeanMethods = false) + static class CustomBatchLoaderRegistryConfiguration { + + @Bean + BatchLoaderRegistry customBatchLoaderRegistry() { + return mock(BatchLoaderRegistry.class); + } + + } + } From 935d37b2ca4d031aeef7f62bd5bc24b652859b22 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Mon, 21 Feb 2022 18:16:07 +0100 Subject: [PATCH 2/2] Polish "Reuse BatchLoaderRegistry in GraphQlService" See gh-29929 --- .../graphql/GraphQlAutoConfiguration.java | 4 +-- .../GraphQlAutoConfigurationTests.java | 28 +++++++++---------- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/graphql/GraphQlAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/graphql/GraphQlAutoConfiguration.java index 8597b6f713..f12d28c8b0 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/graphql/GraphQlAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/graphql/GraphQlAutoConfiguration.java @@ -64,8 +64,6 @@ public class GraphQlAutoConfiguration { private static final Log logger = LogFactory.getLog(GraphQlAutoConfiguration.class); - private final BatchLoaderRegistry batchLoaderRegistry = new DefaultBatchLoaderRegistry(); - @Bean @ConditionalOnMissingBean public GraphQlSource graphQlSource(ResourcePatternResolver resourcePatternResolver, GraphQlProperties properties, @@ -118,7 +116,7 @@ public class GraphQlAutoConfiguration { @Bean @ConditionalOnMissingBean public BatchLoaderRegistry batchLoaderRegistry() { - return this.batchLoaderRegistry; + return new DefaultBatchLoaderRegistry(); } @Bean diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/graphql/GraphQlAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/graphql/GraphQlAutoConfigurationTests.java index 14656cc3d6..09350f17ef 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/graphql/GraphQlAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/graphql/GraphQlAutoConfigurationTests.java @@ -38,6 +38,7 @@ import org.springframework.graphql.GraphQlService; import org.springframework.graphql.data.method.annotation.support.AnnotatedControllerConfigurer; import org.springframework.graphql.execution.BatchLoaderRegistry; import org.springframework.graphql.execution.DataFetcherExceptionResolver; +import org.springframework.graphql.execution.DataLoaderRegistrar; import org.springframework.graphql.execution.GraphQlSource; import org.springframework.graphql.execution.MissingSchemaException; import org.springframework.graphql.execution.RuntimeWiringConfigurer; @@ -170,11 +171,18 @@ class GraphQlAutoConfigurationTests { } @Test - void shouldConfigCustomBatchLoaderRegistry() { - this.contextRunner.withUserConfiguration(CustomBatchLoaderRegistryConfiguration.class).run((context) -> { - assertThat(context).getBeanNames(BatchLoaderRegistry.class).containsOnly("customBatchLoaderRegistry"); - assertThat(context).hasSingleBean(BatchLoaderRegistry.class); - }); + void shouldConfigureCustomBatchLoaderRegistry() { + this.contextRunner + .withBean("customBatchLoaderRegistry", BatchLoaderRegistry.class, () -> mock(BatchLoaderRegistry.class)) + .run((context) -> { + assertThat(context).hasSingleBean(BatchLoaderRegistry.class); + assertThat(context.getBean("customBatchLoaderRegistry")) + .isSameAs(context.getBean(BatchLoaderRegistry.class)); + assertThat(context.getBean(GraphQlService.class)) + .extracting("dataLoaderRegistrars", + InstanceOfAssertFactories.list(DataLoaderRegistrar.class)) + .containsOnly(context.getBean(BatchLoaderRegistry.class)); + }); } @Configuration(proxyBeanMethods = false) @@ -261,14 +269,4 @@ class GraphQlAutoConfigurationTests { } - @Configuration(proxyBeanMethods = false) - static class CustomBatchLoaderRegistryConfiguration { - - @Bean - BatchLoaderRegistry customBatchLoaderRegistry() { - return mock(BatchLoaderRegistry.class); - } - - } - }