首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在DataFetcherExceptionHandler中重写GraphQL?

如何在DataFetcherExceptionHandler中重写GraphQL?
EN

Stack Overflow用户
提问于 2020-07-04 11:53:51
回答 1查看 2.1K关注 0票数 1

我正在使用以下启动程序进行Graphql集成

代码语言:javascript
复制
<dependency>
    <groupId>com.graphql-java-kickstart</groupId>
    <artifactId>graphql-spring-boot-starter</artifactId>
    <version>6.0.1</version>
</dependency>

  1. 我不知道如何用自己的CustomExceptionHandler覆盖SimpleDataFetcherExceptionHandler。图书馆已经收藏了很多东西。是否需要为graphQl对象创建单独的配置?这些文档没有多大帮助。

  1. ,我也尝试将@ControllerAdvice集成到我的Graphql引导应用程序中,但是错误并不由它中的异常处理程序匹配。它们由GraphQl错误处理程序处理。错误如何在Graphql中传播?

我怎样才能改变这种行为?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-04 12:33:09

我有这个,而且它对我有用。对于异常处理,有很多常规的spring内容,但是没有@ControllerAdvice (如果您不能理解的话,这是kotlin -请告诉我,但我认为即使不知道kotlin也很清楚)。

代码语言:javascript
复制
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()
    }
}

我有相当多的依赖项,以防它们在这里起作用:

代码语言:javascript
复制
<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>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62728773

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档