我在 bramble 联合 graphql 网关服务器上设置了一个自定义 http 标头,但它没有推送到我的联合服务,或者我做错了。如果有人遇到同样的问题,请赐教,谢谢。我将gin用于我的 Web 框架。
这是我到目前为止所做的,
我在通过网关服务器调用 api 时设置此标头
{
"X-Custom-Header":"SomeValue"
}
并从请求中得到
func graphHandler() gin.HandlerFunc {
return func(c *gin.Context) {
v := c.Request.Header.Get("X-Custom-Header")
log.Println("X-Custom-Header value", v)
config := graph.Config{Resolvers: &graph.Resolver{}}
config.Directives.Boundary = func(ctx context.Context, obj interface{}, next graphql.Resolver) (res interface{}, err error) {
return next(ctx)
}
h := handler.NewDefaultServer(graph.NewExecutableSchema(config))
h.ServeHTTP(c.Writer, c.Request)
}
}
func main() {
godotenv.Load()
port := os.Getenv("PORT")
if port == "" {
port = defaultPort
}
r := gin.Default()
r.POST("/query", graphHandler())
r.GET("/", playgroundHandler())
r.Run(":" + port)
}
网关配置(config.json)
{
"services": [
"http://app1:8080/query",
"http://user:8080/query"
],
"gateway-port": 8082,
"private-port": 8083,
"metrics-port": 8084,
"log-level": "debug",
"poll-interval": "5s",
"max-requests-per-query": 50,
"max-client-response-size": 1048576,
"id-field-name": "id",
"plugins":[
{
"name": "cors",
"config": {
"allowed-origins": ["*"],
"allowed-headers": ["x-custom-header"],
"allow-credentials": true,
"max-age": 3600,
"debug": true
}
},
{
"name": "admin-ui"
},
{
"name": "playground"
}
]
}