From d0ad66e8f5fa81c6e84b9abc831ee42ab530c12e Mon Sep 17 00:00:00 2001 From: Leslie Qi Wang Date: Thu, 7 Oct 2021 16:20:40 -0700 Subject: [PATCH 1/3] define a customized error type to carry status code and body as a user, I want to know status code directly instead of parsing error text, just incase error text got changed one day Signed-off-by: Leslie Qi Wang --- graphql.go | 5 ++--- scalar.go | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/graphql.go b/graphql.go index 8520956..ec79eb5 100644 --- a/graphql.go +++ b/graphql.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "encoding/json" - "fmt" "io/ioutil" "net/http" @@ -71,8 +70,8 @@ func (c *Client) do(ctx context.Context, op operationType, v interface{}, variab } 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 diff --git a/scalar.go b/scalar.go index 0f7ceea..32ebf70 100644 --- a/scalar.go +++ b/scalar.go @@ -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. @@ -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) +} From c2c251a2b0fb618c8cf28fa992ad9361b8a114a5 Mon Sep 17 00:00:00 2001 From: Leslie Qi Wang Date: Fri, 29 Oct 2021 15:00:40 -0700 Subject: [PATCH 2/3] set logger to intercept raw request and reply data Signed-off-by: Leslie Qi Wang --- graphql.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/graphql.go b/graphql.go index ec79eb5..9bcbea5 100644 --- a/graphql.go +++ b/graphql.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "encoding/json" + "io" "io/ioutil" "net/http" @@ -15,6 +16,7 @@ import ( 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. @@ -29,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. @@ -64,6 +71,9 @@ 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 @@ -84,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. From f12b317a6da7dfe1b56a4f939958463c7c111e17 Mon Sep 17 00:00:00 2001 From: Leslie Qi Wang Date: Mon, 1 Nov 2021 12:36:24 -0700 Subject: [PATCH 3/3] replace with fork repo package Signed-off-by: Leslie Qi Wang --- doc.go | 2 +- example/graphqldev/main.go | 2 +- graphql.go | 2 +- graphql_test.go | 2 +- ident/ident_test.go | 2 +- internal/jsonutil/benchmark_test.go | 4 ++-- internal/jsonutil/graphql_test.go | 4 ++-- query.go | 2 +- scalar_test.go | 2 +- 9 files changed, 11 insertions(+), 11 deletions(-) diff --git a/doc.go b/doc.go index 870e3d4..652e230 100644 --- a/doc.go +++ b/doc.go @@ -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" diff --git a/example/graphqldev/main.go b/example/graphqldev/main.go index ffc8302..a3c71d1 100644 --- a/example/graphqldev/main.go +++ b/example/graphqldev/main.go @@ -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() { diff --git a/graphql.go b/graphql.go index 9bcbea5..17da484 100644 --- a/graphql.go +++ b/graphql.go @@ -8,7 +8,7 @@ import ( "io/ioutil" "net/http" - "github.com/shurcooL/graphql/internal/jsonutil" + "github.com/leslie-qiwa/graphql/internal/jsonutil" "golang.org/x/net/context/ctxhttp" ) diff --git a/graphql_test.go b/graphql_test.go index e09dcc9..c26881c 100644 --- a/graphql_test.go +++ b/graphql_test.go @@ -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) { diff --git a/ident/ident_test.go b/ident/ident_test.go index 9ee1b47..4857043 100644 --- a/ident/ident_test.go +++ b/ident/ident_test.go @@ -5,7 +5,7 @@ import ( "reflect" "testing" - "github.com/shurcooL/graphql/ident" + "github.com/leslie-qiwa/graphql/ident" ) func Example_lowerCamelCaseToMixedCaps() { diff --git a/internal/jsonutil/benchmark_test.go b/internal/jsonutil/benchmark_test.go index f8788b1..89beddc 100644 --- a/internal/jsonutil/benchmark_test.go +++ b/internal/jsonutil/benchmark_test.go @@ -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) { diff --git a/internal/jsonutil/graphql_test.go b/internal/jsonutil/graphql_test.go index 6329ed8..0e5e0ce 100644 --- a/internal/jsonutil/graphql_test.go +++ b/internal/jsonutil/graphql_test.go @@ -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) { diff --git a/query.go b/query.go index e10b771..0d13289 100644 --- a/query.go +++ b/query.go @@ -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 { diff --git a/scalar_test.go b/scalar_test.go index 8334b96..d25c5f4 100644 --- a/scalar_test.go +++ b/scalar_test.go @@ -3,7 +3,7 @@ package graphql_test import ( "testing" - "github.com/shurcooL/graphql" + "github.com/leslie-qiwa/graphql" ) func TestNewScalars(t *testing.T) {