Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read request headers from incoming message in a graphQL endpoint in spring boot application

I have a spring boot application running with a graphql endpoint that validates and executes queries and mutations, however, I need to read one header in the incoming message in order to pass its value to another endpoint. Is there a way in graphql to read these values? some sort of getHeaders or something like that?

like image 303
Juan Jose Villalobos Avatar asked Jan 25 '19 20:01

Juan Jose Villalobos


People also ask

Should GraphQL be placed before or after HTTP endpoint?

GraphQL should be placed after all authentication middleware, so that you have access to the same session and user information you would in your HTTP endpoint handlers. HTTP is commonly associated with REST, which uses "resources" as its core concept. In contrast, GraphQL's conceptual model is an entity graph.

How to read HTTP request header in Spring Boot REST application?

To read HTTP Request Header in Spring Boot REST application we use @RequestHeader annotation. For example, let’s assume we need to create a method in our Rest Controller class that accepts HTTP Get Request with two headers and returns these same headers back in a Response Body. To break it down in steps I will need to:

How do I send a GraphQL query using HTTP?

Your GraphQL HTTP server should handle the HTTP GET and POST methods. When receiving an HTTP GET request, the GraphQL query should be specified in the "query" query string.

How to get the httpservletrequest headers from the GraphQL context?

Consider you use graphql-spring-boot and graphql-java-tools , and assuming that you does not customize GraphQLContext , you can try to add DataFetchingEnvironment argument to your resolver function and then get the GraphQLContext from it. You can then get HttpServletRequest from the context and access the headers :


4 Answers

GraphQL itself does not define any things related to how to expose it over the network , so it does not define any things related to get HTTP header.It is up to developers to use their ways.So, it depends on the underlaying technologies you use to serve GraphQL over HTTP.

Consider you use graphql-spring-boot and graphql-java-tools , and assuming that you does not customize GraphQLContext , you can try to add DataFetchingEnvironment argument to your resolver function and then get the GraphQLContext from it. You can then get HttpServletRequest from the context and access the headers :

    public Foo resolveFoo(Map<String,String> input , DataFetchingEnvironment env){

        GraphQLContext context =  env.getContext();
        HttpServletRequest request = context.getHttpServletRequest().get();
        request.getHeader("content-type");
    }
like image 73
Ken Chan Avatar answered Nov 10 '22 08:11

Ken Chan


The solution by @Ken Chan was not working for me. GraphQLContext had no method named getHttpServletRequest.
Solved it by using GraphQLServletContext instead. You can change the code to:

public Foo resolveFoo(Map<String,String> input , DataFetchingEnvironment env){

    GraphQLServletContext context =  env.getContext();
    String header = context.getHttpServletRequest().getHeader("content-type");
}
like image 32
Mat G Avatar answered Nov 10 '22 10:11

Mat G


Apparently the type of the context is not standardized. I use SPQR, and in my case I discovered (via debug):

        DefaultGlobalContext<ServletWebRequest> context = handlerParameters.getDataFetchingEnvironment().getContext();
        context.getNativeRequest().getHeader("something");
like image 22
Cristian Avatar answered Nov 10 '22 09:11

Cristian


Not a direct answer to your problem statement, but one can use a Filter to handle it before the request hits the resolver endpoints (if that's a requirement):

public class HeaderFilter implements Filter {

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) {
    final HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
    
    String headerVal= httpServletRequest.getHeader("<header string>");
    
    try {
        filterChain.doFilter(httpServletRequest, servletResponse);
    } catch (IOException | ServletException e) {
        //handle as you wish
    }
}
like image 37
Soumav Avatar answered Nov 10 '22 09:11

Soumav