Skip to content

Commit 15da07a

Browse files
committed
Merge pull request #29929 from dugenkui03
* pr/29929: Polish "Reuse BatchLoaderRegistry in GraphQlService" Reuse BatchLoaderRegistry in GraphQlService Closes gh-29929
2 parents 8ee9681 + 935d37b commit 15da07a

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ public class GraphQlAutoConfiguration {
6464

6565
private static final Log logger = LogFactory.getLog(GraphQlAutoConfiguration.class);
6666

67-
private final BatchLoaderRegistry batchLoaderRegistry = new DefaultBatchLoaderRegistry();
68-
6967
@Bean
7068
@ConditionalOnMissingBean
7169
public GraphQlSource graphQlSource(ResourcePatternResolver resourcePatternResolver, GraphQlProperties properties,
@@ -118,14 +116,14 @@ private List<Resource> resolveSchemaResources(ResourcePatternResolver resolver,
118116
@Bean
119117
@ConditionalOnMissingBean
120118
public BatchLoaderRegistry batchLoaderRegistry() {
121-
return this.batchLoaderRegistry;
119+
return new DefaultBatchLoaderRegistry();
122120
}
123121

124122
@Bean
125123
@ConditionalOnMissingBean
126-
public GraphQlService graphQlService(GraphQlSource graphQlSource) {
124+
public GraphQlService graphQlService(GraphQlSource graphQlSource, BatchLoaderRegistry batchLoaderRegistry) {
127125
ExecutionGraphQlService service = new ExecutionGraphQlService(graphQlSource);
128-
service.addDataLoaderRegistrar(this.batchLoaderRegistry);
126+
service.addDataLoaderRegistrar(batchLoaderRegistry);
129127
return service;
130128
}
131129

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/graphql/GraphQlAutoConfigurationTests.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.springframework.graphql.data.method.annotation.support.AnnotatedControllerConfigurer;
3939
import org.springframework.graphql.execution.BatchLoaderRegistry;
4040
import org.springframework.graphql.execution.DataFetcherExceptionResolver;
41+
import org.springframework.graphql.execution.DataLoaderRegistrar;
4142
import org.springframework.graphql.execution.GraphQlSource;
4243
import org.springframework.graphql.execution.MissingSchemaException;
4344
import org.springframework.graphql.execution.RuntimeWiringConfigurer;
@@ -169,6 +170,21 @@ void shouldDisableFieldIntrospection() {
169170
});
170171
}
171172

173+
@Test
174+
void shouldConfigureCustomBatchLoaderRegistry() {
175+
this.contextRunner
176+
.withBean("customBatchLoaderRegistry", BatchLoaderRegistry.class, () -> mock(BatchLoaderRegistry.class))
177+
.run((context) -> {
178+
assertThat(context).hasSingleBean(BatchLoaderRegistry.class);
179+
assertThat(context.getBean("customBatchLoaderRegistry"))
180+
.isSameAs(context.getBean(BatchLoaderRegistry.class));
181+
assertThat(context.getBean(GraphQlService.class))
182+
.extracting("dataLoaderRegistrars",
183+
InstanceOfAssertFactories.list(DataLoaderRegistrar.class))
184+
.containsOnly(context.getBean(BatchLoaderRegistry.class));
185+
});
186+
}
187+
172188
@Configuration(proxyBeanMethods = false)
173189
static class CustomGraphQlBuilderConfiguration {
174190

0 commit comments

Comments
 (0)