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