/
transport.go
74 lines (59 loc) · 1.56 KB
/
transport.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
package transport
import (
"context"
"encoding/json"
"github.com/vektah/gqlparser/v2/gqlerror"
)
type Operation string
const (
Query Operation = "query"
Mutation Operation = "mutation"
Subscription Operation = "subscription"
)
type OperationRequest struct {
Query string `json:"query,omitempty"`
OperationName string `json:"operationName,omitempty"`
Variables map[string]interface{} `json:"variables,omitempty"`
Extensions map[string]interface{} `json:"extensions,omitempty"`
}
func NewOperationRequestFromRequest(req Request) OperationRequest {
return OperationRequest{
Query: req.Query,
OperationName: req.OperationName,
Variables: req.Variables,
Extensions: req.Extensions,
}
}
type OperationResponse struct {
Data json.RawMessage `json:"data,omitempty"`
Errors gqlerror.List `json:"errors,omitempty"`
Extensions RawExtensions `json:"extensions,omitempty"`
}
func (r OperationResponse) UnmarshalData(t interface{}) error {
if r.Data == nil {
return nil
}
return json.Unmarshal(r.Data, t)
}
type RawExtensions map[string]json.RawMessage
func (es RawExtensions) Unmarshal(name string, t interface{}) error {
if es == nil {
return nil
}
ex, ok := es[name]
if !ok {
return nil
}
return json.Unmarshal(ex, t)
}
type Request struct {
Context context.Context
Operation Operation
OperationName string
Query string
Variables map[string]interface{}
Extensions map[string]interface{}
}
type Transport interface {
Request(req Request) Response
}