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