1package resolvers
2
3import (
4 "context"
5
6 "github.com/MichaelMure/git-bug/api/graphql/connections"
7 "github.com/MichaelMure/git-bug/api/graphql/graph"
8 "github.com/MichaelMure/git-bug/api/graphql/models"
9 "github.com/MichaelMure/git-bug/entities/bug"
10 "github.com/MichaelMure/git-bug/entity/dag"
11)
12
13var _ graph.BugResolver = &bugResolver{}
14
15type bugResolver struct{}
16
17func (bugResolver) ID(_ context.Context, obj models.BugWrapper) (string, error) {
18 return obj.Id().String(), nil
19}
20
21func (bugResolver) HumanID(_ context.Context, obj models.BugWrapper) (string, error) {
22 return obj.Id().Human(), nil
23}
24
25func (bugResolver) Comments(_ context.Context, obj models.BugWrapper, after *string, before *string, first *int, last *int) (*models.CommentConnection, error) {
26 input := models.ConnectionInput{
27 Before: before,
28 After: after,
29 First: first,
30 Last: last,
31 }
32
33 edger := func(comment bug.Comment, offset int) connections.Edge {
34 return models.CommentEdge{
35 Node: &comment,
36 Cursor: connections.OffsetToCursor(offset),
37 }
38 }
39
40 conMaker := func(edges []*models.CommentEdge, nodes []bug.Comment, info *models.PageInfo, totalCount int) (*models.CommentConnection, error) {
41 var commentNodes []*bug.Comment
42 for _, c := range nodes {
43 commentNodes = append(commentNodes, &c)
44 }
45 return &models.CommentConnection{
46 Edges: edges,
47 Nodes: commentNodes,
48 PageInfo: info,
49 TotalCount: totalCount,
50 }, nil
51 }
52
53 comments, err := obj.Comments()
54 if err != nil {
55 return nil, err
56 }
57
58 return connections.CommentCon(comments, edger, conMaker, input)
59}
60
61func (bugResolver) Operations(_ context.Context, obj models.BugWrapper, after *string, before *string, first *int, last *int) (*models.OperationConnection, error) {
62 input := models.ConnectionInput{
63 Before: before,
64 After: after,
65 First: first,
66 Last: last,
67 }
68
69 edger := func(op dag.Operation, offset int) connections.Edge {
70 return models.OperationEdge{
71 Node: op,
72 Cursor: connections.OffsetToCursor(offset),
73 }
74 }
75
76 conMaker := func(edges []*models.OperationEdge, nodes []dag.Operation, info *models.PageInfo, totalCount int) (*models.OperationConnection, error) {
77 return &models.OperationConnection{
78 Edges: edges,
79 Nodes: nodes,
80 PageInfo: info,
81 TotalCount: totalCount,
82 }, nil
83 }
84
85 ops, err := obj.Operations()
86 if err != nil {
87 return nil, err
88 }
89
90 return connections.OperationCon(ops, edger, conMaker, input)
91}
92
93func (bugResolver) Timeline(_ context.Context, obj models.BugWrapper, after *string, before *string, first *int, last *int) (*models.TimelineItemConnection, error) {
94 input := models.ConnectionInput{
95 Before: before,
96 After: after,
97 First: first,
98 Last: last,
99 }
100
101 edger := func(op bug.TimelineItem, offset int) connections.Edge {
102 return models.TimelineItemEdge{
103 Node: op,
104 Cursor: connections.OffsetToCursor(offset),
105 }
106 }
107
108 conMaker := func(edges []*models.TimelineItemEdge, nodes []bug.TimelineItem, info *models.PageInfo, totalCount int) (*models.TimelineItemConnection, error) {
109 return &models.TimelineItemConnection{
110 Edges: edges,
111 Nodes: nodes,
112 PageInfo: info,
113 TotalCount: totalCount,
114 }, nil
115 }
116
117 timeline, err := obj.Timeline()
118 if err != nil {
119 return nil, err
120 }
121
122 return connections.TimelineItemCon(timeline, edger, conMaker, input)
123}
124
125func (bugResolver) Actors(_ context.Context, obj models.BugWrapper, after *string, before *string, first *int, last *int) (*models.IdentityConnection, error) {
126 input := models.ConnectionInput{
127 Before: before,
128 After: after,
129 First: first,
130 Last: last,
131 }
132
133 edger := func(actor models.IdentityWrapper, offset int) connections.Edge {
134 return models.IdentityEdge{
135 Node: actor,
136 Cursor: connections.OffsetToCursor(offset),
137 }
138 }
139
140 conMaker := func(edges []*models.IdentityEdge, nodes []models.IdentityWrapper, info *models.PageInfo, totalCount int) (*models.IdentityConnection, error) {
141 return &models.IdentityConnection{
142 Edges: edges,
143 Nodes: nodes,
144 PageInfo: info,
145 TotalCount: totalCount,
146 }, nil
147 }
148
149 actors, err := obj.Actors()
150 if err != nil {
151 return nil, err
152 }
153
154 return connections.IdentityCon(actors, edger, conMaker, input)
155}
156
157func (bugResolver) Participants(_ context.Context, obj models.BugWrapper, after *string, before *string, first *int, last *int) (*models.IdentityConnection, error) {
158 input := models.ConnectionInput{
159 Before: before,
160 After: after,
161 First: first,
162 Last: last,
163 }
164
165 edger := func(participant models.IdentityWrapper, offset int) connections.Edge {
166 return models.IdentityEdge{
167 Node: participant,
168 Cursor: connections.OffsetToCursor(offset),
169 }
170 }
171
172 conMaker := func(edges []*models.IdentityEdge, nodes []models.IdentityWrapper, info *models.PageInfo, totalCount int) (*models.IdentityConnection, error) {
173 return &models.IdentityConnection{
174 Edges: edges,
175 Nodes: nodes,
176 PageInfo: info,
177 TotalCount: totalCount,
178 }, nil
179 }
180
181 participants, err := obj.Participants()
182 if err != nil {
183 return nil, err
184 }
185
186 return connections.IdentityCon(participants, edger, conMaker, input)
187}