因此,我想通过Go与ShurCool或Hasura Go客户端(GO包)从GraphQL服务器查询数据,但数据服务器需要类似‘x-hasura-admin-ret’的密钥和值包含在请求头中.
在两个包的文档中都没有提到如何做到这一点(设置头部键和值),它只提到了如何设置访问令牌.
因此,我想通过Go与ShurCool或Hasura Go客户端(GO包)从GraphQL服务器查询数据,但数据服务器需要类似‘x-hasura-admin-ret’的密钥和值包含在请求头中.
在两个包的文档中都没有提到如何做到这一点(设置头部键和值),它只提到了如何设置访问令牌.
https://github.com/hasura/go-graphql-client提供的客户端有一个WithRequestModifier
方法.您可以添加一个请求头,如下所示:
import (
"net/http"
graphql "github.com/hasura/go-graphql-client"
)
func gqlInit() {
client := graphql.NewClient("your graphql url here", nil)
client = client.WithRequestModifier(func(r *http.Request) {
r.Header.Set("x-hasura-admin-secret", "secret")
})
}
看看https://github.com/shurcooL/graphql和相关的github lib,他们似乎希望你传递一个*http.Client
,为你添加标题,你可以这样做:
import (
"net/http"
graphql "github.com/shurcooL/graphql"
)
type hasuraAuthTransport struct {
secret string
}
func (h hasuraAuthTransport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
req.Header.Set("x-hasura-admin-secret", h.secret)
return http.DefaultTransport.RoundTrip(req)
}
func gqlInit() {
client := graphql.NewClient("your graphql url here", &http.Client{
Transport: hasuraAuthTransport{secret: "secret"},
})
}