Skip to content

Commit 5be86e0

Browse files
authored
Merge pull request #6 from machinebox/logging
Simple logging: added logging capability
2 parents 8ad1e3f + a2c9213 commit 5be86e0

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

graphql.go

Lines changed: 10 additions & 1 deletion
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,7 +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
}
101-
109+
c.logf(">> vars:%+v files:%d query:%s", req.vars, len(req.files), req.q)
102110
gr := &graphResponse{
103111
Data: resp,
104112
}
@@ -119,6 +127,7 @@ func (c *Client) Run(ctx context.Context, req *Request, resp interface{}) error
119127
if _, err := io.Copy(&buf, res.Body); err != nil {
120128
return errors.Wrap(err, "reading body")
121129
}
130+
c.logf("<< %s", buf.String())
122131
if err := json.NewDecoder(&buf).Decode(&gr); err != nil {
123132
return errors.Wrap(err, "decoding response")
124133
}

0 commit comments

Comments
 (0)