我需要从 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**
...
}
}
能够使用 @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");
...
}
}