Merge pull request #29929 from dugenkui03

* pr/29929:
  Polish "Reuse BatchLoaderRegistry in GraphQlService"
  Reuse BatchLoaderRegistry in GraphQlService

Closes gh-29929
pull/29976/head
Stephane Nicoll 3 years ago
commit 15da07ac9c

@ -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,14 +116,14 @@ public class GraphQlAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public BatchLoaderRegistry batchLoaderRegistry() {
return this.batchLoaderRegistry;
return new DefaultBatchLoaderRegistry();
}
@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;
}

@ -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;
@ -169,6 +170,21 @@ class GraphQlAutoConfigurationTests {
});
}
@Test
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)
static class CustomGraphQlBuilderConfiguration {

Loading…
Cancel
Save