...
1 package graphql
2
3 import (
4 "context"
5 "time"
6
7 "github.com/vektah/gqlparser/v2/ast"
8 )
9
10 type key string
11
12 const resolverCtx key = "resolver_context"
13
14
15 type ResolverContext = FieldContext
16
17 type FieldContext struct {
18 Parent *FieldContext
19
20 Object string
21
22 Args map[string]interface{}
23
24 Field CollectedField
25
26 Index *int
27
28 Result interface{}
29
30 IsMethod bool
31
32 IsResolver bool
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 Child func(context.Context, CollectedField) (*FieldContext, error)
52 }
53
54 type FieldStats struct {
55
56 Started time.Time
57
58
59 ArgumentsCompleted time.Time
60
61
62 Completed time.Time
63 }
64
65 func (r *FieldContext) Path() ast.Path {
66 var path ast.Path
67 for it := r; it != nil; it = it.Parent {
68 if it.Index != nil {
69 path = append(path, ast.PathIndex(*it.Index))
70 } else if it.Field.Field != nil {
71 path = append(path, ast.PathName(it.Field.Alias))
72 }
73 }
74
75
76 for i := len(path)/2 - 1; i >= 0; i-- {
77 opp := len(path) - 1 - i
78 path[i], path[opp] = path[opp], path[i]
79 }
80
81 return path
82 }
83
84
85 func GetResolverContext(ctx context.Context) *ResolverContext {
86 return GetFieldContext(ctx)
87 }
88
89 func GetFieldContext(ctx context.Context) *FieldContext {
90 if val, ok := ctx.Value(resolverCtx).(*FieldContext); ok {
91 return val
92 }
93 return nil
94 }
95
96 func WithFieldContext(ctx context.Context, rc *FieldContext) context.Context {
97 rc.Parent = GetFieldContext(ctx)
98 return context.WithValue(ctx, resolverCtx, rc)
99 }
100
101 func equalPath(a ast.Path, b ast.Path) bool {
102 if len(a) != len(b) {
103 return false
104 }
105
106 for i := 0; i < len(a); i++ {
107 if a[i] != b[i] {
108 return false
109 }
110 }
111
112 return true
113 }
114
View as plain text