How can I intercept graphql queries?

371 views Asked by At

I am using spring-boot-graphql-starter in my project. How can I add this interceptor to the graphql queries? This is how I configure the interceptor. However the context value is not filled in the controller. I am using Webflux.

class RequestHeaderInterceptor : WebGraphQlInterceptor {

    private val ccc = "zzz"

    override fun intercept(request: WebGraphQlRequest, chain: WebGraphQlInterceptor.Chain): Mono<WebGraphQlResponse> {
        val value = request.headers.getFirst(ccc)
        request.configureExecutionInput { _, builder ->
            builder.graphQLContext(mapOf("token" to value)).build()
        }
        return chain.next(request)
    }
}
1

There are 1 answers

0
Elyorbek Ibrokhimov On

You need to declare it as a bean. From the WebGraphQlInterceptor documentation:

Interceptors are typically declared as beans in Spring configuration and ordered as defined in ObjectProvider.orderedStream().

GraphQlWebFluxAutoConfiguration class is responsible for registering all the interceptor beans.