2

I want to fetch a header value I am passing during a GraphQl Query call. Something like we can do prior in case of rest api HttpServletRequest.getheader()

I wanted to fetch it from the dataFetchingEnvironment but the context fetched from this value did not get me any means to fetch the header values from request.

        try {
            GraphQLContext context =  env.getGraphQlContext();
            String Id= context.getHeader("headerkeyIpass"); 
// I know this method does not exist i am trying to paint a picture as to what i am asking

I do not intend to change the resolver method calls but any inputs to improve my code would be great.

2 Answers 2

3

Like you said getHttpServletRequest does not exist on DataFetchingEnvironment.getGraphQlContext() when using the new official Spring 2.7 spring-boot-starter-graphql (not the legacy GraphQL Java Kickstart ones).

Instead you can add an autowired HttpServletRequest request variable at the top of your controller and inside each query resolver it will be filled with the request context.

@Controller
public class SomeGraphQLController {
    ...

    @Autowired
    private HttpServletRequest request; // will be filled inside each @QueryResolver and @MutationResolver method.

    ...

    @QueryResolver
    public XXX yyyy() {
        ...
        try {
            String someHeader = request.getHeader("someHeader"); 
        ...
    }
}
Sign up to request clarification or add additional context in comments.

Comments

-1

The GraphQLContext has method getHttpServletRequest() which returns

java.util.Optional<javax.servlet.http.HttpServletRequest> 

So from there u can get the headers

GraphQLContext context =  env.getGraphQlContext();
HttpServletRequest request = context.getHttpServletRequest().get();
String Id= request.getHeader("headerkeyIpass"); 

If you are using the latest graphql libraries there are some breaking changed and you can get the headers from GraphQLServletContext as shown here

GraphQLServletContext graphQLServletContext = (GraphQLServletContext) env.getContext();
    
String user = graphQLServletContext.getHttpServletRequest().getHeader("user");

3 Comments

I did use the below code to get the header value ``` GraphQLServletContext context = env.getContext(); HttpServletRequest request=context.getHttpServletRequest(); String Id= request.getHeader("headerkeyIpass"); ``` But the DataFetcherEnvironement.getContext is deprecated The above code HttpServletRequest request = context.getHttpServletRequest().get(); doesn't work since the getHttpServletRequest does not exist for the GraphQLContext but GraphQLServletContext does have that. Now can we get the GraphQLServletContext from the DataFetching env?
This does not work. context.getHttpServletRequest() doesn't exist on graphQlContext.
the breaking changes are that env.getContext() is deprecated. I am actually trying to get rid of it, but I didn't find how.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.