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, nil
46 }
47
48 return &models.Repository{
49 Repo: repo,
50 }, nil
51}
52
53// Repositories returns all registered repositories as a relay connection.
54// Used by the repo picker UI.
55func (r rootQueryResolver) Repositories(_ context.Context, after *string, before *string, first *int, last *int) (*models.RepositoryConnection, error) {
56 input := models.ConnectionInput{
57 After: after,
58 Before: before,
59 First: first,
60 Last: last,
61 }
62
63 source := r.cache.AllRepos()
64
65 edger := func(repo *cache.RepoCache, offset int) connections.Edge {
66 return models.RepositoryEdge{
67 Node: &models.Repository{Repo: repo},
68 Cursor: connections.OffsetToCursor(offset),
69 }
70 }
71
72 // NodeType is *cache.RepoCache (the source slice element), but the connection
73 // nodes field wants []*models.Repository. Extract them from the edges, which
74 // already hold the wrapped Repository built by the edger above.
75 conMaker := func(edges []*models.RepositoryEdge, _ []*cache.RepoCache, info *models.PageInfo, totalCount int) (*models.RepositoryConnection, error) {
76 nodes := make([]*models.Repository, len(edges))
77 for i, e := range edges {
78 nodes[i] = e.Node
79 }
80 return &models.RepositoryConnection{
81 Edges: edges,
82 Nodes: nodes,
83 PageInfo: info,
84 TotalCount: totalCount,
85 }, nil
86 }
87
88 return connections.Connection(source, edger, conMaker, input)
89}