简体   繁体   中英

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?

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");
    }

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");
}

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");

Google brought me this answer and unfortunately it was for Spring framework. Many use Spring framework but if you use Apache Tomcat with GraphQL integration like I do, you can do use this.

        graphql.kickstart.servlet.context.DefaultGraphQLServletContext.DefaultGraphQLServletContext context =  dataFetchingEnvironment.getContext();
        jakarta.servlet.http.HttpServletRequest request = context.getHttpServletRequest();
        String tokenBearer = request.getHeader("Authorization");

hth

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
    }
}

These answers are too old, for latest versions. you can try to autowire one HttpServletRequest in your controller , such as

@Slf4j
@Controller
public class YourQueryController {
@Autowired
private HttpServletRequest request;
....

Then implement following logic to get header:

request.getHeader("Authorization")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM