GraphQL SPQR- how to access the native ServletRequest and Response

GraphQL SPQR- how to access  the native ServletRequest and Response

GraphQL SPQR- how to access the native ServletRequest and Response

GraphQL SPQR is a powerful library to build graphql services using Springboot.

Code snippet to access native ServletRequest object and populate header based on a condition-

//GraphQL SPQR resolver
@Component
@GraphQLApi
public class SampleResolver{
    @GraphQLQuery
    public String helloworld(@GraphQLEnvironment ResolutionEnvironment env, 
@GraphQLArgument(name = "name") String name){

//If name is empty then set no data content HTTP Header   
if(name.equals("")){
    //GraphQL Global context
        DefaultGlobalContext dgc = env.dataFetchEnvironment().getContext();
       ServletWebRequest request = (ServletWebRequest) dgc.getNativeRequest();
      
       request.getResponse().setHeader(HttpStatus.NO_CONTENT.value()+"",HttpStatus.NO_CONTENT.getReasonPhrase());
        return ""; 
    }        
     return String.format("Hello: %s",name);    
        
    }
}

References

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.