LoginSignup
0
0

More than 3 years have passed since last update.

Spring Boot で graphql-spring-boot 使用時にエラーをどこかへ通知したいとき

Posted at

@RestControllerAdvice とかで取れなかったのと、 @ExceptionHandler よりももう少し詳細な情報が欲しかったので。

環境


val spring_version=2.2.2.RELEASE

implementation("com.graphql-java-kickstart:graphql-spring-boot-starter:6.0.0")
implementation("org.springframework.boot:spring-boot-starter:$spring_version")
implementation("org.springframework.boot:spring-boot-starter-web:$spring_version")

コード例

Map<String, ExecutionStrategy> を Bean に登録すれば良さそう。

@Configuration
class GraphQLConfig {
    @Bean
    fun executionStrategies(): Map<String, ExecutionStrategy> {
        val customDataFetcherExceptionHandler = CustomDataFetcherExceptionHandler
        return mapOf(
                "queryExecutionStrategy" to AsyncExecutionStrategy(customDataFetcherExceptionHandler),
                "mutationExecutionStrategy" to AsyncSerialExecutionStrategy(customDataFetcherExceptionHandler)
        )
    }

    class CustomDataFetcherExceptionHandler: SimpleDataFetcherExceptionHandler() {
        override fun onException(handlerParameters: DataFetcherExceptionHandlerParameters): DataFetcherExceptionHandlerResult {
            // いい感じに通知する
            // もっと低レベルな情報がほしければ
            // handlerParameters.dataFetchingEnvironment.getContext<GraphQLServletContext>().httpServletRequest とかでとれそう。
            return super.onException(handlerParameters)
        }
    }
}

AsyncExecutionStrategyAsyncSerialExecutionStrategy でいいのかはまだよくわかってないけど、ひとまずこれでエラーを slack とかに飛ばすことができる。

もっといい方法があればコメントください。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0