1package resolvers
2
3import (
4 "context"
5 "github.com/MichaelMure/git-bug/bug"
6 "github.com/MichaelMure/git-bug/cache"
7 "github.com/MichaelMure/git-bug/graphql/connections"
8 "github.com/MichaelMure/git-bug/graphql/models"
9)
10
11type repoResolver struct {
12 cache cache.Cacher
13 repo cache.RepoCacher
14}
15
16func (repoResolver) AllBugs(ctx context.Context, obj *repoResolver, input models.ConnectionInput) (models.BugConnection, error) {
17
18 // Simply pass a []string with the ids to the pagination algorithm
19 source, err := obj.repo.AllBugIds()
20
21 if err != nil {
22 return models.BugConnection{}, err
23 }
24
25 // The edger create a custom edge holding just the id
26 edger := func(id string, offset int) connections.Edge {
27 return connections.LazyBugEdge{
28 Id: id,
29 Cursor: connections.OffsetToCursor(offset),
30 }
31 }
32
33 // The conMaker will finally load and compile bugs from git to replace the selected edges
34 conMaker := func(lazyBugEdges []connections.LazyBugEdge, info models.PageInfo, totalCount int) (models.BugConnection, error) {
35 edges := make([]models.BugEdge, len(lazyBugEdges))
36
37 for i, lazyBugEdge := range lazyBugEdges {
38 b, err := obj.repo.ResolveBug(lazyBugEdge.Id)
39
40 if err != nil {
41 return models.BugConnection{}, err
42 }
43
44 snap := b.Snapshot()
45
46 edges[i] = models.BugEdge{
47 Cursor: lazyBugEdge.Cursor,
48 Node: *snap,
49 }
50 }
51
52 return models.BugConnection{
53 Edges: edges,
54 PageInfo: info,
55 TotalCount: totalCount,
56 }, nil
57 }
58
59 return connections.StringCon(source, edger, conMaker, input)
60}
61
62func (repoResolver) Bug(ctx context.Context, obj *repoResolver, prefix string) (*bug.Snapshot, error) {
63 b, err := obj.repo.ResolveBugPrefix(prefix)
64
65 if err != nil {
66 return nil, err
67 }
68
69 return b.Snapshot(), nil
70}