forked from infiotinc/gqlgenc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
115 lines (93 loc) · 2.64 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
package client
import (
"context"
"fmt"
"github.com/DENKweit/gqlgenc/client/transport"
)
type extensions struct {
aroundRequest []AroundRequest
}
func (es *extensions) Use(e Extension) {
if a, ok := e.(AroundRequest); ok {
es.aroundRequest = append(es.aroundRequest, a)
}
}
func (es *extensions) RunAroundRequest(req transport.Request, h RequestHandler) transport.Response {
run := h
for _, _e := range es.aroundRequest {
e := _e // Local ref
next := run // Local ref
run = func(req transport.Request) transport.Response {
return e.AroundRequest(req, next)
}
}
return run(req)
}
type Client struct {
Transport transport.Transport
extensions
}
func (c *Client) doSingle(
ctx context.Context,
operation transport.Operation,
operationName string,
query string,
variables map[string]interface{},
t interface{},
) (transport.OperationResponse, error) {
res := c.do(transport.Request{
Context: ctx,
Operation: operation,
Query: query,
OperationName: operationName,
Variables: variables,
})
defer res.Close()
if !res.Next() {
if err := res.Err(); err != nil {
return transport.OperationResponse{}, err
}
return transport.OperationResponse{}, fmt.Errorf("no response")
}
opres := res.Get()
err := opres.UnmarshalData(t)
if len(opres.Errors) > 0 {
return opres, opres.Errors
}
return opres, err
}
func (c *Client) do(req transport.Request) transport.Response {
if req.Extensions == nil {
req.Extensions = map[string]interface{}{}
}
res := c.RunAroundRequest(req, c.Transport.Request)
go func() {
select {
case <-req.Context.Done():
res.Close()
case <-res.Done():
}
}()
return res
}
// Query runs a query
// operationName is optional
func (c *Client) Query(ctx context.Context, operationName string, query string, variables map[string]interface{}, t interface{}) (transport.OperationResponse, error) {
return c.doSingle(ctx, transport.Query, operationName, query, variables, t)
}
// Mutation runs a mutation
// operationName is optional
func (c *Client) Mutation(ctx context.Context, operationName string, query string, variables map[string]interface{}, t interface{}) (transport.OperationResponse, error) {
return c.doSingle(ctx, transport.Mutation, operationName, query, variables, t)
}
// Subscription starts a GQL subscription
// operationName is optional
func (c *Client) Subscription(ctx context.Context, operationName string, query string, variables map[string]interface{}) transport.Response {
return c.do(transport.Request{
Context: ctx,
Operation: transport.Subscription,
Query: query,
OperationName: operationName,
Variables: variables,
})
}