From a209262d192018f437f3aa38b34998a86468b377 Mon Sep 17 00:00:00 2001 From: Lukasz Raczylo Date: Sat, 27 Mar 2021 07:50:18 +0000 Subject: [PATCH] Add support for gzip content encoding. Hasura supports gzip by default, so why shouldn't we use it, right? --- gql.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/gql.go b/gql.go index 63efc3a..51f020f 100644 --- a/gql.go +++ b/gql.go @@ -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))