Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"

"github.com/shurcooL/go/ctxhttp"
Expand Down Expand Up @@ -69,27 +70,36 @@ func (c *Client) do(ctx context.Context, op operationType, v interface{}, variab
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected status: %v", resp.Status)
}

var out struct {
Data *json.RawMessage
Errors errors
//Extensions interface{} // Unused.
}
err = json.NewDecoder(resp.Body).Decode(&out)
if err != nil {
return err
respBodyCopy, errRead := ioutil.ReadAll(resp.Body)
if errRead != nil {
return fmt.Errorf("error decoding output: <an additional error ocurred while reading output: %s>", errRead)
}
return fmt.Errorf("error decoding output: %v (output was: %s)", err, respBodyCopy)
}
if out.Data != nil {
err := jsonutil.UnmarshalGraphQL(*out.Data, v)
if err != nil {
return err
return fmt.Errorf("error unmarshalling data: %s", err)
}
}
if len(out.Errors) > 0 {
return out.Errors
}
if resp.StatusCode != http.StatusOK {
respBodyCopy, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("unexpected status: %v <an additional error ocurred while reading output: %s>", resp.Status, err)
}
return fmt.Errorf("unexpected status: %v %s", resp.Status, respBodyCopy)
}
return nil
}

Expand Down