/
gql_collection.go
174 lines (161 loc) · 4.38 KB
/
gql_collection.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
// Code generated by ent, DO NOT EDIT.
package ent
import (
"context"
"entgo.io/ent/dialect/sql"
"github.com/99designs/gqlgen/graphql"
"github.com/nexentra/aesir/ent/eval"
)
// CollectFields tells the query-builder to eagerly load connected nodes by resolver context.
func (e *EvalQuery) CollectFields(ctx context.Context, satisfies ...string) (*EvalQuery, error) {
fc := graphql.GetFieldContext(ctx)
if fc == nil {
return e, nil
}
if err := e.collectField(ctx, graphql.GetOperationContext(ctx), fc.Field, nil, satisfies...); err != nil {
return nil, err
}
return e, nil
}
func (e *EvalQuery) collectField(ctx context.Context, opCtx *graphql.OperationContext, collected graphql.CollectedField, path []string, satisfies ...string) error {
path = append([]string(nil), path...)
var (
unknownSeen bool
fieldSeen = make(map[string]struct{}, len(eval.Columns))
selectedFields = []string{eval.FieldID}
)
for _, field := range graphql.CollectFields(opCtx, collected.Selections, satisfies) {
switch field.Name {
case "snippet":
if _, ok := fieldSeen[eval.FieldSnippet]; !ok {
selectedFields = append(selectedFields, eval.FieldSnippet)
fieldSeen[eval.FieldSnippet] = struct{}{}
}
case "result":
if _, ok := fieldSeen[eval.FieldResult]; !ok {
selectedFields = append(selectedFields, eval.FieldResult)
fieldSeen[eval.FieldResult] = struct{}{}
}
case "time":
if _, ok := fieldSeen[eval.FieldTime]; !ok {
selectedFields = append(selectedFields, eval.FieldTime)
fieldSeen[eval.FieldTime] = struct{}{}
}
case "id":
case "__typename":
default:
unknownSeen = true
}
}
if !unknownSeen {
e.Select(selectedFields...)
}
return nil
}
type evalPaginateArgs struct {
first, last *int
after, before *Cursor
opts []EvalPaginateOption
}
func newEvalPaginateArgs(rv map[string]any) *evalPaginateArgs {
args := &evalPaginateArgs{}
if rv == nil {
return args
}
if v := rv[firstField]; v != nil {
args.first = v.(*int)
}
if v := rv[lastField]; v != nil {
args.last = v.(*int)
}
if v := rv[afterField]; v != nil {
args.after = v.(*Cursor)
}
if v := rv[beforeField]; v != nil {
args.before = v.(*Cursor)
}
return args
}
const (
afterField = "after"
firstField = "first"
beforeField = "before"
lastField = "last"
orderByField = "orderBy"
directionField = "direction"
fieldField = "field"
whereField = "where"
)
func fieldArgs(ctx context.Context, whereInput any, path ...string) map[string]any {
field := collectedField(ctx, path...)
if field == nil || field.Arguments == nil {
return nil
}
oc := graphql.GetOperationContext(ctx)
args := field.ArgumentMap(oc.Variables)
return unmarshalArgs(ctx, whereInput, args)
}
// unmarshalArgs allows extracting the field arguments from their raw representation.
func unmarshalArgs(ctx context.Context, whereInput any, args map[string]any) map[string]any {
for _, k := range []string{firstField, lastField} {
v, ok := args[k]
if !ok {
continue
}
i, err := graphql.UnmarshalInt(v)
if err == nil {
args[k] = &i
}
}
for _, k := range []string{beforeField, afterField} {
v, ok := args[k]
if !ok {
continue
}
c := &Cursor{}
if c.UnmarshalGQL(v) == nil {
args[k] = c
}
}
if v, ok := args[whereField]; ok && whereInput != nil {
if err := graphql.UnmarshalInputFromContext(ctx, v, whereInput); err == nil {
args[whereField] = whereInput
}
}
return args
}
func limitRows(partitionBy string, limit int, orderBy ...sql.Querier) func(s *sql.Selector) {
return func(s *sql.Selector) {
d := sql.Dialect(s.Dialect())
s.SetDistinct(false)
with := d.With("src_query").
As(s.Clone()).
With("limited_query").
As(
d.Select("*").
AppendSelectExprAs(
sql.RowNumber().PartitionBy(partitionBy).OrderExpr(orderBy...),
"row_number",
).
From(d.Table("src_query")),
)
t := d.Table("limited_query").As(s.TableName())
*s = *d.Select(s.UnqualifiedColumns()...).
From(t).
Where(sql.LTE(t.C("row_number"), limit)).
Prefix(with)
}
}
// mayAddCondition appends another type condition to the satisfies list
// if condition is enabled (Node/Nodes) and it does not exist in the list.
func mayAddCondition(satisfies []string, typeCond string) []string {
if len(satisfies) == 0 {
return satisfies
}
for _, s := range satisfies {
if typeCond == s {
return satisfies
}
}
return append(satisfies, typeCond)
}