Skip to content

Commit

Permalink
support responses compressed with gzip (shurcooL#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbarrosop authored Feb 4, 2022
1 parent 01a575e commit dedae85
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package graphql

import (
"bytes"
"compress/gzip"
"context"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -200,15 +201,26 @@ func (c *Client) do(ctx context.Context, op operationType, v interface{}, variab
return err
}
defer resp.Body.Close()

r := resp.Body
if resp.Header.Get("Content-Encoding") == "gzip" {
r, err = gzip.NewReader(r)
if err != nil {
return fmt.Errorf("problem trying to create gzip reader: %w", err)
}
defer r.Close()
}

if resp.StatusCode != http.StatusOK {
body, _ := ioutil.ReadAll(resp.Body)
body, _ := ioutil.ReadAll(r)
return fmt.Errorf("non-200 OK status code: %v body: %q", resp.Status, body)
}
var out struct {
Data *json.RawMessage
Errors Errors
}
err = json.NewDecoder(resp.Body).Decode(&out)

err = json.NewDecoder(r).Decode(&out)
if err != nil {
// TODO: Consider including response body in returned error, if deemed helpful.
return err
Expand Down

0 comments on commit dedae85

Please sign in to comment.