Is there a way to read request headers in @GraphqQuery method

Viewed 471

I need to read data from request header in my graphql query resolver.

Using graphql-spqr library .

method sample.

import io.leangen.graphql.annotations.*;
import io.leangen.graphql.spqr.spring.annotation.GraphQLApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@GraphQLApi
@Component
public class ProjectQueryResolver{
    @GraphQLQuery
    public Project projects(@GraphqlArugment String projectId) {
        **// want to read authorization token present in request header**
        ...
    }
}
1 Answers

Able to resolve using @GraphQLRootContext as one of my method params

import io.leangen.graphql.annotations.*;
import io.leangen.graphql.spqr.spring.annotation.GraphQLApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@GraphQLApi
@Component
public class ProjectQueryResolver{
    @GraphQLQuery
    public Project projects(@GraphqlArugment String projectId, 
                            @GraphQLRootContext DefaultGlobalContext context) {

       HttpServletRequest servletRequest = context.getServletRequest();
       String paramValue = servletRequest.getHeader("param"); 
       ...
    }
}
Related