GraphQL-Java sending HTTP headers to REST endpoints

2.7k views Asked by At

I need a way to send http headers for authentication and authorization to my rest endpoints from my grapqh-java impl. I'm doing authentication and authorization at GraphQL service layer and it is successful. Now I need to pass the same headers to all my rest endpoints. Is there a way I could do this. Grapqhl - Spring Boot Rest endpoints - Dropwizard

1

There are 1 answers

0
kaqqao On BEST ANSWER

Maybe just attach the needed user-specific data to the GraphQL context when executing the query, e.g:

graphQL.execute(queryString, context);

Where context could be any object, containing tokens, cookies, session data, HttpServletRequest etc

Then, use it to send the correct headers from your DataFetchers (a.k.a. resolvers):

public Object get(DataFetchingEnvironment environment) {
    Map<String, Object> context = environment.getContext();
    //get tokens, session data or whatever you need from the context to create the headers
    List headers = ...;
    return callRestWithHeaders("/rest/example", headers);
}

Preferably, do this header preparation logic in one place, via composition, inheritance or maybe in AOP style.