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