Skip to content

Commit af7c05d

Browse files
authored
feat(client): expose Full method to provide custom responses (#11)
1 parent 3fb4256 commit af7c05d

File tree

2 files changed

+31
-15
lines changed

2 files changed

+31
-15
lines changed

client.go

+30-14
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ type Response struct {
2929
// request is the payload for GraphQL queries
3030
type gqlRequest struct {
3131
Query string `json:"query"`
32-
Variables map[string]interface{} `json:"variables,omitempty"`
33-
OperationName string `json:"operationName,omitempty"`
32+
Variables map[string]interface{} `json:"variables"`
33+
OperationName *string `json:"operationName"`
3434
}
3535

3636
// Send a GraphQL request to struct or map
@@ -58,30 +58,47 @@ func (c *Client) Send(ctx context.Context, dest interface{}, query string, varia
5858
// Raw sends a basic GraphQL request without any struct types.
5959
// Parameter `variables` can be either a map or a struct
6060
func (c *Client) Raw(ctx context.Context, query string, variables map[string]interface{}) (*Response, error) {
61-
var err error
61+
payload := gqlRequest{
62+
Query: query,
63+
Variables: variables,
64+
}
65+
66+
response := &Response{}
67+
err := c.do(ctx, payload, variables, response)
68+
if err != nil {
69+
return nil, errors.Wrap(err, "raw decode")
70+
}
6271

63-
payload := &gqlRequest{
72+
return response, err
73+
}
74+
75+
func (c *Client) Full(ctx context.Context, query string, variables map[string]interface{}, response interface{}) error {
76+
payload := gqlRequest{
6477
Query: query,
6578
Variables: variables,
6679
}
6780

81+
return c.do(ctx, payload, variables, response)
82+
}
83+
84+
func (c *Client) do(ctx context.Context, payload gqlRequest, variables map[string]interface{}, response interface{}) error {
6885
requestBody, err := json.Marshal(payload)
6986
if err != nil {
70-
return nil, errors.Wrap(err, "raw encode")
87+
return errors.Wrap(err, "raw encode")
7188
}
7289

73-
req, err := http.NewRequest("post", c.url, bytes.NewBuffer(requestBody))
90+
req, err := http.NewRequest("POST", c.url, bytes.NewBuffer(requestBody))
7491

7592
if err != nil {
76-
return nil, err
93+
return err
7794
}
7895

7996
req.Header.Set("content-type", "application/json")
8097
req = req.WithContext(ctx)
8198

8299
rawResponse, err := c.http.Do(req)
83100
if err != nil {
84-
return nil, errors.Wrap(err, "raw post")
101+
return errors.Wrap(err, "raw post")
85102
}
86103
defer func() {
87104
_ = rawResponse.Body.Close()
@@ -90,20 +107,19 @@ func (c *Client) Raw(ctx context.Context, query string, variables map[string]int
90107
responseBody, err := ioutil.ReadAll(rawResponse.Body)
91108

92109
if err != nil {
93-
return nil, errors.Wrap(err, "raw read")
110+
return errors.Wrap(err, "raw read")
94111
}
95112

96113
if rawResponse.StatusCode != http.StatusOK {
97-
return nil, errors.Errorf("http status code %d with response %s", rawResponse.StatusCode, responseBody)
114+
return errors.Errorf("http status code %d with response %s", rawResponse.StatusCode, responseBody)
98115
}
99116

100117
// decode it into map string first, let mapstructure do the final decode
101118
// mapstructure is way stricter about unknown fields, can handle embedded structs and more
102-
respDataRaw := &Response{}
103-
err = json.Unmarshal(responseBody, &respDataRaw)
119+
err = json.Unmarshal(responseBody, &response)
104120
if err != nil {
105-
return nil, errors.Wrap(err, "raw decode")
121+
return errors.Wrap(err, "raw decode")
106122
}
107123

108-
return respDataRaw, nil
124+
return nil
109125
}

client_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func mockServer(t *testing.T) *httptest.Server {
2222
panic(err)
2323
}
2424

25-
expect := `{"query":"query GetUser { user(id: $id) { id name } }","variables":{"id":"1"}}`
25+
expect := `{"query":"query GetUser { user(id: $id) { id name } }","variables":{"id":"1"},"operationName":null}`
2626
require.Equal(t, expect, string(b))
2727

2828
err = json.NewEncoder(w).Encode(map[string]interface{}{

0 commit comments

Comments
 (0)