forked from Yamashou/gqlgenc
/
client.go
183 lines (151 loc) · 5.09 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
package client
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/Yamashou/gqlgenc/graphqljson"
"github.com/vektah/gqlparser/v2/gqlerror"
"golang.org/x/xerrors"
)
// HTTPRequestOption represents the options applicable to the http client
type HTTPRequestOption func(req *http.Request)
// Client is the http client wrapper
type Client struct {
Client *http.Client
BaseURL string
HTTPRequestOptions []HTTPRequestOption
}
// Request represents an outgoing GraphQL request
type Request struct {
Query string `json:"query"`
Variables map[string]interface{} `json:"variables,omitempty"`
OperationName string `json:"operationName,omitempty"`
}
// NewClient creates a new http client wrapper
func NewClient(client *http.Client, baseURL string, options ...HTTPRequestOption) *Client {
return &Client{
Client: client,
BaseURL: baseURL,
HTTPRequestOptions: options,
}
}
func (c *Client) newRequest(ctx context.Context, operationName, query string, vars map[string]interface{}, httpRequestOptions []HTTPRequestOption) (*http.Request, error) {
r := &Request{
Query: query,
Variables: vars,
OperationName: operationName,
}
requestBody, err := json.Marshal(r)
if err != nil {
return nil, xerrors.Errorf("encode: %w", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.BaseURL, bytes.NewBuffer(requestBody))
if err != nil {
return nil, xerrors.Errorf("create request struct failed: %w", err)
}
for _, httpRequestOption := range c.HTTPRequestOptions {
httpRequestOption(req)
}
for _, httpRequestOption := range httpRequestOptions {
httpRequestOption(req)
}
return req, nil
}
// GqlErrorList is the struct of a standard graphql error response
type GqlErrorList struct {
Errors gqlerror.List `json:"errors"`
}
func (e *GqlErrorList) Error() string {
return e.Errors.Error()
}
// HTTPError is the error when a GqlErrorList cannot be parsed
type HTTPError struct {
Code int `json:"code"`
Message string `json:"message"`
}
// ErrorResponse represent an handled error
type ErrorResponse struct {
// populated when http status code is not OK
NetworkError *HTTPError `json:"networkErrors"`
// populated when http status code is OK but the server returned at least one graphql error
GqlErrors *gqlerror.List `json:"graphqlErrors"`
}
// HasErrors returns true when at least one error is declared
func (er *ErrorResponse) HasErrors() bool {
return er.NetworkError != nil || er.GqlErrors != nil
}
func (er *ErrorResponse) Error() string {
content, err := json.Marshal(er)
if err != nil {
return err.Error()
}
return string(content)
}
// Post sends a http POST request to the graphql endpoint with the given query then unpacks
// the response into the given object.
func (c *Client) Post(ctx context.Context, operationName, query string, respData interface{}, vars map[string]interface{}, httpRequestOptions ...HTTPRequestOption) error {
req, err := c.newRequest(ctx, operationName, query, vars, httpRequestOptions)
if err != nil {
return xerrors.Errorf("don't create request: %w", err)
}
req.Header.Set("Content-Type", "application/json; charset=utf-8")
req.Header.Set("Accept", "application/json; charset=utf-8")
resp, err := c.Client.Do(req)
if err != nil {
return xerrors.Errorf("request failed: %w", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return xerrors.Errorf("failed to read response body: %w", err)
}
return parseResponse(body, resp.StatusCode, respData)
}
func parseResponse(body []byte, httpCode int, result interface{}) error {
errResponse := &ErrorResponse{}
isKOCode := httpCode < 200 || 299 < httpCode
if isKOCode {
errResponse.NetworkError = &HTTPError{
Code: httpCode,
Message: fmt.Sprintf("Response body %s", string(body)),
}
}
// some servers return a graphql error with a non OK http code, try anyway to parse the body
if err := unmarshal(body, result); err != nil {
if gqlErr, ok := err.(*GqlErrorList); ok {
errResponse.GqlErrors = &gqlErr.Errors
} else if !isKOCode { // if is KO code there is already the http error, this error should not be returned
return err
}
}
if errResponse.HasErrors() {
return errResponse
}
return nil
}
// response is a GraphQL layer response from a handler.
type response struct {
Data json.RawMessage `json:"data"`
Errors json.RawMessage `json:"errors"`
}
func unmarshal(data []byte, res interface{}) error {
resp := response{}
if err := json.Unmarshal(data, &resp); err != nil {
return xerrors.Errorf("failed to decode data %s: %w", string(data), err)
}
if resp.Errors != nil && len(resp.Errors) > 0 {
// try to parse standard graphql error
errors := &GqlErrorList{}
if e := json.Unmarshal(data, errors); e != nil {
return xerrors.Errorf("faild to parse graphql errors. Response content %s - %w ", string(data), e)
}
return errors
}
if err := graphqljson.UnmarshalData(resp.Data, res); err != nil {
return xerrors.Errorf("failed to decode data into response %s: %w", string(data), err)
}
return nil
}