graphql-java-generator / graphql-maven-plugin-project

graphql-maven-plugin is a Maven Plugin for GraphQL, based on graphql-java. It accelerates the development for both the client and the server, by generating the Java code. It allows a quicker development when in contract-first approach, by avoiding to code the boilerplate code.
https://graphql-maven-plugin-project.graphql-java-generator.com
MIT License
118 stars 47 forks source link

Add access token from camel context to the Default Webclient #211

Closed Prabha1389 closed 3 months ago

Prabha1389 commented 5 months ago

I'm facing a problem where I could not inject access token retrieved in my context to the default WebClient bean of the GraphQL Client.

My application is a wrapper service which exposes a REST Service and it communicates to the backend GraphQL Server. At the server side the service is protected with OKTA OAUTH2.0, hence each request which I make should have a valid bearer token. At this moment the out of the box configuration provides all facility to generate a OAUTH token and call the backend server.

But, the trick here is in order to generate a OAUTH token successfully, I should retrieve the credentials (basically my REST service is exposed with Basic auth) and then add them to my token request to backend.

For instance, i would like to dynamically replace the value of "accessToken" here - https://github.com/Latsode/spring-graphql-shopify-client-sample/blob/master/src/main/java/com/client/shopify/graphql/util/ShopifyHeaderExchangeFilter.java

I tried creating my own Exchange filter but with no luck. Also, tried overwriting the GraphQlClient on MutationExecutor class etc..

My service is a spring-boot application built with Apache Camel. I use the service for Mutation with the Client code generated by by the plugin.

` @Bean(name = "webClient") public WebClient webClient(String graphqlEndpoint) { try { ExchangeStrategies exchangeStrategies = ExchangeStrategies.builder() .codecs(configurer -> configurer.defaultCodecs().maxInMemorySize(-1)).build();

       String xsrfToken=java.util.UUID.randomUUID().toString();

        return WebClient
                .builder()
                .filter(addTokenFilter())
                .baseUrl(graphqlEndpoint)
                .defaultHeader(HttpHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON_VALUE)
                .defaultHeader("ECM-CS-XSRF-Token",xsrfToken)
                .defaultCookie("ECM-CS-XSRF-Token",xsrfToken)
                .defaultUriVariables(Collections.singletonMap("url", graphqlEndpoint))
                .exchangeStrategies(exchangeStrategies)
                .build();

    } catch (Exception e) {
        throw new ExceptionInInitializerError(e);
    }
}

public ExchangeFilterFunction addTokenFilter() {
    return (request, next) -> {

        LOGGER.info("Creating renewTokenFilter");
        if (request.url().getPath().endsWith("/graphql")) {
            String token = request.body().toString();
            ClientRequest filtered = ClientRequest.from(request)
                    .header(HttpHeaders.AUTHORIZATION, accessToken) // I need to replace the token dynamically here. 
                    .build();
            return next.exchange(filtered);
        } else {
            return next.exchange(request);
        }
    };
}`
etienne-sf commented 5 months ago

Hello,

This could fail for lots of reasons. So it's difficult to give a valid answer here.

And this is more a spring boot question than a question about this plugin.

Did you check pages like this one: https://www.baeldung.com/spring-boot-add-filter ?

Étienne