WebGraphQlInterceptor is not invoked from Azure Spring Cloud Function App

Prakash Vasant Holkar 0 Reputation points
2024-10-02T17:15:09.94+00:00

I am using an Azure Spring Cloud Function app with WebFlux and GraphQL. I understand that request headers are not passed directly to the GraphQLContext. Instead, I need to intercept the request, extract the request headers, and set them into the GraphQLContext, so that the headers will be accessible in the next layer (e.g., DataFetcher).

When using the interceptor with a plain Spring Boot application (instead of a function app), it works as expected, and the control enters the interceptor class.

However, when I use the same interceptor in the Azure Function app, it is not being invoked.

Here is my interceptor code snippet:

package com.cb.catalog.intercepter;

import org.springframework.graphql.server.WebGraphQlInterceptor;
import org.springframework.graphql.server.WebGraphQlRequest;
import org.springframework.graphql.server.WebGraphQlResponse;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

import java.util.Collections;

@Component
class RequestHeaderInterceptor implements WebGraphQlInterceptor {

    @Override
    public Mono<WebGraphQlResponse> intercept(WebGraphQlRequest request, Chain chain) {
        String value = request.getHeaders().getFirst("Authorization");
        request.configureExecutionInput((executionInput, builder) ->
                builder.graphQLContext(Collections.singletonMap("Authorization", value)).build());
        return chain.next(request);
    }
}

Could you please advise why the WebGraphQlInterceptor is not being invoked in the Azure spring could Function app and suggest a solution to ensure it works in this environment?

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,978 questions
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
12,004 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Sina Salam 10,491 Reputation points
    2024-10-02T19:21:49.8033333+00:00

    Hello Prakash Vasant Holkar,

    Welcome to the Microsoft Q&A and thank you for posting your questions here.

    The reason your WebGraphQlInterceptor is not being invoked in the Azure spring Cloud Function App is because of many things, but majorly the cloud and traditional Spring Boot application architecture, Azure Functions Routing, Interceptor Registration, and Triggers.

    I will advise you to start from your code: In your interceptor, try using Mono.defer to ensure that the execution input is configured only when the request is being processed. For example:

    @Override
    public Mono<WebGraphQlResponse> intercept(WebGraphQlRequest request, Chain chain) {
        String value = request.getHeaders().getFirst("Authorization");
        
        return Mono.defer(() -> {
            request.configureExecutionInput((executionInput, builder) ->
                builder.graphQLContext(Collections.singletonMap("Authorization", value)).build());
            return chain.next(request);
        });
    }
    

    Secondly, if your interceptor is in a package not scanned by Spring, it won’t be registered. You can explicitly specify the package to scan in your @SpringBootApplication class using @ComponentScan.

    Thirdly, set up a Spring GraphQL function in Azure.

    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    @RestController
    public class GraphQLController {
        @GetMapping("/graphql")
        public Mono<WebGraphQlResponse> handleGraphQL(WebGraphQlRequest request) {
            // Logic to handle the request
        }
    }
    

    Then, check your pom.xml or build.gradle to verify that the necessary Spring GraphQL libraries are included.

    I hope this is helpful! Do not hesitate to let me know if you have any other questions.


    Please don't forget to close up the thread here by upvoting and accept it as an answer if it is helpful.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.