From eda299df90a350a3d6f15615ca3713ed92319c44 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 14 Mar 2023 12:10:34 +0000 Subject: [PATCH] Configure exception resolver for `@GraphQlExceptionHandler` methods To apply `@ControllerAdvice` exception handling to non-controller DataFetcher implementations like QueryDslDataFetcher, QueryByExampleDataFetcher, and others, this commit exposes the DataFetcherExceptionResolver from AnnotatedControllerConfigurer as a bean. The existing auto-configured for DataFetcherExceptionResolver then picks this up and passes it into the builder used to create the GraphQlSource. Closes gh-34526 --- .../autoconfigure/graphql/GraphQlAutoConfiguration.java | 6 ++++++ .../graphql/GraphQlAutoConfigurationTests.java | 6 ++++-- 2 files changed, 10 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 e38602eda7..8d28e1093e 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 @@ -148,6 +148,12 @@ public class GraphQlAutoConfiguration { return controllerConfigurer; } + @Bean + DataFetcherExceptionResolver annotatedControllerConfigurerDataFetcherExceptionResolver( + AnnotatedControllerConfigurer annotatedControllerConfigurer) { + return annotatedControllerConfigurer.getExceptionResolver(); + } + static class GraphQlResourcesRuntimeHints implements RuntimeHintsRegistrar { @Override 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 908147d0bf..409193ba09 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 @@ -116,8 +116,10 @@ class GraphQlAutoConfigurationTests { GraphQlSource graphQlSource = context.getBean(GraphQlSource.class); GraphQL graphQL = graphQlSource.graphQl(); assertThat(graphQL.getQueryStrategy()).extracting("dataFetcherExceptionHandler") - .satisfies((exceptionHandler) -> assertThat(exceptionHandler.getClass().getName()) - .endsWith("ExceptionResolversExceptionHandler")); + .satisfies((exceptionHandler) -> { + assertThat(exceptionHandler.getClass().getName()).endsWith("ExceptionResolversExceptionHandler"); + assertThat(exceptionHandler).extracting("resolvers").asList().hasSize(2); + }); }); }