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