forked from shurcooL/graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphql.go
120 lines (111 loc) · 3.14 KB
/
graphql.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
package graphql
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/merico-dev/graphql/internal/jsonutil"
"golang.org/x/net/context/ctxhttp"
)
// Client is a GraphQL client.
type Client struct {
url string // GraphQL server URL.
httpClient *http.Client
}
// NewClient creates a GraphQL client targeting the specified GraphQL server URL.
// If httpClient is nil, then http.DefaultClient is used.
func NewClient(url string, httpClient *http.Client) *Client {
if httpClient == nil {
httpClient = http.DefaultClient
}
return &Client{
url: url,
httpClient: httpClient,
}
}
// Query executes a single GraphQL query request,
// with a query derived from q, populating the response into it.
// q should be a pointer to struct that corresponds to the GraphQL schema.
func (c *Client) Query(ctx context.Context, q interface{}, variables map[string]interface{}) ([]DataError, error) {
query, variables := ConstructQuery(q, variables)
data, dataErrors, err := c.do(ctx, query, q, variables)
if err != nil {
return nil, err
}
if data != nil {
err = jsonutil.UnmarshalGraphQL(*data, q)
if err != nil {
return nil, err
}
}
return dataErrors, nil
}
// Mutate executes a single GraphQL mutation request,
// with a mutation derived from m, populating the response into it.
// m should be a pointer to struct that corresponds to the GraphQL schema.
func (c *Client) Mutate(ctx context.Context, m interface{}, variables map[string]interface{}) ([]DataError, error) {
query := ConstructMutation(m, variables)
data, dataErrors, err := c.do(ctx, query, m, variables)
if err != nil {
return nil, err
}
if data != nil {
err = jsonutil.UnmarshalGraphQL(*data, m)
if err != nil {
return nil, err
}
}
return dataErrors, nil
}
// do executes a single GraphQL operation.
func (c *Client) do(ctx context.Context, query string, v interface{}, variables map[string]interface{}) (*json.RawMessage, []DataError, error) {
in := struct {
Query string `json:"query"`
Variables map[string]interface{} `json:"variables,omitempty"`
}{
Query: query,
Variables: variables,
}
var buf bytes.Buffer
err := json.NewEncoder(&buf).Encode(in)
if err != nil {
return nil, nil, err
}
resp, err := ctxhttp.Post(ctx, c.httpClient, c.url, "application/json", &buf)
if err != nil {
return nil, nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := ioutil.ReadAll(resp.Body)
return nil, nil, fmt.Errorf("non-200 OK status code: %v body: %q", resp.Status, body)
}
var out struct {
Data *json.RawMessage
Errors []DataError
//Extensions interface{} // Unused.
}
err = json.NewDecoder(resp.Body).Decode(&out)
if err != nil {
return nil, nil, err
}
if len(out.Errors) > 0 {
return out.Data, out.Errors, nil
}
return out.Data, nil, nil
}
// DataError represents the "errors" in a response from a GraphQL server.
// Specification: https://facebook.github.io/graphql/#sec-Errors.
type DataError struct {
Message string
Locations []struct {
Line int
Column int
}
}
// Error implements error interface.
func (e DataError) Error() string {
return e.Message
}