-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphql.go
140 lines (115 loc) · 3.12 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package graphql
import (
"bytes"
"context"
"encoding/json"
"log"
"net/http"
)
type Client struct {
HttpClient *http.Client
Endpoint string
Header http.Header
}
func NewGraphQLClient(endpoint string, httpClient *http.Client, header http.Header) *Client {
return &Client{
HttpClient: httpClient,
Endpoint: endpoint,
Header: header,
}
}
func (c *Client) Query(graphqlRequest Request) (*Response, error) {
// Build the request body
body, _ := json.Marshal(graphqlRequest)
// Make the request
req, err := http.NewRequest(http.MethodPost, c.Endpoint, bytes.NewBuffer(body))
if err != nil {
log.Println("error creating request:", err)
return nil, err
}
req.Header = c.Header
req.Header.Set("Content-Type", "application/json")
// Get the response
resp, err := c.HttpClient.Do(req)
if err != nil {
log.Printf("error making request: %v", err)
return nil, err
}
// Decode the response body
var result Response
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
log.Printf("error decoding response: %v", err)
return nil, err
}
return &result, nil
}
func (c *Client) QueryWithContext(ctx context.Context, graphqlRequest Request, cookies ...*http.Cookie) (*Response, error) {
// Build the request body
body, _ := json.Marshal(graphqlRequest)
// Make the request with the provided context
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.Endpoint, bytes.NewBuffer(body))
if err != nil {
log.Println("error creating request:", err)
return nil, err
}
req.Header = c.Header
req.Header.Set("Content-Type", "application/json")
if len(cookies) > 0 {
for _, v := range cookies {
req.AddCookie(v)
}
}
// Get the response
resp, err := c.HttpClient.Do(req)
if err != nil {
log.Printf("error making request: %v", err)
return nil, err
}
// Decode the response body
var result Response
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
log.Printf("error decoding response: %v", err)
return nil, err
}
return &result, nil
}
func (c *Client) Mutation(graphqlRequest Request) (*Response, error) {
// Build the request body
body, _ := json.Marshal(graphqlRequest)
// Make the request
req, err := http.NewRequest(http.MethodPost, c.Endpoint, bytes.NewBuffer(body))
if err != nil {
return nil, err
}
req.Header = c.Header
req.Header.Set("Content-Type", "application/json")
// Get the response
resp, err := c.HttpClient.Do(req)
if err != nil {
return nil, err
}
// Decode the response body
var result Response
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, err
}
return &result, nil
}
type Response struct {
Data any `json:"data"`
Errors []Error `json:"errors"`
Extensions map[string]any `json:"extensions"`
}
type Request struct {
Query string `json:"query,omitempty"`
Variables map[string]any `json:"variables,omitempty"`
}
type Error struct {
Message string `json:"message"`
Locations []ErrorLocation `json:"locations"`
Path []any `json:"path"`
}
type ErrorLocation struct {
Line int `json:"line"`
Column int `json:"column"`
}