repo.go

 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, input models.ConnectionInput) (models.BugConnection, error) {
13
14	// Simply pass a []string with the ids to the pagination algorithm
15	source, err := obj.Repo.AllBugIds()
16
17	if err != nil {
18		return models.BugConnection{}, err
19	}
20
21	// The edger create a custom edge holding just the id
22	edger := func(id string, offset int) connections.Edge {
23		return connections.LazyBugEdge{
24			Id:     id,
25			Cursor: connections.OffsetToCursor(offset),
26		}
27	}
28
29	// The conMaker will finally load and compile bugs from git to replace the selected edges
30	conMaker := func(lazyBugEdges []connections.LazyBugEdge, info models.PageInfo, totalCount int) (models.BugConnection, error) {
31		edges := make([]models.BugEdge, len(lazyBugEdges))
32
33		for i, lazyBugEdge := range lazyBugEdges {
34			b, err := obj.Repo.ResolveBug(lazyBugEdge.Id)
35
36			if err != nil {
37				return models.BugConnection{}, err
38			}
39
40			snap := b.Snapshot()
41
42			edges[i] = models.BugEdge{
43				Cursor: lazyBugEdge.Cursor,
44				Node:   *snap,
45			}
46		}
47
48		return models.BugConnection{
49			Edges:      edges,
50			PageInfo:   info,
51			TotalCount: totalCount,
52		}, nil
53	}
54
55	return connections.StringCon(source, edger, conMaker, input)
56}
57
58func (repoResolver) Bug(ctx context.Context, obj *models.Repository, prefix string) (*bug.Snapshot, error) {
59	b, err := obj.Repo.ResolveBugPrefix(prefix)
60
61	if err != nil {
62		return nil, err
63	}
64
65	return b.Snapshot(), nil
66}
67
68func (repoResolver) Mutation(ctx context.Context, obj *models.Repository) (models.RepositoryMutation, error) {
69	return models.RepositoryMutation{
70		Repo:  obj.Repo,
71		Cache: obj.Cache,
72	}, nil
73}