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