forked from samsarahq/thunder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
executor.go
413 lines (357 loc) · 10.7 KB
/
executor.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
package graphql
import (
"bytes"
"context"
"errors"
"fmt"
"reflect"
"runtime"
"strings"
"sync"
"github.com/samsarahq/thunder/concurrencylimiter"
"github.com/samsarahq/thunder/reactive"
)
type pathError struct {
inner error
path []string
}
func nestPathError(key string, err error) error {
// Don't nest SanitzedError's, as they are intended for human consumption.
if se, ok := err.(SanitizedError); ok {
return se
}
if pe, ok := err.(*pathError); ok {
return &pathError{
inner: pe.inner,
path: append(pe.path, key),
}
}
return &pathError{
inner: err,
path: []string{key},
}
}
func ErrorCause(err error) error {
if pe, ok := err.(*pathError); ok {
return pe.inner
}
return err
}
func (pe *pathError) Error() string {
var buffer bytes.Buffer
for i := len(pe.path) - 1; i >= 0; i-- {
if i < len(pe.path)-1 {
buffer.WriteString(".")
}
buffer.WriteString(pe.path[i])
}
buffer.WriteString(": ")
buffer.WriteString(pe.inner.Error())
return buffer.String()
}
func isNilArgs(args interface{}) bool {
m, ok := args.(map[string]interface{})
return args == nil || (ok && len(m) == 0)
}
// unwrap will return the value associated with a pointer type, or nil if the
// pointer is nil
func unwrap(v interface{}) interface{} {
i := reflect.ValueOf(v)
for i.Kind() == reflect.Ptr && !i.IsNil() {
i = i.Elem()
}
if i.Kind() == reflect.Invalid {
return nil
}
return i.Interface()
}
// PrepareQuery checks that the given selectionSet matches the schema typ, and
// parses the args in selectionSet
func PrepareQuery(typ Type, selectionSet *SelectionSet) error {
switch typ := typ.(type) {
case *Scalar:
if selectionSet != nil {
return NewClientError("scalar field must have no selections")
}
return nil
case *Enum:
if selectionSet != nil {
return NewClientError("enum field must have no selections")
}
return nil
case *Union:
if selectionSet == nil {
return NewClientError("object field must have selections")
}
for _, fragment := range selectionSet.Fragments {
for typString, graphqlTyp := range typ.Types {
if fragment.On != typString {
continue
}
if err := PrepareQuery(graphqlTyp, fragment.SelectionSet); err != nil {
return err
}
}
}
for _, selection := range selectionSet.Selections {
if selection.Name == "__typename" {
if !isNilArgs(selection.Args) {
return NewClientError(`error parsing args for "__typename": no args expected`)
}
if selection.SelectionSet != nil {
return NewClientError(`scalar field "__typename" must have no selection`)
}
continue
}
return NewClientError(`unknown field "%s"`, selection.Name)
}
return nil
case *Object:
if selectionSet == nil {
return NewClientError("object field must have selections")
}
for _, selection := range selectionSet.Selections {
if selection.Name == "__typename" {
if !isNilArgs(selection.Args) {
return NewClientError(`error parsing args for "__typename": no args expected`)
}
if selection.SelectionSet != nil {
return NewClientError(`scalar field "__typename" must have no selection`)
}
continue
}
field, ok := typ.Fields[selection.Name]
if !ok {
return NewClientError(`unknown field "%s"`, selection.Name)
}
// Only parse args once for a given selection.
if !selection.parsed {
parsed, err := field.ParseArguments(selection.Args)
if err != nil {
return NewClientError(`error parsing args for "%s": %s`, selection.Name, err)
}
selection.Args = parsed
selection.parsed = true
}
if err := PrepareQuery(field.Type, selection.SelectionSet); err != nil {
return err
}
}
for _, fragment := range selectionSet.Fragments {
if err := PrepareQuery(typ, fragment.SelectionSet); err != nil {
return err
}
}
return nil
case *List:
return PrepareQuery(typ.Type, selectionSet)
case *NonNull:
return PrepareQuery(typ.Type, selectionSet)
default:
panic("unknown type kind")
}
}
type panicError struct {
message string
}
func (p panicError) Error() string {
return p.message
}
func safeResolve(ctx context.Context, field *Field, source, args interface{}, selectionSet *SelectionSet) (result interface{}, err error) {
defer func() {
if panicErr := recover(); panicErr != nil {
const size = 64 << 10
buf := make([]byte, size)
buf = buf[:runtime.Stack(buf, false)]
result, err = nil, fmt.Errorf("graphql: panic: %v\n%s", panicErr, buf)
}
}()
return field.Resolve(ctx, source, args, selectionSet)
}
type resolveAndExecuteCacheKey struct {
field *Field
source interface{}
selection *Selection
}
func (e *Executor) resolveAndExecute(ctx context.Context, field *Field, source interface{}, selection *Selection) (interface{}, error) {
if field.Expensive {
// TODO: Skip goroutine for cached value
ctx, release := concurrencylimiter.Acquire(ctx)
return fork(func() (interface{}, error) {
defer release()
value := reflect.ValueOf(source)
// cache the body of resolve and excecute so that if the source doesn't change, we
// don't need to recompute
key := resolveAndExecuteCacheKey{field: field, source: source, selection: selection}
// some types can't be put in a map; for those, use a always different value
// as source
if value.IsValid() && !value.Type().Comparable() {
// TODO: Warn, or somehow prevent using type-system?
key.source = new(byte)
}
// TODO: Consider cacheing resolve and execute independently
resolvedValue, err := reactive.Cache(ctx, key, func(ctx context.Context) (interface{}, error) {
value, err := safeResolve(ctx, field, source, selection.Args, selection.SelectionSet)
if err != nil {
return nil, err
}
// Release concurrency token before recursing into execute. It will attempt to
// grab another concurrency token.
release()
e.mu.Lock()
value, err = e.execute(ctx, field.Type, value, selection.SelectionSet)
e.mu.Unlock()
if err != nil {
return nil, err
}
return await(value)
})
return resolvedValue, err
}), nil
}
value, err := safeResolve(ctx, field, source, selection.Args, selection.SelectionSet)
if err != nil {
return nil, err
}
return e.execute(ctx, field.Type, value, selection.SelectionSet)
}
func (e *Executor) executeUnion(ctx context.Context, typ *Union, source interface{}, selectionSet *SelectionSet) (interface{}, error) {
value := reflect.ValueOf(source)
if value.Kind() == reflect.Ptr && value.IsNil() {
return nil, nil
}
fields := make(map[string]interface{})
for _, selection := range selectionSet.Selections {
if selection.Name == "__typename" {
fields[selection.Alias] = typ.Name
continue
}
}
// For every inline fragment spread, check if the current concrete type
// matches and execute that object.
var possibleTypes []string
for typString, graphqlTyp := range typ.Types {
inner := reflect.ValueOf(source)
if inner.Kind() == reflect.Ptr && inner.Elem().Kind() == reflect.Struct {
inner = inner.Elem()
}
inner = inner.FieldByName(typString)
if inner.IsNil() {
continue
}
possibleTypes = append(possibleTypes, graphqlTyp.String())
for _, fragment := range selectionSet.Fragments {
if fragment.On != typString {
continue
}
resolved, err := e.executeObject(ctx, graphqlTyp, inner.Interface(), fragment.SelectionSet)
if err != nil {
return nil, nestPathError(typString, err)
}
for k, v := range resolved.(map[string]interface{}) {
fields[k] = v
}
}
}
if len(possibleTypes) > 1 {
return nil, fmt.Errorf("union type field should only return one value, but received: %s", strings.Join(possibleTypes, " "))
}
return fields, nil
}
// executeObject executes an object query
func (e *Executor) executeObject(ctx context.Context, typ *Object, source interface{}, selectionSet *SelectionSet) (interface{}, error) {
value := reflect.ValueOf(source)
if value.Kind() == reflect.Ptr && value.IsNil() {
return nil, nil
}
selections := Flatten(selectionSet)
fields := make(map[string]interface{})
// for every selection, resolve the value and store it in the output object
for _, selection := range selections {
if selection.Name == "__typename" {
fields[selection.Alias] = typ.Name
continue
}
field := typ.Fields[selection.Name]
resolved, err := e.resolveAndExecute(ctx, field, source, selection)
if err != nil {
return nil, nestPathError(selection.Alias, err)
}
fields[selection.Alias] = resolved
}
if typ.Key != nil {
value, err := e.resolveAndExecute(ctx, &Field{Type: &Scalar{Type: "string"}, Resolve: typ.Key}, source, &Selection{})
if err != nil {
return nil, nestPathError("__key", err)
}
fields["__key"] = value
}
return fields, nil
}
var emptyList = []interface{}{}
// executeList executes a set query
func (e *Executor) executeList(ctx context.Context, typ *List, source interface{}, selectionSet *SelectionSet) (interface{}, error) {
if reflect.ValueOf(source).IsNil() {
return emptyList, nil
}
// iterate over arbitrary slice types using reflect
slice := reflect.ValueOf(source)
items := make([]interface{}, slice.Len())
// resolve every element in the slice
for i := 0; i < slice.Len(); i++ {
value := slice.Index(i)
resolved, err := e.execute(ctx, typ.Type, value.Interface(), selectionSet)
if err != nil {
return nil, nestPathError(fmt.Sprint(i), err)
}
items[i] = resolved
}
return items, nil
}
// execute executes a query by dispatches according to typ
func (e *Executor) execute(ctx context.Context, typ Type, source interface{}, selectionSet *SelectionSet) (interface{}, error) {
if err := ctx.Err(); err != nil {
return nil, err
}
switch typ := typ.(type) {
case *Scalar:
if typ.Unwrapper != nil {
return typ.Unwrapper(source)
}
return unwrap(source), nil
case *Enum:
val := unwrap(source)
if mapVal, ok := typ.ReverseMap[val]; ok {
return mapVal, nil
}
return nil, errors.New("enum is not valid")
case *Union:
return e.executeUnion(ctx, typ, source, selectionSet)
case *Object:
return e.executeObject(ctx, typ, source, selectionSet)
case *List:
return e.executeList(ctx, typ, source, selectionSet)
case *NonNull:
return e.execute(ctx, typ.Type, source, selectionSet)
default:
panic(typ)
}
}
type Executor struct {
mu sync.Mutex
}
// Execute executes a query by dispatches according to typ
func (e *Executor) Execute(ctx context.Context, typ Type, source interface{}, query *Query) (interface{}, error) {
e.mu.Lock()
value, err := e.execute(ctx, typ, source, query.SelectionSet)
e.mu.Unlock()
// Await the promise if things look good so far.
if err == nil {
value, err = await(value)
}
// Maybe error wrap if we have an error and a name to attach.
if err != nil && query.Name != "" {
err = nestPathError(query.Name, err)
}
return value, err
}