This repository has been archived by the owner on Jan 8, 2024. It is now read-only.
forked from machinebox/graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphql.go
317 lines (291 loc) · 8.61 KB
/
graphql.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// Package graphql provides a low level GraphQL client.
//
// // create a client (safe to share across requests)
// client := graphql.NewClient("https://machinebox.io/graphql")
//
// // make a request
// req := graphql.NewRequest(`
// query ($key: String!) {
// items (id:$key) {
// field1
// field2
// field3
// }
// }
// `)
//
// // set any variables
// req.Var("key", "value")
//
// // run it and capture the response
// var respData ResponseStruct
// if err := client.Run(ctx, req, &respData); err != nil {
// log.Fatal(err)
// }
//
// Specify client
//
// To specify your own http.Client, use the WithHTTPClient option:
// httpclient := &http.Client{}
// client := graphql.NewClient("https://machinebox.io/graphql", graphql.WithHTTPClient(httpclient))
package graphql
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"mime/multipart"
"net/http"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
)
type (
// Client is a client for interacting with a GraphQL API.
Client struct {
endpoint string
httpClient CustomHttpClient
useMultipartForm bool
// closeReq will close the request body immediately allowing for reuse of client
closeReq bool
// Log is called with various debug information.
// To log to standard out, use:
// client.Log = func(s string) { log.Println(s) }
Log func(s string)
}
// CustomHttpClient allows a custom http.Client to be used other than the default one provided by golang.
CustomHttpClient interface {
Do(*http.Request) (*http.Response, error)
}
// ClientOption are functions that are passed into NewClient to
// modify the behaviour of the Client.
ClientOption func(*Client)
graphResponse struct {
Data interface{} `json:"data"`
Errors []GraphErr `json:"errors"`
}
graphValidationMessage struct {
Code string `json:"code"`
Message *string `json:"message"`
}
graphMutationPayload struct {
Messages []*graphValidationMessage `json:"messages"`
Result interface{} `json:"result"`
Successful bool `json:"successful"`
}
)
// NewClient makes a new Client capable of making GraphQL requests.
// In case no option for http.Client is provided the default one is used in place.
func NewClient(endpoint string, opts ...ClientOption) *Client {
c := &Client{
endpoint: endpoint,
Log: func(string) {},
}
for _, optionFunc := range opts {
optionFunc(c)
}
if c.httpClient == nil {
c.httpClient = http.DefaultClient
}
return c
}
// WithHTTPClient specifies the underlying http.Client to use when
// making requests.
// NewClient(endpoint, WithHTTPClient(specificHTTPClient))
func WithHTTPClient(httpclient CustomHttpClient) ClientOption {
return func(client *Client) {
client.httpClient = httpclient
}
}
// UseMultipartForm uses multipart/form-data and activates support for
// files.
func UseMultipartForm() ClientOption {
return func(client *Client) {
client.useMultipartForm = true
}
}
//ImmediatelyCloseReqBody will close the req body immediately after each request body is ready
func ImmediatelyCloseReqBody() ClientOption {
return func(client *Client) {
client.closeReq = true
}
}
func (c *Client) logf(format string, args ...interface{}) {
c.Log(fmt.Sprintf(format, args...))
}
// Run executes the query and unmarshals the response from the data field
// into the response object.
// Pass in a nil response object to skip response parsing.
// If the request fails or the server returns an error, the first error
// will be returned.
func (c *Client) Run(ctx context.Context, op Operation, resp interface{}) Error {
select {
case <-ctx.Done():
return NewExecutionError(ctx.Err())
default:
}
if len(op.Files()) > 0 && !c.useMultipartForm {
return NewExecutionError(errors.New("cannot send files with PostFields option"))
}
if c.useMultipartForm {
return c.runWithPostFields(ctx, op, resp)
}
return c.runWithJSON(ctx, op, resp)
}
func (c *Client) runWithJSON(ctx context.Context, op Operation, resp interface{}) Error {
req := op.Request()
var requestBody bytes.Buffer
requestBodyObj := struct {
Query string `json:"query"`
Variables map[string]interface{} `json:"variables"`
}{
Query: req.q,
Variables: req.vars,
}
if err := json.NewEncoder(&requestBody).Encode(requestBodyObj); err != nil {
return NewExecutionError(errors.Wrap(err, "encode body"))
}
r, err := http.NewRequest(http.MethodPost, c.endpoint, &requestBody)
if err != nil {
return NewExecutionError(err)
}
r.Close = c.closeReq
r.Header.Set("Content-Type", "application/json; charset=utf-8")
r.Header.Set("Accept", "application/json; charset=utf-8")
for key, values := range req.Header {
for _, value := range values {
r.Header.Add(key, value)
}
}
c.logf(">> headers: %v", r.Header)
r = r.WithContext(ctx)
res, err := c.httpClient.Do(r)
if err != nil {
return NewExecutionError(err)
}
if res.StatusCode != http.StatusOK {
return NewRequestError(res)
}
defer res.Body.Close()
var buf bytes.Buffer
if _, err := io.Copy(&buf, res.Body); err != nil {
return NewExecutionError(errors.Wrap(err, "reading body"))
}
c.logf("<< %s", buf.String())
var gr *graphResponse
switch op.(type) {
case *Mutation:
var results struct {
Data map[string]graphMutationPayload
}
if err := json.NewDecoder(&buf).Decode(&results); err != nil {
return NewExecutionError(errors.Wrap(err, "decoding response"))
}
gr = &graphResponse{}
for _, result := range results.Data {
if !result.Successful {
messages := result.Messages
errors := make([]GraphErr, len(messages))
for i, message := range messages {
errors[i] = GraphErr{
Message: emptyOrString(message.Message),
Code: message.Code,
}
}
gr.Errors = append(gr.Errors, errors...)
} else {
err := mapstructure.Decode(results.Data, &resp)
if err != nil {
return NewExecutionError(errors.Wrap(err, "decoding response"))
}
}
// The code above only supports payloads with a single mutation
break
}
default:
gr = &graphResponse{Data: resp}
if err := json.NewDecoder(&buf).Decode(&gr); err != nil {
return NewExecutionError(errors.Wrap(err, "decoding response"))
}
}
if len(gr.Errors) > 0 {
return NewGraphQLError(gr.Errors, res)
}
return nil
}
func (c *Client) runWithPostFields(ctx context.Context, op Operation, resp interface{}) Error {
req := op.Request()
var requestBody bytes.Buffer
writer := multipart.NewWriter(&requestBody)
if err := writer.WriteField("query", req.q); err != nil {
return NewExecutionError(errors.Wrap(err, "write query field"))
}
var variablesBuf bytes.Buffer
if len(req.vars) > 0 {
variablesField, err := writer.CreateFormField("variables")
if err != nil {
return NewExecutionError(errors.Wrap(err, "create variables field"))
}
if err := json.NewEncoder(io.MultiWriter(variablesField, &variablesBuf)).Encode(req.vars); err != nil {
return NewExecutionError(errors.Wrap(err, "encode variables"))
}
}
for i := range req.files {
part, err := writer.CreateFormFile(req.files[i].Field, req.files[i].Name)
if err != nil {
return NewExecutionError(errors.Wrap(err, "create form file"))
}
if _, err := io.Copy(part, req.files[i].R); err != nil {
return NewExecutionError(errors.Wrap(err, "preparing file"))
}
}
if err := writer.Close(); err != nil {
return NewExecutionError(errors.Wrap(err, "close writer"))
}
c.logf(">> variables: %s", variablesBuf.String())
c.logf(">> files: %d", len(req.files))
c.logf(">> query: %s", req.q)
gr := &graphResponse{
Data: resp,
}
r, err := http.NewRequest(http.MethodPost, c.endpoint, &requestBody)
if err != nil {
return NewExecutionError(err)
}
r.Close = c.closeReq
r.Header.Set("Content-Type", writer.FormDataContentType())
r.Header.Set("Accept", "application/json; charset=utf-8")
for key, values := range req.Header {
for _, value := range values {
r.Header.Add(key, value)
}
}
c.logf(">> headers: %v", r.Header)
r = r.WithContext(ctx)
res, err := c.httpClient.Do(r)
if err != nil {
return NewExecutionError(err)
}
if res.StatusCode != http.StatusOK {
return NewRequestError(res)
}
defer res.Body.Close()
var buf bytes.Buffer
if _, err := io.Copy(&buf, res.Body); err != nil {
return NewExecutionError(errors.Wrap(err, "reading body"))
}
c.logf("<< %s", buf.String())
if err := json.NewDecoder(&buf).Decode(&gr); err != nil {
return NewExecutionError(errors.Wrap(err, "decoding response"))
}
if len(gr.Errors) > 0 {
return NewGraphQLError(gr.Errors, res)
}
return nil
}
func emptyOrString(pointer *string) string {
if pointer == nil {
return ""
}
return *pointer
}