diff options
author | Michael Muré <batolettre@gmail.com> | 2018-07-29 20:58:22 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2018-07-29 20:58:22 +0200 |
commit | c58aa18a2d0683b0a1e6f0597724e67b500503a0 (patch) | |
tree | 06b7611ebce2a4cbe0c99f72729517cb8114e3f1 /graphql/resolvers/repo.go | |
parent | 08f03ecf3cbbc350585acf0492966681ec449a94 (diff) | |
download | git-bug-c58aa18a2d0683b0a1e6f0597724e67b500503a0.tar.gz |
graphql: lazy loading for the bug relay connection
Diffstat (limited to 'graphql/resolvers/repo.go')
-rw-r--r-- | graphql/resolvers/repo.go | 45 |
1 files changed, 44 insertions, 1 deletions
diff --git a/graphql/resolvers/repo.go b/graphql/resolvers/repo.go index d8190f15..2031d7b6 100644 --- a/graphql/resolvers/repo.go +++ b/graphql/resolvers/repo.go @@ -4,6 +4,7 @@ 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" ) @@ -13,7 +14,49 @@ type repoResolver struct { } func (repoResolver) AllBugs(ctx context.Context, obj *repoResolver, input models.ConnectionInput) (models.BugConnection, error) { - panic("implement me") + + // 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) { |