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