2018-07-29 19:11:33 +03:00
|
|
|
package resolvers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"github.com/MichaelMure/git-bug/bug"
|
|
|
|
"github.com/MichaelMure/git-bug/cache"
|
2018-07-29 21:58:22 +03:00
|
|
|
"github.com/MichaelMure/git-bug/graphql/connections"
|
2018-07-29 20:37:06 +03:00
|
|
|
"github.com/MichaelMure/git-bug/graphql/models"
|
2018-07-29 19:11:33 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type repoResolver struct {
|
|
|
|
cache cache.Cacher
|
|
|
|
repo cache.RepoCacher
|
|
|
|
}
|
|
|
|
|
2018-07-29 20:37:06 +03:00
|
|
|
func (repoResolver) AllBugs(ctx context.Context, obj *repoResolver, input models.ConnectionInput) (models.BugConnection, error) {
|
2018-07-29 21:58:22 +03:00
|
|
|
|
|
|
|
// 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)
|
2018-07-29 19:11:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|