repo.go

 1package resolvers
 2
 3import (
 4	"context"
 5
 6	"github.com/MichaelMure/git-bug/bug"
 7	"github.com/MichaelMure/git-bug/cache"
 8	"github.com/MichaelMure/git-bug/graphql/connections"
 9	"github.com/MichaelMure/git-bug/graphql/models"
10)
11
12type repoResolver struct{}
13
14func (repoResolver) AllBugs(ctx context.Context, obj *models.Repository, after *string, before *string, first *int, last *int) (models.BugConnection, error) {
15	input := models.ConnectionInput{
16		Before: before,
17		After:  after,
18		First:  first,
19		Last:   last,
20	}
21
22	// Simply pass a []string with the ids to the pagination algorithm
23	source := obj.Repo.AllBugsId(cache.OrderByCreation, cache.OrderAscending)
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, lazyNode []string, info models.PageInfo, totalCount int) (models.BugConnection, error) {
35		edges := make([]models.BugEdge, len(lazyBugEdges))
36		nodes := make([]bug.Snapshot, len(lazyBugEdges))
37
38		for i, lazyBugEdge := range lazyBugEdges {
39			b, err := obj.Repo.ResolveBug(lazyBugEdge.Id)
40
41			if err != nil {
42				return models.BugConnection{}, err
43			}
44
45			snap := b.Snapshot()
46
47			edges[i] = models.BugEdge{
48				Cursor: lazyBugEdge.Cursor,
49				Node:   *snap,
50			}
51			nodes[i] = *snap
52		}
53
54		return models.BugConnection{
55			Edges:      edges,
56			Nodes:      nodes,
57			PageInfo:   info,
58			TotalCount: totalCount,
59		}, nil
60	}
61
62	return connections.StringCon(source, edger, conMaker, input)
63}
64
65func (repoResolver) Bug(ctx context.Context, obj *models.Repository, prefix string) (*bug.Snapshot, error) {
66	b, err := obj.Repo.ResolveBugPrefix(prefix)
67
68	if err != nil {
69		return nil, err
70	}
71
72	return b.Snapshot(), nil
73}