diff options
Diffstat (limited to 'graphql')
-rw-r--r-- | graphql/config/config.go | 7 | ||||
-rw-r--r-- | graphql/graphql_test.go | 3 | ||||
-rw-r--r-- | graphql/handler.go | 5 | ||||
-rw-r--r-- | graphql/resolvers/mutation.go | 24 | ||||
-rw-r--r-- | graphql/resolvers/repo.go | 8 | ||||
-rw-r--r-- | graphql/resolvers/root.go | 14 |
6 files changed, 52 insertions, 9 deletions
diff --git a/graphql/config/config.go b/graphql/config/config.go new file mode 100644 index 00000000..2e5f11b3 --- /dev/null +++ b/graphql/config/config.go @@ -0,0 +1,7 @@ +// Package config contains configuration for GraphQL stuff. +package config + +// Config holds configuration elements. +type Config struct { + ReadOnly bool +} diff --git a/graphql/graphql_test.go b/graphql/graphql_test.go index 0ff2c3fb..7c1ae66b 100644 --- a/graphql/graphql_test.go +++ b/graphql/graphql_test.go @@ -5,6 +5,7 @@ import ( "github.com/99designs/gqlgen/client" + "github.com/MichaelMure/git-bug/graphql/config" "github.com/MichaelMure/git-bug/graphql/models" "github.com/MichaelMure/git-bug/misc/random_bugs" "github.com/MichaelMure/git-bug/repository" @@ -16,7 +17,7 @@ func TestQueries(t *testing.T) { random_bugs.FillRepoWithSeed(repo, 10, 42) - handler, err := NewHandler(repo) + handler, err := NewHandler(repo, config.Config{}) if err != nil { t.Fatal(err) } diff --git a/graphql/handler.go b/graphql/handler.go index 55ef6fc4..a1be7352 100644 --- a/graphql/handler.go +++ b/graphql/handler.go @@ -8,6 +8,7 @@ import ( "github.com/99designs/gqlgen/graphql/handler" + "github.com/MichaelMure/git-bug/graphql/config" "github.com/MichaelMure/git-bug/graphql/graph" "github.com/MichaelMure/git-bug/graphql/resolvers" "github.com/MichaelMure/git-bug/repository" @@ -19,9 +20,9 @@ type Handler struct { *resolvers.RootResolver } -func NewHandler(repo repository.ClockedRepo) (Handler, error) { +func NewHandler(repo repository.ClockedRepo, cfg config.Config) (Handler, error) { h := Handler{ - RootResolver: resolvers.NewRootResolver(), + RootResolver: resolvers.NewRootResolver(cfg), } err := h.RootResolver.RegisterDefaultRepository(repo) diff --git a/graphql/resolvers/mutation.go b/graphql/resolvers/mutation.go index 850645f4..80d6fb1a 100644 --- a/graphql/resolvers/mutation.go +++ b/graphql/resolvers/mutation.go @@ -7,8 +7,32 @@ import ( "github.com/MichaelMure/git-bug/cache" "github.com/MichaelMure/git-bug/graphql/graph" "github.com/MichaelMure/git-bug/graphql/models" + "github.com/vektah/gqlparser/gqlerror" ) +var _ graph.MutationResolver = &readonlyMutationResolver{} + +type readonlyMutationResolver struct{} + +func (readonlyMutationResolver) NewBug(_ context.Context, _ models.NewBugInput) (*models.NewBugPayload, error) { + return nil, gqlerror.Errorf("readonly mode") +} +func (readonlyMutationResolver) AddComment(_ context.Context, input models.AddCommentInput) (*models.AddCommentPayload, error) { + return nil, gqlerror.Errorf("readonly mode") +} +func (readonlyMutationResolver) ChangeLabels(_ context.Context, input *models.ChangeLabelInput) (*models.ChangeLabelPayload, error) { + return nil, gqlerror.Errorf("readonly mode") +} +func (readonlyMutationResolver) OpenBug(_ context.Context, input models.OpenBugInput) (*models.OpenBugPayload, error) { + return nil, gqlerror.Errorf("readonly mode") +} +func (readonlyMutationResolver) CloseBug(_ context.Context, input models.CloseBugInput) (*models.CloseBugPayload, error) { + return nil, gqlerror.Errorf("readonly mode") +} +func (readonlyMutationResolver) SetTitle(_ context.Context, input models.SetTitleInput) (*models.SetTitlePayload, error) { + return nil, gqlerror.Errorf("readonly mode") +} + var _ graph.MutationResolver = &mutationResolver{} type mutationResolver struct { diff --git a/graphql/resolvers/repo.go b/graphql/resolvers/repo.go index 639e8f90..e30b49f0 100644 --- a/graphql/resolvers/repo.go +++ b/graphql/resolvers/repo.go @@ -5,6 +5,7 @@ import ( "github.com/MichaelMure/git-bug/bug" "github.com/MichaelMure/git-bug/entity" + "github.com/MichaelMure/git-bug/graphql/config" "github.com/MichaelMure/git-bug/graphql/connections" "github.com/MichaelMure/git-bug/graphql/graph" "github.com/MichaelMure/git-bug/graphql/models" @@ -13,7 +14,7 @@ import ( var _ graph.RepositoryResolver = &repoResolver{} -type repoResolver struct{} +type repoResolver struct{ cfg config.Config } func (repoResolver) Name(_ context.Context, obj *models.Repository) (*string, error) { name := obj.Repo.Name() @@ -149,7 +150,10 @@ func (repoResolver) Identity(_ context.Context, obj *models.Repository, prefix s return models.NewLazyIdentity(obj.Repo, excerpt), nil } -func (repoResolver) UserIdentity(_ context.Context, obj *models.Repository) (models.IdentityWrapper, error) { +func (r repoResolver) UserIdentity(_ context.Context, obj *models.Repository) (models.IdentityWrapper, error) { + if r.cfg.ReadOnly { + return nil, nil + } excerpt, err := obj.Repo.GetUserIdentityExcerpt() if err != nil { return nil, err diff --git a/graphql/resolvers/root.go b/graphql/resolvers/root.go index 9973ff59..214bbae3 100644 --- a/graphql/resolvers/root.go +++ b/graphql/resolvers/root.go @@ -3,6 +3,7 @@ package resolvers import ( "github.com/MichaelMure/git-bug/cache" + "github.com/MichaelMure/git-bug/graphql/config" "github.com/MichaelMure/git-bug/graphql/graph" ) @@ -10,11 +11,13 @@ var _ graph.ResolverRoot = &RootResolver{} type RootResolver struct { cache.MultiRepoCache + cfg config.Config } -func NewRootResolver() *RootResolver { +func NewRootResolver(cfg config.Config) *RootResolver { return &RootResolver{ MultiRepoCache: cache.NewMultiRepoCache(), + cfg: cfg, } } @@ -25,13 +28,16 @@ func (r RootResolver) Query() graph.QueryResolver { } func (r RootResolver) Mutation() graph.MutationResolver { + if r.cfg.ReadOnly { + return &readonlyMutationResolver{} + } return &mutationResolver{ cache: &r.MultiRepoCache, } } -func (RootResolver) Repository() graph.RepositoryResolver { - return &repoResolver{} +func (r RootResolver) Repository() graph.RepositoryResolver { + return &repoResolver{r.cfg} } func (RootResolver) Bug() graph.BugResolver { @@ -50,7 +56,7 @@ func (RootResolver) Label() graph.LabelResolver { return &labelResolver{} } -func (r RootResolver) Identity() graph.IdentityResolver { +func (RootResolver) Identity() graph.IdentityResolver { return &identityResolver{} } |