-
Notifications
You must be signed in to change notification settings - Fork 22
/
queryerNetwork.go
98 lines (80 loc) · 2.43 KB
/
queryerNetwork.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
package graphql
import (
"context"
"encoding/json"
"net/http"
"github.com/mitchellh/mapstructure"
)
// SingleRequestQueryer sends the query to a url and returns the response
type SingleRequestQueryer struct {
// internals for bundling queries
queryer *NetworkQueryer
}
// NewSingleRequestQueryer returns a SingleRequestQueryer pointed to the given url
func NewSingleRequestQueryer(url string) *SingleRequestQueryer {
return &SingleRequestQueryer{
queryer: &NetworkQueryer{URL: url},
}
}
// WithMiddlewares returns a network queryer that will apply the provided middlewares
func (q *SingleRequestQueryer) WithMiddlewares(mwares []NetworkMiddleware) Queryer {
// for now just change the internal reference
q.queryer.Middlewares = mwares
// return it
return q
}
// WithHTTPClient lets the user configure the underlying http client being used
func (q *SingleRequestQueryer) WithHTTPClient(client *http.Client) Queryer {
q.queryer.Client = client
return q
}
func (q *SingleRequestQueryer) URL() string {
return q.queryer.URL
}
// Query sends the query to the designated url and returns the response.
func (q *SingleRequestQueryer) Query(ctx context.Context, input *QueryInput, receiver interface{}) error {
// check if query contains attached files
uploadMap := extractFiles(input)
// the payload
payload, err := json.Marshal(map[string]interface{}{
"query": input.Query,
"variables": input.Variables,
"operationName": input.OperationName,
})
if err != nil {
return err
}
var response []byte
if uploadMap.NotEmpty() {
body, contentType, err := prepareMultipart(payload, uploadMap)
responseBody, err := q.queryer.SendMultipart(ctx, body, contentType)
if err != nil {
return err
}
response = responseBody
} else {
// send that query to the api and write the appropriate response to the receiver
responseBody, err := q.queryer.SendQuery(ctx, payload)
if err != nil {
return err
}
response = responseBody
}
result := map[string]interface{}{}
if err = json.Unmarshal(response, &result); err != nil {
return err
}
// assign the result under the data key to the receiver
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
TagName: "json",
Result: receiver,
})
if err != nil {
return err
}
if err = decoder.Decode(result["data"]); err != nil {
return err
}
// finally extract errors, if any, and return them
return q.queryer.ExtractErrors(result) // TODO add unit tests!
}