mirror of
https://github.com/lukaszraczylo/go-simple-graphql.git
synced 2026-10-12 00:45:31 +00:00
Add support for gzip content encoding.
Hasura supports gzip by default, so why shouldn't we use it, right?
This commit is contained in:
@@ -40,18 +40,28 @@ func Query(query string, variables interface{}, headers map[string]interface{})
|
||||
readyQuery := queryBuilder(query, variables)
|
||||
req := fasthttp.AcquireRequest()
|
||||
req.Header.SetContentType("application/json")
|
||||
req.Header.Set("Accept-Encoding", "gzip")
|
||||
for header, value := range headers {
|
||||
req.Header.Set(fmt.Sprintf("%v", header), fmt.Sprintf("%v", value))
|
||||
}
|
||||
req.Header.SetMethodBytes([]byte("POST"))
|
||||
req.SetBody(readyQuery)
|
||||
req.SetRequestURI(GraphQLUrl)
|
||||
|
||||
res := fasthttp.AcquireResponse()
|
||||
if err := fasthttp.Do(req, res); err != nil {
|
||||
panic("Unable to execute request")
|
||||
}
|
||||
fasthttp.ReleaseRequest(req)
|
||||
body := res.Body()
|
||||
|
||||
contentEncoding := res.Header.Peek("Content-Encoding")
|
||||
var body []byte
|
||||
if bytes.EqualFold(contentEncoding, []byte("gzip")) {
|
||||
body, _ = res.BodyGunzip()
|
||||
} else {
|
||||
body = res.Body()
|
||||
}
|
||||
|
||||
toReturn := gjson.Get(string(body), "data")
|
||||
if toReturn.String() == "" {
|
||||
fmt.Println("Probably not what you expect:", string(body))
|
||||
|
||||
Reference in New Issue
Block a user