2

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**
        ...
    }
}
dchhatani
  • 31
  • 5
  • is your application running in a container? spring, quarkus, wildfly? – Andreas Hauschild Jun 02 '20 at 08:08
  • using springboot @AndreasHauschild – dchhatani Jun 02 '20 at 08:22
  • i am not a spring guy but in general you can always inject something like a request context, which should contain the token information in the request header. Maybe this can help you: https://stackoverflow.com/questions/3320674/spring-how-do-i-inject-an-httpservletrequest-into-a-request-scoped-bean or this: https://stackoverflow.com/questions/47038349/how-to-extract-authentication-token-in-controller/47046477 – Andreas Hauschild Jun 02 '20 at 12:21
  • thank you for your suggestions and answer @AndreasHauschild. I am able to retrieve the request context now using GraphQLRootContext https://stackoverflow.com/a/62157667/3395237 – dchhatani Jun 02 '20 at 17:23

1 Answers1

1

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"); 
       ...
    }
}
dchhatani
  • 31
  • 5