Skip to content

Commit 94a5736

Browse files
committed
added logging capability
1 parent d413340 commit 94a5736

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

graphql.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"bytes"
3535
"context"
3636
"encoding/json"
37+
"fmt"
3738
"io"
3839
"mime/multipart"
3940
"net/http"
@@ -45,12 +46,15 @@ import (
4546
type Client struct {
4647
endpoint string
4748
httpClient *http.Client
49+
50+
Log func(s string)
4851
}
4952

5053
// NewClient makes a new Client capable of making GraphQL requests.
5154
func NewClient(endpoint string, opts ...ClientOption) *Client {
5255
c := &Client{
5356
endpoint: endpoint,
57+
Log: func(string) {},
5458
}
5559
for _, optionFunc := range opts {
5660
optionFunc(c)
@@ -61,6 +65,10 @@ func NewClient(endpoint string, opts ...ClientOption) *Client {
6165
return c
6266
}
6367

68+
func (c *Client) logf(format string, args ...interface{}) {
69+
c.Log(fmt.Sprintf(format, args...))
70+
}
71+
6472
// Run executes the query and unmarshals the response from the data field
6573
// into the response object.
6674
// Pass in a nil response object to skip response parsing.
@@ -98,6 +106,7 @@ func (c *Client) Run(ctx context.Context, req *Request, resp interface{}) error
98106
if err := writer.Close(); err != nil {
99107
return errors.Wrap(err, "close writer")
100108
}
109+
c.logf(">> vars:%+v files:%d query:%q", req.vars, len(req.files), req.q)
101110
var graphResponse = struct {
102111
Data interface{}
103112
Errors []graphErr
@@ -120,6 +129,7 @@ func (c *Client) Run(ctx context.Context, req *Request, resp interface{}) error
120129
if _, err := io.Copy(&buf, res.Body); err != nil {
121130
return errors.Wrap(err, "reading body")
122131
}
132+
c.logf("<< %s", buf.String())
123133
if err := json.NewDecoder(&buf).Decode(&graphResponse); err != nil {
124134
return errors.Wrap(err, "decoding response")
125135
}

0 commit comments

Comments
 (0)