-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
235 lines (200 loc) · 6.43 KB
/
client.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
package client
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"github.com/mitchellh/mapstructure"
)
type (
// Client used for testing GraphREST servers. Not for production use.
Client struct {
h http.Handler
opts []Option
}
// Option implements a visitor that mutates an outgoing GraphQL request
//
// This is the Option pattern - https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis
Option func(bd *Request)
// Request represents an outgoing GraphQL request
Request struct {
HTTP *http.Request
}
// Response is a GraphQL layer response from a handler.
Response struct {
Code int
Message string
Data interface{}
//Errors json.RawMessage
//Extensions map[string]interface{}
}
)
const OK = 0
// New creates a graphql client
// Options can be set that should be applied to all requests made with this client
func New(h http.Handler, opts ...Option) *Client {
p := &Client{
h: h,
opts: opts,
}
return p
}
// MustPost is a convenience wrapper around Post that automatically panics on error
func (p *Client) MustGet(target string, response interface{}, options ...Option) {
if err := p.Post(target, response, options...); err != nil {
panic(err)
}
}
// Post sends a http POST request to the graphql endpoint with the given query then unpacks
// the response into the given object.
func (p *Client) Get(target string, response interface{}, options ...Option) error {
respDataRaw, err := p.RawRequest(http.MethodGet, target, options...)
if err != nil {
return err
}
if response == nil {
return nil
}
// we want to unpack even if there is an error, so we can see partial responses
unpackErr := unpack(respDataRaw, response)
// if respDataRaw.Errors != nil {
// return RawJsonError{respDataRaw.Errors}
// }
if respDataRaw.Code != OK {
return fmt.Errorf("code %d: %s", respDataRaw.Code, respDataRaw.Message)
}
return unpackErr
}
// MustPost is a convenience wrapper around Post that automatically panics on error
func (p *Client) MustPost(query string, response interface{}, options ...Option) {
if err := p.Post(query, response, options...); err != nil {
panic(err)
}
}
// Post sends a http POST request to the graphql endpoint with the given query then unpacks
// the response into the given object.
func (p *Client) Post(target string, response interface{}, options ...Option) error {
respDataRaw, err := p.RawRequest(http.MethodPost, target, options...)
if err != nil {
return err
}
if response == nil {
return nil
}
// we want to unpack even if there is an error, so we can see partial responses
unpackErr := unpack(respDataRaw, response)
// if respDataRaw.Errors != nil {
// return RawJsonError{respDataRaw.Errors}
// }
if respDataRaw.Code != OK {
return fmt.Errorf("code %d: %s", respDataRaw.Code, respDataRaw.Message)
}
return unpackErr
}
// MustPost is a convenience wrapper around Post that automatically panics on error
func (p *Client) MustPut(target string, response interface{}, options ...Option) {
if err := p.Post(target, response, options...); err != nil {
panic(err)
}
}
// Post sends a http POST request to the graphql endpoint with the given query then unpacks
// the response into the given object.
func (p *Client) Put(target string, response interface{}, options ...Option) error {
respDataRaw, err := p.RawRequest(http.MethodPut, target, options...)
if err != nil {
return err
}
if response == nil {
return nil
}
// we want to unpack even if there is an error, so we can see partial responses
unpackErr := unpack(respDataRaw, response)
// if respDataRaw.Errors != nil {
// return RawJsonError{respDataRaw.Errors}
// }
if respDataRaw.Code != OK {
return fmt.Errorf("code %d: %s", respDataRaw.Code, respDataRaw.Message)
}
return unpackErr
}
// MustPost is a convenience wrapper around Post that automatically panics on error
func (p *Client) MustDelete(target string, response interface{}, options ...Option) {
if err := p.Post(target, response, options...); err != nil {
panic(err)
}
}
// Post sends a http POST request to the graphql endpoint with the given query then unpacks
// the response into the given object.
func (p *Client) Delete(target string, response interface{}, options ...Option) error {
respDataRaw, err := p.RawRequest(http.MethodDelete, target, options...)
if err != nil {
return err
}
if response == nil {
return nil
}
// we want to unpack even if there is an error, so we can see partial responses
unpackErr := unpack(respDataRaw, response)
// if respDataRaw.Errors != nil {
// return RawJsonError{respDataRaw.Errors}
// }
if respDataRaw.Code != 0 {
return fmt.Errorf("code %d: %s", respDataRaw.Code, respDataRaw.Message)
}
return unpackErr
}
// RawPost is similar to Post, except it skips decoding the raw json response
// unpacked onto Response. This is used to test extension keys which are not
// available when using Post.
func (p *Client) RawRequest(method string, target string, options ...Option) (*Response, error) {
r, err := p.newRequest(method, target, options...)
if err != nil {
return nil, fmt.Errorf("build: %s", err.Error())
}
w := httptest.NewRecorder()
p.h.ServeHTTP(w, r)
if w.Code >= http.StatusBadRequest {
return nil, fmt.Errorf("http %d: %s", w.Code, w.Body.String())
}
// decode it into map string first, let mapstructure do the final decode
// because it can be much stricter about unknown fields.
respDataRaw := &Response{}
err = json.Unmarshal(w.Body.Bytes(), &respDataRaw)
if err != nil {
return nil, fmt.Errorf("decode: %s", err.Error())
}
return respDataRaw, nil
}
func (p *Client) newRequest(method string, target string, options ...Option) (*http.Request, error) {
bd := &Request{
HTTP: httptest.NewRequest(method, target, nil),
}
bd.HTTP.Header.Set("Content-Type", "application/json")
// per client options from client.New apply first
for _, option := range p.opts {
option(bd)
}
// per request options
for _, option := range options {
option(bd)
}
return bd.HTTP, nil
}
func unpack(data interface{}, into interface{}) error {
var metadata mapstructure.Metadata
d, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
Result: into,
TagName: "json",
ErrorUnused: true,
ZeroFields: true,
Metadata: &metadata,
})
if err != nil {
return fmt.Errorf("mapstructure: %s", err.Error())
}
err = d.Decode(data)
if err != nil {
return fmt.Errorf("mapstructure %s. data='%s', meta='%s'", err.Error(), data, metadata)
}
return err
}