2018-07-29 19:11:33 +03:00
|
|
|
package resolvers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-08-13 19:32:11 +03:00
|
|
|
|
2018-07-29 19:11:33 +03:00
|
|
|
"github.com/MichaelMure/git-bug/bug"
|
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
|
|
|
)
|
|
|
|
|
2018-07-30 00:48:52 +03:00
|
|
|
type repoResolver struct{}
|
2018-07-29 19:11:33 +03:00
|
|
|
|
2018-08-01 20:24:19 +03:00
|
|
|
func (repoResolver) AllBugs(ctx context.Context, obj *models.Repository, after *string, before *string, first *int, last *int) (models.BugConnection, error) {
|
|
|
|
input := models.ConnectionInput{
|
|
|
|
Before: before,
|
|
|
|
After: after,
|
|
|
|
First: first,
|
|
|
|
Last: last,
|
|
|
|
}
|
2018-07-29 21:58:22 +03:00
|
|
|
|
|
|
|
// Simply pass a []string with the ids to the pagination algorithm
|
2018-07-30 00:48:52 +03:00
|
|
|
source, err := obj.Repo.AllBugIds()
|
2018-07-29 21:58:22 +03:00
|
|
|
|
|
|
|
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
|
2018-08-01 20:24:19 +03:00
|
|
|
conMaker := func(lazyBugEdges []connections.LazyBugEdge, lazyNode []string, info models.PageInfo, totalCount int) (models.BugConnection, error) {
|
2018-07-29 21:58:22 +03:00
|
|
|
edges := make([]models.BugEdge, len(lazyBugEdges))
|
2018-08-01 20:24:19 +03:00
|
|
|
nodes := make([]bug.Snapshot, len(lazyBugEdges))
|
2018-07-29 21:58:22 +03:00
|
|
|
|
|
|
|
for i, lazyBugEdge := range lazyBugEdges {
|
2018-07-30 00:48:52 +03:00
|
|
|
b, err := obj.Repo.ResolveBug(lazyBugEdge.Id)
|
2018-07-29 21:58:22 +03:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return models.BugConnection{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
snap := b.Snapshot()
|
|
|
|
|
|
|
|
edges[i] = models.BugEdge{
|
|
|
|
Cursor: lazyBugEdge.Cursor,
|
|
|
|
Node: *snap,
|
|
|
|
}
|
2018-08-01 20:24:19 +03:00
|
|
|
nodes[i] = *snap
|
2018-07-29 21:58:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return models.BugConnection{
|
|
|
|
Edges: edges,
|
2018-08-01 20:24:19 +03:00
|
|
|
Nodes: nodes,
|
2018-07-29 21:58:22 +03:00
|
|
|
PageInfo: info,
|
|
|
|
TotalCount: totalCount,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return connections.StringCon(source, edger, conMaker, input)
|
2018-07-29 19:11:33 +03:00
|
|
|
}
|
|
|
|
|
2018-07-30 00:48:52 +03:00
|
|
|
func (repoResolver) Bug(ctx context.Context, obj *models.Repository, prefix string) (*bug.Snapshot, error) {
|
|
|
|
b, err := obj.Repo.ResolveBugPrefix(prefix)
|
2018-07-29 19:11:33 +03:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.Snapshot(), nil
|
|
|
|
}
|