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