repo.go

 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 := obj.Repo.AllBugOrderById()
23
24	// The edger create a custom edge holding just the id
25	edger := func(id string, offset int) connections.Edge {
26		return connections.LazyBugEdge{
27			Id:     id,
28			Cursor: connections.OffsetToCursor(offset),
29		}
30	}
31
32	// The conMaker will finally load and compile bugs from git to replace the selected edges
33	conMaker := func(lazyBugEdges []connections.LazyBugEdge, lazyNode []string, info models.PageInfo, totalCount int) (models.BugConnection, error) {
34		edges := make([]models.BugEdge, len(lazyBugEdges))
35		nodes := make([]bug.Snapshot, 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			nodes[i] = *snap
51		}
52
53		return models.BugConnection{
54			Edges:      edges,
55			Nodes:      nodes,
56			PageInfo:   info,
57			TotalCount: totalCount,
58		}, nil
59	}
60
61	return connections.StringCon(source, edger, conMaker, input)
62}
63
64func (repoResolver) Bug(ctx context.Context, obj *models.Repository, prefix string) (*bug.Snapshot, error) {
65	b, err := obj.Repo.ResolveBugPrefix(prefix)
66
67	if err != nil {
68		return nil, err
69	}
70
71	return b.Snapshot(), nil
72}