-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
client.go
148 lines (124 loc) · 3.85 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
// client is used internally for testing. See readme for alternatives
package client
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"github.com/mitchellh/mapstructure"
)
type (
// Client used for testing GraphQL 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 {
Query string `json:"query"`
Variables map[string]interface{} `json:"variables,omitempty"`
OperationName string `json:"operationName,omitempty"`
HTTP *http.Request `json:"-"`
}
// Response is a GraphQL layer response from a handler.
Response struct {
Data interface{}
Errors json.RawMessage
Extensions map[string]interface{}
}
)
// 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) 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(query string, response interface{}, options ...Option) error {
respDataRaw, err := p.RawPost(query, options...)
if err != nil {
return err
}
// we want to unpack even if there is an error, so we can see partial responses
unpackErr := unpack(respDataRaw.Data, response)
if respDataRaw.Errors != nil {
return RawJsonError{respDataRaw.Errors}
}
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) RawPost(query string, options ...Option) (*Response, error) {
r, err := p.newRequest(query, options...)
if err != nil {
return nil, fmt.Errorf("build: %w", err)
}
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: %w", err)
}
return respDataRaw, nil
}
func (p *Client) newRequest(query string, options ...Option) (*http.Request, error) {
bd := &Request{
Query: query,
HTTP: httptest.NewRequest(http.MethodPost, "/", 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)
}
switch bd.HTTP.Header.Get("Content-Type") {
case "application/json":
requestBody, err := json.Marshal(bd)
if err != nil {
return nil, fmt.Errorf("encode: %w", err)
}
bd.HTTP.Body = ioutil.NopCloser(bytes.NewBuffer(requestBody))
default:
panic("unsupported encoding" + bd.HTTP.Header.Get("Content-Type"))
}
return bd.HTTP, nil
}
func unpack(data interface{}, into interface{}) error {
d, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
Result: into,
TagName: "json",
ErrorUnused: true,
ZeroFields: true,
})
if err != nil {
return fmt.Errorf("mapstructure: %w", err)
}
return d.Decode(data)
}