1package resolvers
2
3import (
4 "context"
5
6 "github.com/git-bug/git-bug/api/graphql/connections"
7 "github.com/git-bug/git-bug/api/graphql/graph"
8 "github.com/git-bug/git-bug/api/graphql/models"
9 "github.com/git-bug/git-bug/cache"
10)
11
12var _ graph.QueryResolver = &rootQueryResolver{}
13
14type rootQueryResolver struct {
15 cache *cache.MultiRepoCache
16 authMode string
17 loginProviders []string
18}
19
20// ServerConfig returns static server configuration including the auth mode.
21// The frontend uses this to decide whether to show a login button, show "Read only",
22// or operate silently in single-user local mode.
23func (r rootQueryResolver) ServerConfig(_ context.Context) (*models.ServerConfig, error) {
24 providers := r.loginProviders
25 if providers == nil {
26 providers = []string{}
27 }
28 return &models.ServerConfig{
29 AuthMode: r.authMode,
30 LoginProviders: providers,
31 }, nil
32}
33
34func (r rootQueryResolver) Repository(_ context.Context, ref *string) (*models.Repository, error) {
35 var repo *cache.RepoCache
36 var err error
37
38 if ref == nil {
39 repo, err = r.cache.DefaultRepo()
40 } else {
41 repo, err = r.cache.ResolveRepo(*ref)
42 }
43
44 if err != nil {
45 return nil, err
46 }
47
48 return &models.Repository{
49 Cache: r.cache,
50 Repo: repo,
51 }, nil
52}
53
54// Repositories returns all registered repositories as a relay connection.
55// Used by the repo picker UI.
56func (r rootQueryResolver) Repositories(_ context.Context, after *string, before *string, first *int, last *int) (*models.RepositoryConnection, error) {
57 input := models.ConnectionInput{
58 After: after,
59 Before: before,
60 First: first,
61 Last: last,
62 }
63
64 source := r.cache.AllRepos()
65
66 edger := func(repo *cache.RepoCache, offset int) connections.Edge {
67 return models.RepositoryEdge{
68 Node: &models.Repository{Cache: r.cache, Repo: repo},
69 Cursor: connections.OffsetToCursor(offset),
70 }
71 }
72
73 // NodeType is *cache.RepoCache (the source slice element), but the connection
74 // nodes field wants []*models.Repository. Extract them from the edges, which
75 // already hold the wrapped Repository built by the edger above.
76 conMaker := func(edges []*models.RepositoryEdge, _ []*cache.RepoCache, info *models.PageInfo, totalCount int) (*models.RepositoryConnection, error) {
77 nodes := make([]*models.Repository, len(edges))
78 for i, e := range edges {
79 nodes[i] = e.Node
80 }
81 return &models.RepositoryConnection{
82 Edges: edges,
83 Nodes: nodes,
84 PageInfo: info,
85 TotalCount: totalCount,
86 }, nil
87 }
88
89 return connections.Connection(source, edger, conMaker, input)
90}