-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathquery_resolver.go
117 lines (103 loc) · 3.86 KB
/
query_resolver.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
package gql
import (
"context"
"errors"
"github.com/interline-io/transitland-lib/tl/tt"
"github.com/interline-io/transitland-mw/auth/authn"
"github.com/interline-io/transitland-mw/auth/authz"
"github.com/interline-io/transitland-server/model"
)
const MAX_RADIUS = 100_000
// query root
type queryResolver struct{ *Resolver }
func (r *queryResolver) Me(ctx context.Context) (*model.Me, error) {
cfg := model.ForContext(ctx)
me := model.Me{}
me.ExternalData = tt.NewMap(map[string]any{})
if whoami := cfg.Whoami; whoami != nil {
// Use whoami checker if available
cm, err := whoami.Me(ctx, &authz.MeRequest{})
if err != nil {
return nil, err
}
me.ID = cm.User.Id
me.Email = &cm.User.Email
me.Name = &cm.User.Name
me.Roles = cm.Roles
for k, v := range cm.ExternalData {
me.ExternalData.Val[k] = v
}
} else if user := authn.ForContext(ctx); user != nil {
// Fallback to user context
um := user.Email()
un := user.Name()
me.ID = user.ID()
me.Name = &un
me.Email = &um
me.Roles = user.Roles()
} else {
return nil, errors.New("no user")
}
return &me, nil
}
func (r *queryResolver) Agencies(ctx context.Context, limit *int, after *int, ids []int, where *model.AgencyFilter) ([]*model.Agency, error) {
addMetric(ctx, "agencies")
if where != nil {
if err := checkGeo(where.Near, where.Bbox); err != nil {
return nil, err
}
}
return model.ForContext(ctx).Finder.FindAgencies(ctx, checkLimit(limit), checkCursor(after), ids, where)
}
func (r *queryResolver) Routes(ctx context.Context, limit *int, after *int, ids []int, where *model.RouteFilter) ([]*model.Route, error) {
addMetric(ctx, "routes")
if where != nil {
if err := checkGeo(where.Near, where.Bbox); err != nil {
return nil, err
}
}
return model.ForContext(ctx).Finder.FindRoutes(ctx, checkLimit(limit), checkCursor(after), ids, where)
}
func (r *queryResolver) Stops(ctx context.Context, limit *int, after *int, ids []int, where *model.StopFilter) ([]*model.Stop, error) {
addMetric(ctx, "stops")
if where != nil {
if err := checkGeo(where.Near, where.Bbox); err != nil {
return nil, err
}
}
return model.ForContext(ctx).Finder.FindStops(ctx, checkLimit(limit), checkCursor(after), ids, where)
}
func (r *queryResolver) Trips(ctx context.Context, limit *int, after *int, ids []int, where *model.TripFilter) ([]*model.Trip, error) {
addMetric(ctx, "trips")
return model.ForContext(ctx).Finder.FindTrips(ctx, checkLimit(limit), checkCursor(after), ids, where)
}
func (r *queryResolver) FeedVersions(ctx context.Context, limit *int, after *int, ids []int, where *model.FeedVersionFilter) ([]*model.FeedVersion, error) {
addMetric(ctx, "feedVersions")
if where != nil {
if err := checkGeo(where.Near, where.Bbox); err != nil {
return nil, err
}
}
return model.ForContext(ctx).Finder.FindFeedVersions(ctx, checkLimit(limit), checkCursor(after), ids, where)
}
func (r *queryResolver) Feeds(ctx context.Context, limit *int, after *int, ids []int, where *model.FeedFilter) ([]*model.Feed, error) {
addMetric(ctx, "feeds")
if where != nil {
if err := checkGeo(where.Near, where.Bbox); err != nil {
return nil, err
}
}
return model.ForContext(ctx).Finder.FindFeeds(ctx, checkLimit(limit), checkCursor(after), ids, where)
}
func (r *queryResolver) Operators(ctx context.Context, limit *int, after *int, ids []int, where *model.OperatorFilter) ([]*model.Operator, error) {
addMetric(ctx, "operators")
if where != nil {
if err := checkGeo(where.Near, where.Bbox); err != nil {
return nil, err
}
}
return model.ForContext(ctx).Finder.FindOperators(ctx, checkLimit(limit), checkCursor(after), ids, where)
}
func (r *queryResolver) Places(ctx context.Context, limit *int, after *int, level *model.PlaceAggregationLevel, where *model.PlaceFilter) ([]*model.Place, error) {
return model.ForContext(ctx).Finder.FindPlaces(ctx, checkLimit(limit), checkCursor(after), nil, level, where)
}