/
gql_collection.go
210 lines (194 loc) · 5.28 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
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
// Code generated by ent, DO NOT EDIT.
package ent
import (
"context"
"entgo.io/ent/dialect/sql"
"github.com/99designs/gqlgen/graphql"
)
// CollectFields tells the query-builder to eagerly load connected nodes by resolver context.
func (b *BenchQuery) CollectFields(ctx context.Context, satisfies ...string) (*BenchQuery, error) {
fc := graphql.GetFieldContext(ctx)
if fc == nil {
return b, nil
}
if err := b.collectField(ctx, graphql.GetOperationContext(ctx), fc.Field, nil, satisfies...); err != nil {
return nil, err
}
return b, nil
}
func (b *BenchQuery) collectField(ctx context.Context, opCtx *graphql.OperationContext, field graphql.CollectedField, path []string, satisfies ...string) error {
path = append([]string(nil), path...)
for _, field := range graphql.CollectFields(opCtx, field.Selections, satisfies) {
switch field.Name {
case "benchResult":
var (
alias = field.Alias
path = append(path, alias)
query = (&BenchResultClient{config: b.config}).Query()
)
if err := query.collectField(ctx, opCtx, field, path, satisfies...); err != nil {
return err
}
b.WithNamedBenchResult(alias, func(wq *BenchResultQuery) {
*wq = *query
})
}
}
return nil
}
type benchPaginateArgs struct {
first, last *int
after, before *Cursor
opts []BenchPaginateOption
}
func newBenchPaginateArgs(rv map[string]interface{}) *benchPaginateArgs {
args := &benchPaginateArgs{}
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)
}
if v, ok := rv[whereField].(*BenchWhereInput); ok {
args.opts = append(args.opts, WithBenchFilter(v.Filter))
}
return args
}
// CollectFields tells the query-builder to eagerly load connected nodes by resolver context.
func (br *BenchResultQuery) CollectFields(ctx context.Context, satisfies ...string) (*BenchResultQuery, error) {
fc := graphql.GetFieldContext(ctx)
if fc == nil {
return br, nil
}
if err := br.collectField(ctx, graphql.GetOperationContext(ctx), fc.Field, nil, satisfies...); err != nil {
return nil, err
}
return br, nil
}
func (br *BenchResultQuery) collectField(ctx context.Context, opCtx *graphql.OperationContext, field graphql.CollectedField, path []string, satisfies ...string) error {
path = append([]string(nil), path...)
return nil
}
type benchresultPaginateArgs struct {
first, last *int
after, before *Cursor
opts []BenchResultPaginateOption
}
func newBenchResultPaginateArgs(rv map[string]interface{}) *benchresultPaginateArgs {
args := &benchresultPaginateArgs{}
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)
}
if v, ok := rv[whereField].(*BenchResultWhereInput); ok {
args.opts = append(args.opts, WithBenchResultFilter(v.Filter))
}
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 interface{}, path ...string) map[string]interface{} {
fc := graphql.GetFieldContext(ctx)
if fc == nil {
return nil
}
oc := graphql.GetOperationContext(ctx)
for _, name := range path {
var field *graphql.CollectedField
for _, f := range graphql.CollectFields(oc, fc.Field.Selections, nil) {
if f.Alias == name {
field = &f
break
}
}
if field == nil {
return nil
}
cf, err := fc.Child(ctx, *field)
if err != nil {
args := field.ArgumentMap(oc.Variables)
return unmarshalArgs(ctx, whereInput, args)
}
fc = cf
}
return fc.Args
}
// unmarshalArgs allows extracting the field arguments from their raw representation.
func unmarshalArgs(ctx context.Context, whereInput interface{}, args map[string]interface{}) map[string]interface{} {
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)
}
}