aboutsummaryrefslogtreecommitdiffstats
path: root/graphql/resolvers/repo.go
blob: 68a3ce0a64c18b285cab3149d1b3b01091bc1296 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package resolvers

import (
	"context"

	"github.com/MichaelMure/git-bug/bug"
	"github.com/MichaelMure/git-bug/cache"
	"github.com/MichaelMure/git-bug/graphql/connections"
	"github.com/MichaelMure/git-bug/graphql/graph"
	"github.com/MichaelMure/git-bug/graphql/models"
	"github.com/MichaelMure/git-bug/identity"
)

var _ graph.RepositoryResolver = &repoResolver{}

type repoResolver struct{}

func (repoResolver) AllBugs(ctx context.Context, obj *models.Repository, after *string, before *string, first *int, last *int, queryStr *string) (*models.BugConnection, error) {
	input := models.ConnectionInput{
		Before: before,
		After:  after,
		First:  first,
		Last:   last,
	}

	var query *cache.Query
	if queryStr != nil {
		query2, err := cache.ParseQuery(*queryStr)
		if err != nil {
			return nil, err
		}
		query = query2
	} else {
		query = cache.NewQuery()
	}

	// Simply pass a []string with the ids to the pagination algorithm
	source := obj.Repo.QueryBugs(query)

	// The edger create a custom edge holding just the id
	edger := func(id string, offset int) connections.Edge {
		return connections.LazyBugEdge{
			Id:     id,
			Cursor: connections.OffsetToCursor(offset),
		}
	}

	// The conMaker will finally load and compile bugs from git to replace the selected edges
	conMaker := func(lazyBugEdges []*connections.LazyBugEdge, lazyNode []string, info *models.PageInfo, totalCount int) (*models.BugConnection, error) {
		edges := make([]*models.BugEdge, len(lazyBugEdges))
		nodes := make([]*bug.Snapshot, len(lazyBugEdges))

		for i, lazyBugEdge := range lazyBugEdges {
			b, err := obj.Repo.ResolveBug(lazyBugEdge.Id)

			if err != nil {
				return nil, err
			}

			snap := b.Snapshot()

			edges[i] = &models.BugEdge{
				Cursor: lazyBugEdge.Cursor,
				Node:   snap,
			}
			nodes[i] = snap
		}

		return &models.BugConnection{
			Edges:      edges,
			Nodes:      nodes,
			PageInfo:   info,
			TotalCount: totalCount,
		}, nil
	}

	return connections.LazyBugCon(source, edger, conMaker, input)
}

func (repoResolver) Bug(ctx context.Context, obj *models.Repository, prefix string) (*bug.Snapshot, error) {
	b, err := obj.Repo.ResolveBugPrefix(prefix)

	if err != nil {
		return nil, err
	}

	return b.Snapshot(), nil
}

func (repoResolver) AllIdentities(ctx context.Context, obj *models.Repository, after *string, before *string, first *int, last *int) (*models.IdentityConnection, error) {
	input := models.ConnectionInput{
		Before: before,
		After:  after,
		First:  first,
		Last:   last,
	}

	// Simply pass a []string with the ids to the pagination algorithm
	source := obj.Repo.AllIdentityIds()

	// The edger create a custom edge holding just the id
	edger := func(id string, offset int) connections.Edge {
		return connections.LazyIdentityEdge{
			Id:     id,
			Cursor: connections.OffsetToCursor(offset),
		}
	}

	// The conMaker will finally load and compile identities from git to replace the selected edges
	conMaker := func(lazyIdentityEdges []*connections.LazyIdentityEdge, lazyNode []string, info *models.PageInfo, totalCount int) (*models.IdentityConnection, error) {
		edges := make([]*models.IdentityEdge, len(lazyIdentityEdges))
		nodes := make([]identity.Interface, len(lazyIdentityEdges))

		for k, lazyIdentityEdge := range lazyIdentityEdges {
			i, err := obj.Repo.ResolveIdentity(lazyIdentityEdge.Id)

			if err != nil {
				return nil, err
			}

			ii := identity.Interface(i.Identity)

			edges[k] = &models.IdentityEdge{
				Cursor: lazyIdentityEdge.Cursor,
				Node:   i.Identity,
			}
			nodes[k] = ii
		}

		return &models.IdentityConnection{
			Edges:      edges,
			Nodes:      nodes,
			PageInfo:   info,
			TotalCount: totalCount,
		}, nil
	}

	return connections.LazyIdentityCon(source, edger, conMaker, input)
}

func (repoResolver) Identity(ctx context.Context, obj *models.Repository, prefix string) (identity.Interface, error) {
	i, err := obj.Repo.ResolveIdentityPrefix(prefix)

	if err != nil {
		return nil, err
	}

	return i.Identity, nil
}

func (repoResolver) UserIdentity(ctx context.Context, obj *models.Repository) (identity.Interface, error) {
	i, err := obj.Repo.GetUserIdentity()

	if err != nil {
		return nil, err
	}

	return i.Identity, nil
}

func (repoResolver) ValidLabels(ctx context.Context, obj *models.Repository) ([]bug.Label, error) {
	return obj.Repo.ValidLabels(), nil
}