@@ -34,6 +34,7 @@ import (
34
34
"bytes"
35
35
"context"
36
36
"encoding/json"
37
+ "fmt"
37
38
"io"
38
39
"mime/multipart"
39
40
"net/http"
@@ -45,12 +46,15 @@ import (
45
46
type Client struct {
46
47
endpoint string
47
48
httpClient * http.Client
49
+
50
+ Log func (s string )
48
51
}
49
52
50
53
// NewClient makes a new Client capable of making GraphQL requests.
51
54
func NewClient (endpoint string , opts ... ClientOption ) * Client {
52
55
c := & Client {
53
56
endpoint : endpoint ,
57
+ Log : func (string ) {},
54
58
}
55
59
for _ , optionFunc := range opts {
56
60
optionFunc (c )
@@ -61,6 +65,10 @@ func NewClient(endpoint string, opts ...ClientOption) *Client {
61
65
return c
62
66
}
63
67
68
+ func (c * Client ) logf (format string , args ... interface {}) {
69
+ c .Log (fmt .Sprintf (format , args ... ))
70
+ }
71
+
64
72
// Run executes the query and unmarshals the response from the data field
65
73
// into the response object.
66
74
// 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
98
106
if err := writer .Close (); err != nil {
99
107
return errors .Wrap (err , "close writer" )
100
108
}
101
-
109
+ c . logf ( ">> vars:%+v files:%d query:%s" , req . vars , len ( req . files ), req . q )
102
110
gr := & graphResponse {
103
111
Data : resp ,
104
112
}
@@ -119,6 +127,7 @@ func (c *Client) Run(ctx context.Context, req *Request, resp interface{}) error
119
127
if _ , err := io .Copy (& buf , res .Body ); err != nil {
120
128
return errors .Wrap (err , "reading body" )
121
129
}
130
+ c .logf ("<< %s" , buf .String ())
122
131
if err := json .NewDecoder (& buf ).Decode (& gr ); err != nil {
123
132
return errors .Wrap (err , "decoding response" )
124
133
}
0 commit comments