-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
125 lines (99 loc) Β· 3.22 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
package gqlclient
import (
"bytes"
"context"
"encoding/json"
"io/ioutil"
"net/http"
"github.com/pkg/errors"
"github.com/steebchen/gqlclient/structs"
)
// Error is a GraphQL Error
type Error struct {
Message string `json:"message,omitempty"`
Path []string `json:"path,omitempty"`
Extensions map[string]interface{} `json:"extensions,omitempty"`
}
// Response is the payload for a GraphQL response
type Response struct {
Data interface{} `json:"data,omitempty"`
Errors []Error `json:"errors,omitempty"`
Extensions map[string]interface{} `json:"extensions,omitempty"`
}
// gqlRequest is the payload for GraphQL queries
type gqlRequest struct {
Query string `json:"query"`
Variables map[string]interface{} `json:"variables"`
OperationName *string `json:"operationName"`
}
// Send a GraphQL request to struct or map
func (c *Client) Send(ctx context.Context, dest interface{}, query string, variables interface{}) (*Response, error) {
unboxedVars, err := structs.StructToMap(variables)
if err != nil {
return nil, errors.Wrap(err, "StructToMap failed")
}
resp, err := c.Raw(ctx, query, unboxedVars)
if err != nil {
return nil, err
}
if resp.Errors != nil {
return resp, nil
}
// unpack even if there is an error so we can see partial responses
unpackErr := structs.Unpack(resp.Data, dest)
return resp, unpackErr
}
// Raw sends a basic GraphQL request without any struct types.
// Parameter `variables` can be either a map or a struct
func (c *Client) Raw(ctx context.Context, query string, variables map[string]interface{}) (*Response, error) {
payload := gqlRequest{
Query: query,
Variables: variables,
}
response := &Response{}
err := c.do(ctx, payload, variables, response)
if err != nil {
return nil, errors.Wrap(err, "raw do")
}
return response, err
}
func (c *Client) Full(ctx context.Context, query string, variables map[string]interface{}, response interface{}) error {
payload := gqlRequest{
Query: query,
Variables: variables,
}
return c.do(ctx, payload, variables, response)
}
func (c *Client) do(ctx context.Context, payload gqlRequest, variables map[string]interface{}, response interface{}) error {
requestBody, err := json.Marshal(payload)
if err != nil {
return errors.Wrap(err, "raw encode")
}
req, err := http.NewRequest("POST", c.url, bytes.NewBuffer(requestBody))
if err != nil {
return err
}
req.Header.Set("content-type", "application/json")
req = req.WithContext(ctx)
rawResponse, err := c.http.Do(req)
if err != nil {
return errors.Wrap(err, "raw post")
}
defer func() {
_ = rawResponse.Body.Close()
}()
responseBody, err := ioutil.ReadAll(rawResponse.Body)
if err != nil {
return errors.Wrap(err, "raw read")
}
if rawResponse.StatusCode != http.StatusOK {
return errors.Errorf("http status code %d with response %s", rawResponse.StatusCode, responseBody)
}
// decode it into map string first, let mapstructure do the final decode
// mapstructure is way stricter about unknown fields, can handle embedded structs and more
err = json.Unmarshal(responseBody, response)
if err != nil {
return errors.Wrap(err, "raw decode")
}
return nil
}