如何在 GraphQL Java 中覆盖 DataFetcherExceptionHandler?
How to override DataFetcherExceptionHandler in GraphQL Java?
我正在使用以下 starter 进行 Graphql 集成
<dependency>
<groupId>com.graphql-java-kickstart</groupId>
<artifactId>graphql-spring-boot-starter</artifactId>
<version>6.0.1</version>
</dependency>
我不确定如何用我自己的 CustomExceptionHandler 覆盖 SimpleDataFetcherExceptionHandler。图书馆已经自动装配了很多东西。我需要为 graphQl 对象创建单独的配置吗?文档帮助不大。
我还尝试将@ControllerAdvice 集成到我的 Graphql java spring 启动应用程序中,但错误与其中的异常处理程序不匹配。它们由 GraphQl 错误处理程序处理。错误如何在 Graphql 内部传播?
如何改变这种行为?
我有这个,它对我有用。非常常规的 spring 用于异常处理的东西,但是没有 @ControllerAdvice
(顺便说一句,如果你不明白,这是 kotlin - 让我知道,但我认为即使没有 kotlin 知识也很清楚)。
import org.springframework.web.bind.annotation.ExceptionHandler
import com.oembedler.moon.graphql.boot.error.ThrowableGraphQLError
internal class GraphQLExceptionHandler {
@ExceptionHandler(AppException::class)
fun handleGenericException(ex: AppException): ThrowableGraphQLError {
return ThrowableGraphQLError(ex)
}
@ExceptionHandler(Exception::class)
fun handleGenericException(ex: Exception): ThrowableGraphQLError {
return ThrowableGraphQLError(TechnicalException())
}
}
@Configuration
internal open class GraphQLConfiguration {
@Bean
open fun graphQLExceptionHandler(): GraphQLExceptionHandler {
return GraphQLExceptionHandler()
}
}
我有很多依赖项,以防它们在这里很重要:
<graphql.version>5.4.1</graphql.version>
<graphql-datetime-spring-boot-starter.version>1.4.0</graphql-datetime-spring-boot-starter.version>
<dependency>
<groupId>com.graphql-java-kickstart</groupId>
<artifactId>graphql-spring-boot-starter</artifactId>
<version>${graphql.version}</version>
</dependency>
<dependency>
<groupId>com.graphql-java-kickstart</groupId>
<artifactId>graphiql-spring-boot-starter</artifactId>
<version>${graphql.version}</version>
</dependency>
<dependency>
<groupId>com.graphql-java-kickstart</groupId>
<artifactId>voyager-spring-boot-starter</artifactId>
<version>${graphql.version}</version>
</dependency>
<dependency>
<groupId>com.graphql-java-kickstart</groupId>
<artifactId>graphql-java-tools</artifactId>
<version>${graphql.version}</version>
</dependency>
<dependency>
<groupId>com.zhokhov.graphql</groupId>
<artifactId>graphql-datetime-spring-boot-starter</artifactId>
<version>${graphql-datetime-spring-boot-starter.version}</version>
</dependency>
我正在使用以下 starter 进行 Graphql 集成
<dependency>
<groupId>com.graphql-java-kickstart</groupId>
<artifactId>graphql-spring-boot-starter</artifactId>
<version>6.0.1</version>
</dependency>
我不确定如何用我自己的 CustomExceptionHandler 覆盖 SimpleDataFetcherExceptionHandler。图书馆已经自动装配了很多东西。我需要为 graphQl 对象创建单独的配置吗?文档帮助不大。
我还尝试将@ControllerAdvice 集成到我的 Graphql java spring 启动应用程序中,但错误与其中的异常处理程序不匹配。它们由 GraphQl 错误处理程序处理。错误如何在 Graphql 内部传播?
如何改变这种行为?
我有这个,它对我有用。非常常规的 spring 用于异常处理的东西,但是没有 @ControllerAdvice
(顺便说一句,如果你不明白,这是 kotlin - 让我知道,但我认为即使没有 kotlin 知识也很清楚)。
import org.springframework.web.bind.annotation.ExceptionHandler
import com.oembedler.moon.graphql.boot.error.ThrowableGraphQLError
internal class GraphQLExceptionHandler {
@ExceptionHandler(AppException::class)
fun handleGenericException(ex: AppException): ThrowableGraphQLError {
return ThrowableGraphQLError(ex)
}
@ExceptionHandler(Exception::class)
fun handleGenericException(ex: Exception): ThrowableGraphQLError {
return ThrowableGraphQLError(TechnicalException())
}
}
@Configuration
internal open class GraphQLConfiguration {
@Bean
open fun graphQLExceptionHandler(): GraphQLExceptionHandler {
return GraphQLExceptionHandler()
}
}
我有很多依赖项,以防它们在这里很重要:
<graphql.version>5.4.1</graphql.version>
<graphql-datetime-spring-boot-starter.version>1.4.0</graphql-datetime-spring-boot-starter.version>
<dependency>
<groupId>com.graphql-java-kickstart</groupId>
<artifactId>graphql-spring-boot-starter</artifactId>
<version>${graphql.version}</version>
</dependency>
<dependency>
<groupId>com.graphql-java-kickstart</groupId>
<artifactId>graphiql-spring-boot-starter</artifactId>
<version>${graphql.version}</version>
</dependency>
<dependency>
<groupId>com.graphql-java-kickstart</groupId>
<artifactId>voyager-spring-boot-starter</artifactId>
<version>${graphql.version}</version>
</dependency>
<dependency>
<groupId>com.graphql-java-kickstart</groupId>
<artifactId>graphql-java-tools</artifactId>
<version>${graphql.version}</version>
</dependency>
<dependency>
<groupId>com.zhokhov.graphql</groupId>
<artifactId>graphql-datetime-spring-boot-starter</artifactId>
<version>${graphql-datetime-spring-boot-starter.version}</version>
</dependency>