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