-
Notifications
You must be signed in to change notification settings - Fork 25
/
node.go
234 lines (205 loc) · 5.29 KB
/
node.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package gqlserver
import (
"bytes"
"crypto/rand"
"crypto/subtle"
"encoding/base32"
"errors"
"fmt"
"reflect"
"strconv"
"github.com/graphql-go/graphql"
)
var (
idKey [64]byte
idEncoding = base32.HexEncoding.WithPadding(base32.NoPadding)
nodeTypes = map[string]*NodeTypeBase{}
errNotFound = errors.New("node not found")
errNoDelete = errors.New("cannot delete node")
)
func xorID(value []byte) []byte {
for off := 0; off < len(value); off += len(idKey) {
subtle.XORBytes(value[off:], value[off:], idKey[:])
}
return value
}
func makeID(prefix string, suffix any) (id string) {
var buffer bytes.Buffer
fmt.Fprintf(&buffer, "%s:%v", prefix, suffix)
return idEncoding.EncodeToString(xorID(buffer.Bytes()))
}
func parseID(id string) (prefix, suffix string, ok bool) {
value, e := idEncoding.DecodeString(id)
if e != nil {
return
}
value = xorID(value)
tokens := bytes.SplitN(value, []byte{':'}, 2)
if len(tokens) != 2 {
return
}
return string(tokens[0]), string(tokens[1]), true
}
// RetrieveNode retrieves a node.
func RetrieveNode(id string) (any, error) {
prefix, suffix, ok := parseID(id)
if !ok {
return nil, nil
}
if nt := nodeTypes[prefix]; nt != nil {
return nt.retrieveAny(suffix)
}
return nil, nil
}
// NodeTypeBase contains non-generic fields of NodeType.
type NodeTypeBase struct {
Object *graphql.Object
prefix string
retrieveAny func(suffix string) (node any, e error)
deleteByID func(suffix string) (ok bool, e error)
}
// NodeType defines a Node subtype.
type NodeType[T any] struct {
NodeTypeBase
retrieve func(suffix string) T
}
// Retrieve retrieves node by ID.
func (nt *NodeType[T]) Retrieve(id string) (node T) {
prefix, suffix, ok := parseID(id)
if !ok || prefix != nt.prefix {
return node
}
return nt.retrieve(suffix)
}
// NewNodeType creates a NodeType.
func NewNodeType[T any](oc graphql.ObjectConfig, nc NodeConfig[T]) (nt *NodeType[T]) {
fields, _ := oc.Fields.(graphql.Fields)
fields["id"] = &graphql.Field{
Type: graphql.NewNonNull(graphql.ID),
Description: "Globally unique ID.",
Resolve: nc.makeResolveID(oc.Name, fields),
}
oc.Fields = fields
nodeInterface.AppendTo(&oc)
obj := graphql.NewObject(oc)
ImplementsInterface[T](obj, nodeInterface)
nt = &NodeType[T]{
NodeTypeBase: NodeTypeBase{
Object: obj,
prefix: oc.Name,
},
retrieve: nc.makeRetrieve(),
}
nt.retrieveAny = func(suffix string) (any, error) {
node := nt.retrieve(suffix)
if reflect.ValueOf(node).IsZero() {
return nil, errNotFound
}
return node, nil
}
nt.deleteByID = func(suffix string) (ok bool, e error) {
node := nt.retrieve(suffix)
if reflect.ValueOf(node).IsZero() {
return false, nil
}
if nc.Delete == nil {
return true, errNoDelete
}
return true, nc.Delete(node)
}
nodeTypes[nt.prefix] = &nt.NodeTypeBase
return nt
}
// NodeConfig contains options to construct a NodeType.
type NodeConfig[T any] struct {
// GetID extracts un-prefixed ID from the source object.
GetID func(source T) string
// Retrieve fetches an object from un-prefixed ID.
// Returning zero value indicates the object does not exist.
Retrieve func(id string) T
// RetrieveInt fetches an object from un-prefixed ID parsed as integer.
RetrieveInt func(id int) T
// Delete deletes the source object.
Delete func(source T) error
}
func (nc NodeConfig[T]) makeResolveID(prefix string, fields graphql.Fields) graphql.FieldResolveFn {
if nc.GetID != nil {
return func(p graphql.ResolveParams) (any, error) {
return makeID(prefix, nc.GetID(p.Source.(T))), nil
}
}
if nidField := fields["nid"]; nidField != nil {
switch nidField.Type {
case NonNullID, NonNullInt, NonNullString:
return func(p graphql.ResolveParams) (any, error) {
nid, e := nidField.Resolve(p)
if e != nil {
return nil, e
}
return makeID(prefix, nid), nil
}
}
}
logger.Panic("cannot resolve 'id' field")
return nil
}
func (nc NodeConfig[T]) makeRetrieve() func(suffix string) T {
switch {
case nc.Retrieve != nil:
return nc.Retrieve
case nc.RetrieveInt != nil:
return func(suffix string) (node T) {
n, e := strconv.Atoi(suffix)
if e != nil {
return
}
return nc.RetrieveInt(n)
}
}
logger.Panic("either Retrieve or RetrieveInt must be set")
return nil
}
var nodeInterface = NewInterface(graphql.InterfaceConfig{
Name: "Node",
Fields: graphql.Fields{
"id": &graphql.Field{
Type: NonNullID,
},
},
})
func init() {
rand.Read(idKey[:])
AddQuery(&graphql.Field{
Name: "node",
Description: "Retrieve object by global ID.",
Args: graphql.FieldConfigArgument{
"id": &graphql.ArgumentConfig{
Type: NonNullID,
},
},
Type: nodeInterface.Interface,
Resolve: func(p graphql.ResolveParams) (any, error) {
return RetrieveNode(p.Args["id"].(string))
},
})
AddMutation(&graphql.Field{
Name: "delete",
Description: "Delete object by global ID. The result indicates whether the object previously exists.",
Args: graphql.FieldConfigArgument{
"id": &graphql.ArgumentConfig{
Type: NonNullID,
},
},
Type: NonNullBoolean,
Resolve: func(p graphql.ResolveParams) (any, error) {
prefix, suffix, ok := parseID(p.Args["id"].(string))
if !ok {
return false, nil
}
if nt := nodeTypes[prefix]; nt != nil {
return nt.deleteByID(suffix)
}
return false, nil
},
})
}