/
role.go
51 lines (42 loc) · 1021 Bytes
/
role.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
package loader
import (
"github.com/authgear/authgear-server/pkg/api/model"
"github.com/authgear/authgear-server/pkg/util/graphqlutil"
)
type RoleLoaderRoles interface {
GetManyRoles(ids []string) ([]*model.Role, error)
}
type RoleLoader struct {
*graphqlutil.DataLoader `wire:"-"`
Roles RoleLoaderRoles
}
func NewRoleLoader(roles RoleLoaderRoles) *RoleLoader {
l := &RoleLoader{
Roles: roles,
}
l.DataLoader = graphqlutil.NewDataLoader(l.LoadFunc)
return l
}
func (l *RoleLoader) LoadFunc(keys []interface{}) ([]interface{}, error) {
// Prepare IDs.
ids := make([]string, len(keys))
for i, key := range keys {
ids[i] = key.(string)
}
// Get entities.
entities, err := l.Roles.GetManyRoles(ids)
if err != nil {
return nil, err
}
// Create map.
entityMap := make(map[string]*model.Role)
for _, entity := range entities {
entityMap[entity.ID] = entity
}
out := make([]interface{}, len(keys))
for i, id := range ids {
entity := entityMap[id]
out[i] = entity
}
return out, nil
}