-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
resolver.go
84 lines (66 loc) · 1.68 KB
/
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
package graphql
import (
"context"
"fmt"
"github.com/99designs/gqlgen/graphql"
)
type key int8
const (
userCtxKey key = 0
)
// GetUserFromContext finds the user from the context. This is usually inserted
// by WithUser.
func GetUserFromContext(ctx context.Context) *User {
u, ok := ctx.Value(userCtxKey).(*User)
if !ok {
return nil
}
return u
}
// ParseLimit turns a limit and applies defaults into a pair of ints.
func ParseLimit(lim *Limit, defaultLimit, defaultOffset int) (int, int) {
limit := defaultLimit
offset := defaultOffset
if lim != nil {
i := *lim
if i.Limit != nil {
limit = *i.Limit
}
if i.Offset != nil {
offset = *i.Offset
}
}
return limit, offset
}
// WithUser puts a user in the context.
func WithUser(ctx context.Context, u *User) context.Context {
return context.WithValue(ctx, userCtxKey, u)
}
// Resolver is the type that gqlgen expects to exist
type Resolver struct{}
// New returns a Config that has all of the proper settings for this graphql
// server.
func New() Config {
c := Config{
Resolvers: &Resolver{},
}
c.Directives.HasRole = func(ctx context.Context, _ interface{}, next graphql.Resolver, role Role) (interface{}, error) {
u := GetUserFromContext(ctx)
if u == nil || Role(u.Role) != role {
// block calling the next resolver
return nil, fmt.Errorf("forbidden")
}
// or let it pass through
return next(ctx)
}
c.Directives.LoggedIn = func(ctx context.Context, _ interface{}, next graphql.Resolver) (interface{}, error) {
u := GetUserFromContext(ctx)
if u == nil {
// block calling the next resolver
return nil, fmt.Errorf("forbidden")
}
// or let it pass through
return next(ctx)
}
return c
}