Skip to content

Commit 3561ab8

Browse files
maxhovbclozel
authored andcommitted
Allow configuring custom GraphQL argument resolvers
This commit gathers `HandlerMethodArgumentResolver` beans contributed by the application and sets them up on the auto-configured `AnnotatedControllerConfigurer` bean. This allows easier registrationsfor custom argument resolvers in Spring for GraphQL applications. Closes gh-40393
1 parent 3ef2bcf commit 3561ab8

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import org.springframework.core.log.LogMessage;
5050
import org.springframework.data.domain.ScrollPosition;
5151
import org.springframework.graphql.ExecutionGraphQlService;
52+
import org.springframework.graphql.data.method.HandlerMethodArgumentResolver;
5253
import org.springframework.graphql.data.method.annotation.support.AnnotatedControllerConfigurer;
5354
import org.springframework.graphql.data.pagination.ConnectionFieldTypeVisitor;
5455
import org.springframework.graphql.data.pagination.CursorEncoder;
@@ -154,11 +155,13 @@ public ExecutionGraphQlService executionGraphQlService(GraphQlSource graphQlSour
154155
@Bean
155156
@ConditionalOnMissingBean
156157
public AnnotatedControllerConfigurer annotatedControllerConfigurer(
157-
@Qualifier(TaskExecutionAutoConfiguration.APPLICATION_TASK_EXECUTOR_BEAN_NAME) ObjectProvider<Executor> executorProvider) {
158+
@Qualifier(TaskExecutionAutoConfiguration.APPLICATION_TASK_EXECUTOR_BEAN_NAME) ObjectProvider<Executor> executorProvider,
159+
ObjectProvider<HandlerMethodArgumentResolver> argumentResolvers) {
158160
AnnotatedControllerConfigurer controllerConfigurer = new AnnotatedControllerConfigurer();
159161
controllerConfigurer
160162
.addFormatterRegistrar((registry) -> ApplicationConversionService.addBeans(registry, this.beanFactory));
161163
executorProvider.ifAvailable(controllerConfigurer::setExecutor);
164+
argumentResolvers.orderedStream().forEach(controllerConfigurer::addCustomArgumentResolver);
162165
return controllerConfigurer;
163166
}
164167

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import org.springframework.core.io.ByteArrayResource;
4646
import org.springframework.core.io.ClassPathResource;
4747
import org.springframework.graphql.ExecutionGraphQlService;
48+
import org.springframework.graphql.data.method.HandlerMethodArgumentResolver;
4849
import org.springframework.graphql.data.method.annotation.support.AnnotatedControllerConfigurer;
4950
import org.springframework.graphql.data.pagination.EncodingCursorStrategy;
5051
import org.springframework.graphql.execution.BatchLoaderRegistry;
@@ -241,6 +242,15 @@ void whenCustomExecutorIsDefinedThenAnnotatedControllerConfigurerDoesNotUseIt()
241242
});
242243
}
243244

245+
@Test
246+
void whenAHandlerMethodArgumentResolverIsDefinedThenAnnotatedControllerConfigurerShouldUseIt() {
247+
this.contextRunner.withUserConfiguration(CustomHandlerMethodArgumentResolverConfiguration.class)
248+
.run((context) -> assertThat(context.getBean(AnnotatedControllerConfigurer.class))
249+
.extracting("customArgumentResolvers")
250+
.asInstanceOf(InstanceOfAssertFactories.LIST)
251+
.hasSize(1));
252+
}
253+
244254
@Configuration(proxyBeanMethods = false)
245255
static class CustomGraphQlBuilderConfiguration {
246256

@@ -336,4 +346,13 @@ Executor customExecutor() {
336346

337347
}
338348

349+
static class CustomHandlerMethodArgumentResolverConfiguration {
350+
351+
@Bean
352+
HandlerMethodArgumentResolver customHandlerMethodArgumentResolver() {
353+
return mock(HandlerMethodArgumentResolver.class);
354+
}
355+
356+
}
357+
339358
}

0 commit comments

Comments
 (0)