aboutsummaryrefslogtreecommitdiffstats
path: root/graphql/resolvers/repo.go
blob: 2031d7b635966ed2ed1dc7c732a5e5cee73c1715 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package resolvers

import (
	"context"
	"github.com/MichaelMure/git-bug/bug"
	"github.com/MichaelMure/git-bug/cache"
	"github.com/MichaelMure/git-bug/graphql/connections"
	"github.com/MichaelMure/git-bug/graphql/models"
)

type repoResolver struct {
	cache cache.Cacher
	repo  cache.RepoCacher
}

func (repoResolver) AllBugs(ctx context.Context, obj *repoResolver, input models.ConnectionInput) (models.BugConnection, error) {

	// Simply pass a []string with the ids to the pagination algorithm
	source, err := obj.repo.AllBugIds()

	if err != nil {
		return models.BugConnection{}, err
	}

	// The edger create a custom edge holding just the id
	edger := func(id string, offset int) connections.Edge {
		return connections.LazyBugEdge{
			Id:     id,
			Cursor: connections.OffsetToCursor(offset),
		}
	}

	// The conMaker will finally load and compile bugs from git to replace the selected edges
	conMaker := func(lazyBugEdges []connections.LazyBugEdge, info models.PageInfo, totalCount int) (models.BugConnection, error) {
		edges := make([]models.BugEdge, len(lazyBugEdges))

		for i, lazyBugEdge := range lazyBugEdges {
			b, err := obj.repo.ResolveBug(lazyBugEdge.Id)

			if err != nil {
				return models.BugConnection{}, err
			}

			snap := b.Snapshot()

			edges[i] = models.BugEdge{
				Cursor: lazyBugEdge.Cursor,
				Node:   *snap,
			}
		}

		return models.BugConnection{
			Edges:      edges,
			PageInfo:   info,
			TotalCount: totalCount,
		}, nil
	}

	return connections.StringCon(source, edger, conMaker, input)
}

func (repoResolver) Bug(ctx context.Context, obj *repoResolver, prefix string) (*bug.Snapshot, error) {
	b, err := obj.repo.ResolveBugPrefix(prefix)

	if err != nil {
		return nil, err
	}

	return b.Snapshot(), nil
}