/
userfavorite.go
75 lines (65 loc) · 2.09 KB
/
userfavorite.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
package graphql
import (
"errors"
"github.com/target/goalert/assignment"
"github.com/target/goalert/permission"
g "github.com/graphql-go/graphql"
)
func (h *Handler) setUserFavoriteField() *g.Field {
return &g.Field{
Type: h.assignmentTarget,
Args: g.FieldConfigArgument{
"input": &g.ArgumentConfig{
Description: "The target to set as a favorite.",
Type: g.NewInputObject(g.InputObjectConfig{
Name: "SetFavoriteInput",
Fields: g.InputObjectConfigFieldMap{
"target_type": &g.InputObjectFieldConfig{Type: g.NewNonNull(assignmentTargetType)},
"target_id": &g.InputObjectFieldConfig{Type: g.NewNonNull(g.String)},
},
}),
},
},
Resolve: func(p g.ResolveParams) (interface{}, error) {
m, ok := p.Args["input"].(map[string]interface{})
if !ok {
return nil, errors.New("invalid input type")
}
var tgt RawTarget
tgt.Type, _ = m["target_type"].(assignment.TargetType)
tgt.ID, _ = m["target_id"].(string)
userID := permission.UserID(p.Context)
err := h.c.UserFavoriteStore.Set(p.Context, userID, tgt)
return newScrubber(p.Context).scrub(tgt, err)
},
}
}
func (h *Handler) unsetUserFavoriteField() *g.Field {
return &g.Field{
Type: h.assignmentTarget,
Args: g.FieldConfigArgument{
"input": &g.ArgumentConfig{
Description: "The target to unset as a favorite.",
Type: g.NewInputObject(g.InputObjectConfig{
Name: "UnsetFavoriteInput",
Fields: g.InputObjectConfigFieldMap{
"target_type": &g.InputObjectFieldConfig{Type: g.NewNonNull(assignmentTargetType)},
"target_id": &g.InputObjectFieldConfig{Type: g.NewNonNull(g.String)},
},
}),
},
},
Resolve: func(p g.ResolveParams) (interface{}, error) {
m, ok := p.Args["input"].(map[string]interface{})
if !ok {
return nil, errors.New("invalid input type")
}
var tgt RawTarget
tgt.Type, _ = m["target_type"].(assignment.TargetType)
tgt.ID, _ = m["target_id"].(string)
userID := permission.UserID(p.Context)
err := h.c.UserFavoriteStore.Unset(p.Context, userID, &tgt)
return newScrubber(p.Context).scrub(tgt, err)
},
}
}