-
Notifications
You must be signed in to change notification settings - Fork 13
/
clientGQL.go
102 lines (84 loc) · 2.71 KB
/
clientGQL.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
package opslevel
import (
"context"
"fmt"
"net/http"
"strings"
"github.com/hashicorp/go-retryablehttp"
"github.com/hasura/go-graphql-client"
)
type Client struct {
pageSize int
client *graphql.Client
}
func NewGQLClient(options ...Option) *Client {
settings := newClientSettings(options...)
retryClient := retryablehttp.NewClient()
retryClient.RetryMax = settings.retries
retryClient.Logger = nil
standardClient := retryClient.StandardClient()
var url string
if strings.Contains(settings.url, "/LOCAL_TESTING/") {
url = settings.url
} else {
url = fmt.Sprintf("%s/graphql", settings.url)
}
auth := fmt.Sprintf("Bearer %s", settings.token)
modifier := graphql.RequestModifier(
func(r *http.Request) {
r.Header.Add("Authorization", auth)
for key, value := range settings.headers {
r.Header.Add(key, value)
}
})
return &Client{
pageSize: settings.pageSize,
client: graphql.NewClient(url, standardClient).WithRequestModifier(modifier),
}
}
func (client *Client) InitialPageVariables() PayloadVariables {
return PayloadVariables{
"after": "",
"first": client.pageSize,
}
}
func (client *Client) InitialPageVariablesPointer() *PayloadVariables {
v := PayloadVariables{
"after": "",
"first": client.pageSize,
}
return &v
}
func (client *Client) Query(q interface{}, variables map[string]interface{}, options ...graphql.Option) error {
return client.QueryCTX(context.Background(), q, variables, options...)
}
func (client *Client) QueryCTX(ctx context.Context, q interface{}, variables map[string]interface{}, options ...graphql.Option) error {
return client.client.Query(ctx, q, variables, options...)
}
func (client *Client) Mutate(m interface{}, variables map[string]interface{}, options ...graphql.Option) error {
return client.MutateCTX(context.Background(), m, variables, options...)
}
func (client *Client) MutateCTX(ctx context.Context, m interface{}, variables map[string]interface{}, options ...graphql.Option) error {
return client.client.Mutate(ctx, m, variables, options...)
}
func (client *Client) ExecRaw(q string, variables map[string]interface{}, options ...graphql.Option) ([]byte, error) {
return client.ExecRawCTX(context.Background(), q, variables, options...)
}
func (client *Client) ExecRawCTX(ctx context.Context, q string, variables map[string]interface{}, options ...graphql.Option) ([]byte, error) {
return client.client.ExecRaw(ctx, q, variables, options...)
}
func (client *Client) Validate() error {
var q struct {
Account struct {
Id ID
}
}
err := client.Query(&q, nil)
if err != nil {
return fmt.Errorf("client validation error: %s", err.Error())
}
return nil
}
func WithName(name string) graphql.Option {
return graphql.OperationName(name)
}