Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

grErrs type as error #37

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
43 changes: 32 additions & 11 deletions graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/pkg/errors"
"io"
"mime/multipart"
"net/http"

"github.com/pkg/errors"
"strings"
)

// Client is a client for interacting with a GraphQL API.
type Client struct {
endpoint string
httpClient *http.Client
httpClient httpDoer
useMultipartForm bool

// closeReq will close the request body immediately allowing for reuse of client
Expand Down Expand Up @@ -143,9 +143,9 @@ func (c *Client) runWithJSON(ctx context.Context, req *Request, resp interface{}
}
return errors.Wrap(err, "decoding response")
}

if len(gr.Errors) > 0 {
// return first error
return gr.Errors[0]
return gr.Errors
}
return nil
}
Expand Down Expand Up @@ -214,17 +214,17 @@ func (c *Client) runWithPostFields(ctx context.Context, req *Request, resp inter
}
return errors.Wrap(err, "decoding response")
}

if len(gr.Errors) > 0 {
// return first error
return gr.Errors[0]
return gr.Errors
}
return nil
}

// WithHTTPClient specifies the underlying http.Client to use when
// making requests.
// NewClient(endpoint, WithHTTPClient(specificHTTPClient))
func WithHTTPClient(httpclient *http.Client) ClientOption {
func WithHTTPClient(httpclient httpDoer) ClientOption {
return func(client *Client) {
client.httpClient = httpclient
}
Expand All @@ -249,17 +249,34 @@ func ImmediatelyCloseReqBody() ClientOption {
// modify the behaviour of the Client.
type ClientOption func(*Client)

type graphErrs []graphErr

func (e graphErrs) Error() string {
if len(e) == 1 {
return fmt.Sprintf("1 error occurred:\n\t* %s\n\n", e[0])
}

points := make([]string, len(e))
for i, err := range e {
points[i] = fmt.Sprintf("* %s", err)
}

return fmt.Sprintf(
"%d errors occurred:\n\t%s\n\n",
len(e), strings.Join(points, "\n\t"))
}

type graphErr struct {
Message string
Message string `json:"message"`
}

func (e graphErr) Error() string {
return "graphql: " + e.Message
}

type graphResponse struct {
Data interface{}
Errors []graphErr
Data interface{} `json:"data"`
Errors graphErrs `json:"errors"`
}

// Request is a GraphQL request.
Expand Down Expand Up @@ -322,3 +339,7 @@ type File struct {
Name string
R io.Reader
}

type httpDoer interface {
Do(req *http.Request) (*http.Response, error)
}
4 changes: 3 additions & 1 deletion graphql_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -92,7 +93,8 @@ func TestDoJSONBadRequestErr(t *testing.T) {
var responseData map[string]interface{}
err := client.Run(ctx, &Request{q: "query {}"}, &responseData)
is.Equal(calls, 1) // calls
is.Equal(err.Error(), "graphql: miscellaneous message as to why the the request was bad")
is.Equal(len(err.(graphErrs)), 1)
is.True(strings.Contains(err.Error(), "graphql: miscellaneous message as to why the the request was bad"))
}

func TestQueryJSON(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions graphql_multipart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func TestDoErr(t *testing.T) {
var responseData map[string]interface{}
err := client.Run(ctx, &Request{q: "query {}"}, &responseData)
is.True(err != nil)
is.Equal(err.Error(), "graphql: Something went wrong")
is.True(strings.Contains(err.Error(), "graphql: Something went wrong"))
}

func TestDoServerErr(t *testing.T) {
Expand Down Expand Up @@ -164,7 +164,7 @@ func TestDoBadRequestErr(t *testing.T) {
defer cancel()
var responseData map[string]interface{}
err := client.Run(ctx, &Request{q: "query {}"}, &responseData)
is.Equal(err.Error(), "graphql: miscellaneous message as to why the the request was bad")
is.True(strings.Contains(err.Error(), "graphql: miscellaneous message as to why the the request was bad"))
}

func TestDoNoResponse(t *testing.T) {
Expand Down