1

我需要从我的 graphql 查询解析器中的请求标头中读取数据。

使用 graphql-spqr 库。

方法样本。

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**
        ...
    }
}
4

1 回答 1

1

能够使用@GraphQLRootContext 作为我的方法参数之一来解决

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"); 
       ...
    }
}
于 2020-06-02T17:20:10.910 回答