Skip to content

Commit

Permalink
Allow configuring custom GraphQL argument resolvers
Browse files Browse the repository at this point in the history
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 spring-projectsgh-40393
  • Loading branch information
maxhov authored and bclozel committed Jul 23, 2024
1 parent 3ef2bcf commit 3561ab8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import org.springframework.core.log.LogMessage;
import org.springframework.data.domain.ScrollPosition;
import org.springframework.graphql.ExecutionGraphQlService;
import org.springframework.graphql.data.method.HandlerMethodArgumentResolver;
import org.springframework.graphql.data.method.annotation.support.AnnotatedControllerConfigurer;
import org.springframework.graphql.data.pagination.ConnectionFieldTypeVisitor;
import org.springframework.graphql.data.pagination.CursorEncoder;
Expand Down Expand Up @@ -154,11 +155,13 @@ public ExecutionGraphQlService executionGraphQlService(GraphQlSource graphQlSour
@Bean
@ConditionalOnMissingBean
public AnnotatedControllerConfigurer annotatedControllerConfigurer(
@Qualifier(TaskExecutionAutoConfiguration.APPLICATION_TASK_EXECUTOR_BEAN_NAME) ObjectProvider<Executor> executorProvider) {
@Qualifier(TaskExecutionAutoConfiguration.APPLICATION_TASK_EXECUTOR_BEAN_NAME) ObjectProvider<Executor> executorProvider,
ObjectProvider<HandlerMethodArgumentResolver> argumentResolvers) {
AnnotatedControllerConfigurer controllerConfigurer = new AnnotatedControllerConfigurer();
controllerConfigurer
.addFormatterRegistrar((registry) -> ApplicationConversionService.addBeans(registry, this.beanFactory));
executorProvider.ifAvailable(controllerConfigurer::setExecutor);
argumentResolvers.orderedStream().forEach(controllerConfigurer::addCustomArgumentResolver);
return controllerConfigurer;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.graphql.ExecutionGraphQlService;
import org.springframework.graphql.data.method.HandlerMethodArgumentResolver;
import org.springframework.graphql.data.method.annotation.support.AnnotatedControllerConfigurer;
import org.springframework.graphql.data.pagination.EncodingCursorStrategy;
import org.springframework.graphql.execution.BatchLoaderRegistry;
Expand Down Expand Up @@ -241,6 +242,15 @@ void whenCustomExecutorIsDefinedThenAnnotatedControllerConfigurerDoesNotUseIt()
});
}

@Test
void whenAHandlerMethodArgumentResolverIsDefinedThenAnnotatedControllerConfigurerShouldUseIt() {
this.contextRunner.withUserConfiguration(CustomHandlerMethodArgumentResolverConfiguration.class)
.run((context) -> assertThat(context.getBean(AnnotatedControllerConfigurer.class))
.extracting("customArgumentResolvers")
.asInstanceOf(InstanceOfAssertFactories.LIST)
.hasSize(1));
}

@Configuration(proxyBeanMethods = false)
static class CustomGraphQlBuilderConfiguration {

Expand Down Expand Up @@ -336,4 +346,13 @@ Executor customExecutor() {

}

static class CustomHandlerMethodArgumentResolverConfiguration {

@Bean
HandlerMethodArgumentResolver customHandlerMethodArgumentResolver() {
return mock(HandlerMethodArgumentResolver.class);
}

}

}

0 comments on commit 3561ab8

Please sign in to comment.