How to read request headers from incoming message in a graphQL endpoint in spring boot application

Juan Jose Villalobos :

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?

Ken Chan :

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=76050&siteId=1