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/config"
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/query"
13)
14
15var _ graph.RepositoryResolver = &repoResolver{}
16
17type repoResolver struct{ cfg config.Config }
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 (r repoResolver) UserIdentity(_ context.Context, obj *models.Repository) (models.IdentityWrapper, error) {
154 if r.cfg.ReadOnly {
155 return nil, nil
156 }
157 excerpt, err := obj.Repo.GetUserIdentityExcerpt()
158 if err != nil {
159 return nil, err
160 }
161
162 return models.NewLazyIdentity(obj.Repo, excerpt), nil
163}
164
165func (repoResolver) ValidLabels(_ context.Context, obj *models.Repository, after *string, before *string, first *int, last *int) (*models.LabelConnection, error) {
166 input := models.ConnectionInput{
167 Before: before,
168 After: after,
169 First: first,
170 Last: last,
171 }
172
173 edger := func(label bug.Label, offset int) connections.Edge {
174 return models.LabelEdge{
175 Node: label,
176 Cursor: connections.OffsetToCursor(offset),
177 }
178 }
179
180 conMaker := func(edges []*models.LabelEdge, nodes []bug.Label, info *models.PageInfo, totalCount int) (*models.LabelConnection, error) {
181 return &models.LabelConnection{
182 Edges: edges,
183 Nodes: nodes,
184 PageInfo: info,
185 TotalCount: totalCount,
186 }, nil
187 }
188
189 return connections.LabelCon(obj.Repo.ValidLabels(), edger, conMaker, input)
190}