forked from nautilus/graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queryer.go
executable file
·224 lines (184 loc) · 5.99 KB
/
queryer.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
package graphql
import (
"bytes"
"context"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"reflect"
"strconv"
"github.com/vektah/gqlparser/v2/ast"
)
// RemoteSchema encapsulates a particular schema that can be executed by sending network requests to the
// specified URL.
type RemoteSchema struct {
Schema *ast.Schema
URL string
}
// QueryInput provides all of the information required to fire a query
type QueryInput struct {
Query string `json:"query"`
QueryDocument *ast.QueryDocument `json:"-"`
OperationName string `json:"operationName"`
Variables map[string]interface{} `json:"variables"`
}
// String returns a guaranteed unique string that can be used to identify the input
func (i *QueryInput) String() string {
// let's just marshal the input
marshaled, err := json.Marshal(i)
if err != nil {
return ""
}
// return the result
return string(marshaled)
}
// Raw returns the "raw underlying value of the key" when used by dataloader
func (i *QueryInput) Raw() interface{} {
return i
}
// Queryer is a interface for objects that can perform
type Queryer interface {
Query(context.Context, *QueryInput, interface{}) error
}
// NetworkMiddleware are functions can be passed to SingleRequestQueryer.WithMiddleware to affect its internal
// behavior
type NetworkMiddleware func(*http.Request) error
// QueryerWithMiddlewares is an interface for queryers that support network middlewares
type QueryerWithMiddlewares interface {
WithMiddlewares(wares []NetworkMiddleware) Queryer
}
// HTTPQueryer is an interface for queryers that let you configure an underlying http.Client
type HTTPQueryer interface {
WithHTTPClient(client *http.Client) Queryer
}
// HTTPQueryerWithMiddlewares is an interface for queryers that let you configure an underlying http.Client
// and accept middlewares
type HTTPQueryerWithMiddlewares interface {
WithHTTPClient(client *http.Client) Queryer
WithMiddlewares(wares []NetworkMiddleware) Queryer
}
// Provided Implementations
// MockSuccessQueryer responds with pre-defined value when executing a query
type MockSuccessQueryer struct {
Value interface{}
}
// Query looks up the name of the query in the map of responses and returns the value
func (q *MockSuccessQueryer) Query(ctx context.Context, input *QueryInput, receiver interface{}) error {
// assume the mock is writing the same kind as the receiver
reflect.ValueOf(receiver).Elem().Set(reflect.ValueOf(q.Value))
// this will panic if something goes wrong
return nil
}
// QueryerFunc responds to the query by calling the provided function
type QueryerFunc func(*QueryInput) (interface{}, error)
// Query invokes the provided function and writes the response to the receiver
func (q QueryerFunc) Query(ctx context.Context, input *QueryInput, receiver interface{}) error {
// invoke the handler
response, err := q(input)
if err != nil {
return err
}
// assume the mock is writing the same kind as the receiver
reflect.ValueOf(receiver).Elem().Set(reflect.ValueOf(response))
// no errors
return nil
}
type NetworkQueryer struct {
URL string
Middlewares []NetworkMiddleware
Client *http.Client
}
// SendQuery is responsible for sending the provided payload to the desingated URL
func (q *NetworkQueryer) SendQuery(ctx context.Context, payload []byte) ([]byte, error) {
// construct the initial request we will send to the client
req, err := http.NewRequest("POST", q.URL, bytes.NewBuffer(payload))
if err != nil {
return nil, err
}
// add the current context to the request
acc := req.WithContext(ctx)
acc.Header.Set("Content-Type", "application/json")
return q.sendRequest(acc)
}
// SendMultipart is responsible for sending multipart request to the desingated URL
func (q *NetworkQueryer) SendMultipart(ctx context.Context, payload []byte, contentType string) ([]byte, error) {
// construct the initial request we will send to the client
req, err := http.NewRequest("POST", q.URL, bytes.NewBuffer(payload))
if err != nil {
return nil, err
}
// add the current context to the request
acc := req.WithContext(ctx)
acc.Header.Set("Content-Type", contentType)
return q.sendRequest(acc)
}
func (q *NetworkQueryer) sendRequest(acc *http.Request) ([]byte, error) {
// we could have any number of middlewares that we have to go through so
for _, mware := range q.Middlewares {
err := mware(acc)
if err != nil {
return nil, err
}
}
// fire the response to the queryer's url
if q.Client == nil {
q.Client = &http.Client{}
}
resp, err := q.Client.Do(acc)
if err != nil {
return nil, err
}
// read the full body
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
defer resp.Body.Close()
// check for HTTP errors
if resp.StatusCode < 200 || resp.StatusCode > 299 {
return body, errors.New("response was not successful with status code: " + strconv.Itoa(resp.StatusCode))
}
// we're done
return body, err
}
// ExtractErrors takes the result from a remote query and writes it to the provided pointer
func (q *NetworkQueryer) ExtractErrors(result map[string]interface{}) error {
// if there is an error
if _, ok := result["errors"]; ok {
// a list of errors from the response
errList := ErrorList{}
// build up a list of errors
errs, ok := result["errors"].([]interface{})
if !ok {
return errors.New("errors was not a list")
}
// a list of error messages
for _, err := range errs {
obj, ok := err.(map[string]interface{})
if !ok {
return errors.New("encountered non-object error")
}
message, ok := obj["message"].(string)
if !ok {
return errors.New("error message was not a string")
}
var extensions map[string]interface{}
if e, ok := obj["extensions"].(map[string]interface{}); ok {
extensions = e
}
var path []interface{}
if p, ok := obj["path"].([]interface{}); ok {
path = p
}
errList = append(errList, &Error{
Message: message,
Path: path,
Extensions: extensions,
})
}
return errList
}
// pass the result along
return nil
}