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

define a customized error type to carry status code and body #76

Open
wants to merge 3 commits into
base: main
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
2 changes: 1 addition & 1 deletion doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
// That package is driving the feature development.
//
// For now, see README for more details.
package graphql // import "github.com/shurcooL/graphql"
package graphql // import "github.com/leslie-qiwa/graphql"
2 changes: 1 addition & 1 deletion example/graphqldev/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
graphqlserver "github.com/graph-gophers/graphql-go"
"github.com/graph-gophers/graphql-go/example/starwars"
"github.com/graph-gophers/graphql-go/relay"
"github.com/shurcooL/graphql"
"github.com/leslie-qiwa/graphql"
)

func main() {
Expand Down
20 changes: 16 additions & 4 deletions graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"

"github.com/shurcooL/graphql/internal/jsonutil"
"github.com/leslie-qiwa/graphql/internal/jsonutil"
"golang.org/x/net/context/ctxhttp"
)

// Client is a GraphQL client.
type Client struct {
url string // GraphQL server URL.
httpClient *http.Client
logger io.Writer
}

// NewClient creates a GraphQL client targeting the specified GraphQL server URL.
Expand All @@ -30,6 +31,11 @@ func NewClient(url string, httpClient *http.Client) *Client {
}
}

// SetLogger set verbose logger
func (c *Client) SetLogger(l io.Writer) {
c.logger = l
}

// Query executes a single GraphQL query request,
// with a query derived from q, populating the response into it.
// q should be a pointer to struct that corresponds to the GraphQL schema.
Expand Down Expand Up @@ -65,14 +71,17 @@ func (c *Client) do(ctx context.Context, op operationType, v interface{}, variab
if err != nil {
return err
}
if c.logger != nil {
c.logger.Write(append([]byte("request:\n\t"), buf.Bytes()...))
}
resp, err := ctxhttp.Post(ctx, c.httpClient, c.url, "application/json", &buf)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := ioutil.ReadAll(resp.Body)
return fmt.Errorf("non-200 OK status code: %v body: %q", resp.Status, body)
body, err := ioutil.ReadAll(resp.Body)
return &GithubError{Status: resp.Status, StatusCode: resp.StatusCode, Body: body, Err: err}
}
var out struct {
Data *json.RawMessage
Expand All @@ -85,6 +94,9 @@ func (c *Client) do(ctx context.Context, op operationType, v interface{}, variab
return err
}
if out.Data != nil {
if c.logger != nil {
c.logger.Write(append([]byte("reply:\n\t"), (*out.Data)...))
}
err := jsonutil.UnmarshalGraphQL(*out.Data, v)
if err != nil {
// TODO: Consider including response body in returned error, if deemed helpful.
Expand Down
2 changes: 1 addition & 1 deletion graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"net/http/httptest"
"testing"

"github.com/shurcooL/graphql"
"github.com/leslie-qiwa/graphql"
)

func TestClient_Query_partialDataWithErrorResponse(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion ident/ident_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"reflect"
"testing"

"github.com/shurcooL/graphql/ident"
"github.com/leslie-qiwa/graphql/ident"
)

func Example_lowerCamelCaseToMixedCaps() {
Expand Down
4 changes: 2 additions & 2 deletions internal/jsonutil/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"testing"
"time"

"github.com/shurcooL/graphql"
"github.com/shurcooL/graphql/internal/jsonutil"
"github.com/leslie-qiwa/graphql"
"github.com/leslie-qiwa/graphql/internal/jsonutil"
)

func TestUnmarshalGraphQL_benchmark(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions internal/jsonutil/graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"testing"
"time"

"github.com/shurcooL/graphql"
"github.com/shurcooL/graphql/internal/jsonutil"
"github.com/leslie-qiwa/graphql"
"github.com/leslie-qiwa/graphql/internal/jsonutil"
)

func TestUnmarshalGraphQL(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion query.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"reflect"
"sort"

"github.com/shurcooL/graphql/ident"
"github.com/leslie-qiwa/graphql/ident"
)

func constructQuery(v interface{}, variables map[string]interface{}) string {
Expand Down
17 changes: 17 additions & 0 deletions scalar.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package graphql

import (
"fmt"
)

// Note: These custom types are meant to be used in queries for now.
// But the plan is to switch to using native Go types (string, int, bool, time.Time, etc.).
// See https://github.com/shurcooL/githubv4/issues/9 for details.
Expand Down Expand Up @@ -49,3 +53,16 @@ func NewInt(v Int) *Int { return &v }

// NewString is a helper to make a new *String.
func NewString(v String) *String { return &v }

// GithubError is error to contain http status code and body
type GithubError struct {
Err error
Status string
StatusCode int
Body []byte
}

// Error is method inherited from error interface
func (m *GithubError) Error() string {
return fmt.Sprintf("non-200 OK status code: %v body: %q", m.Status, m.Body)
}
2 changes: 1 addition & 1 deletion scalar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package graphql_test
import (
"testing"

"github.com/shurcooL/graphql"
"github.com/leslie-qiwa/graphql"
)

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