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/graph"
 10	"github.com/MichaelMure/git-bug/graphql/models"
 11	"github.com/MichaelMure/git-bug/identity"
 12)
 13
 14var _ graph.RepositoryResolver = &repoResolver{}
 15
 16type repoResolver struct{}
 17
 18func (repoResolver) AllBugs(ctx context.Context, obj *models.Repository, after *string, before *string, first *int, last *int, queryStr *string) (*models.BugConnection, error) {
 19	input := models.ConnectionInput{
 20		Before: before,
 21		After:  after,
 22		First:  first,
 23		Last:   last,
 24	}
 25
 26	var query *cache.Query
 27	if queryStr != nil {
 28		query2, err := cache.ParseQuery(*queryStr)
 29		if err != nil {
 30			return nil, err
 31		}
 32		query = query2
 33	} else {
 34		query = cache.NewQuery()
 35	}
 36
 37	// Simply pass a []string with the ids to the pagination algorithm
 38	source := obj.Repo.QueryBugs(query)
 39
 40	// The edger create a custom edge holding just the id
 41	edger := func(id string, offset int) connections.Edge {
 42		return connections.LazyBugEdge{
 43			Id:     id,
 44			Cursor: connections.OffsetToCursor(offset),
 45		}
 46	}
 47
 48	// The conMaker will finally load and compile bugs from git to replace the selected edges
 49	conMaker := func(lazyBugEdges []connections.LazyBugEdge, lazyNode []string, info models.PageInfo, totalCount int) (*models.BugConnection, error) {
 50		edges := make([]models.BugEdge, len(lazyBugEdges))
 51		nodes := make([]bug.Snapshot, len(lazyBugEdges))
 52
 53		for i, lazyBugEdge := range lazyBugEdges {
 54			b, err := obj.Repo.ResolveBug(lazyBugEdge.Id)
 55
 56			if err != nil {
 57				return nil, err
 58			}
 59
 60			snap := b.Snapshot()
 61
 62			edges[i] = models.BugEdge{
 63				Cursor: lazyBugEdge.Cursor,
 64				Node:   *snap,
 65			}
 66			nodes[i] = *snap
 67		}
 68
 69		return &models.BugConnection{
 70			Edges:      edges,
 71			Nodes:      nodes,
 72			PageInfo:   info,
 73			TotalCount: totalCount,
 74		}, nil
 75	}
 76
 77	return connections.LazyBugCon(source, edger, conMaker, input)
 78}
 79
 80func (repoResolver) Bug(ctx context.Context, obj *models.Repository, prefix string) (*bug.Snapshot, error) {
 81	b, err := obj.Repo.ResolveBugPrefix(prefix)
 82
 83	if err != nil {
 84		return nil, err
 85	}
 86
 87	return b.Snapshot(), nil
 88}
 89
 90func (repoResolver) AllIdentities(ctx context.Context, obj *models.Repository, after *string, before *string, first *int, last *int) (*models.IdentityConnection, error) {
 91	input := models.ConnectionInput{
 92		Before: before,
 93		After:  after,
 94		First:  first,
 95		Last:   last,
 96	}
 97
 98	// Simply pass a []string with the ids to the pagination algorithm
 99	source := obj.Repo.AllIdentityIds()
100
101	// The edger create a custom edge holding just the id
102	edger := func(id string, offset int) connections.Edge {
103		return connections.LazyIdentityEdge{
104			Id:     id,
105			Cursor: connections.OffsetToCursor(offset),
106		}
107	}
108
109	// The conMaker will finally load and compile identities from git to replace the selected edges
110	conMaker := func(lazyIdentityEdges []connections.LazyIdentityEdge, lazyNode []string, info models.PageInfo, totalCount int) (*models.IdentityConnection, error) {
111		edges := make([]models.IdentityEdge, len(lazyIdentityEdges))
112		nodes := make([]identity.Interface, len(lazyIdentityEdges))
113
114		for k, lazyIdentityEdge := range lazyIdentityEdges {
115			i, err := obj.Repo.ResolveIdentity(lazyIdentityEdge.Id)
116
117			if err != nil {
118				return nil, err
119			}
120
121			ii := identity.Interface(i.Identity)
122
123			edges[k] = models.IdentityEdge{
124				Cursor: lazyIdentityEdge.Cursor,
125				Node:   ii,
126			}
127			nodes[k] = ii
128		}
129
130		return &models.IdentityConnection{
131			Edges:      edges,
132			Nodes:      nodes,
133			PageInfo:   info,
134			TotalCount: totalCount,
135		}, nil
136	}
137
138	return connections.LazyIdentityCon(source, edger, conMaker, input)
139}
140
141func (repoResolver) Identity(ctx context.Context, obj *models.Repository, prefix string) (identity.Interface, error) {
142	i, err := obj.Repo.ResolveIdentityPrefix(prefix)
143
144	if err != nil {
145		return nil, err
146	}
147
148	return i.Identity, nil
149}
150
151func (repoResolver) UserIdentity(ctx context.Context, obj *models.Repository) (identity.Interface, error) {
152	i, err := obj.Repo.GetUserIdentity()
153
154	if err != nil {
155		return nil, err
156	}
157
158	return i.Identity, nil
159}