diff options
Diffstat (limited to 'graphql/resolvers')
-rw-r--r-- | graphql/resolvers/errors.go | 6 | ||||
-rw-r--r-- | graphql/resolvers/mutation.go | 59 | ||||
-rw-r--r-- | graphql/resolvers/repo.go | 10 |
3 files changed, 43 insertions, 32 deletions
diff --git a/graphql/resolvers/errors.go b/graphql/resolvers/errors.go new file mode 100644 index 00000000..2a8024e0 --- /dev/null +++ b/graphql/resolvers/errors.go @@ -0,0 +1,6 @@ +package resolvers + +import "errors" + +// ErrNotAuthenticated is returned to the client if the user requests an action requiring authentication, and they are not authenticated. +var ErrNotAuthenticated = errors.New("not authenticated or read-only") diff --git a/graphql/resolvers/mutation.go b/graphql/resolvers/mutation.go index 62b92aaa..8d2d8081 100644 --- a/graphql/resolvers/mutation.go +++ b/graphql/resolvers/mutation.go @@ -2,18 +2,15 @@ package resolvers import ( "context" - "errors" "time" "github.com/MichaelMure/git-bug/bug" "github.com/MichaelMure/git-bug/cache" "github.com/MichaelMure/git-bug/graphql/graph" + "github.com/MichaelMure/git-bug/graphql/graphqlidentity" "github.com/MichaelMure/git-bug/graphql/models" - "github.com/MichaelMure/git-bug/identity" ) -var ErrNotAuthenticated = errors.New("not authenticated or read-only") - var _ graph.MutationResolver = &mutationResolver{} type mutationResolver struct { @@ -47,13 +44,14 @@ func (r mutationResolver) NewBug(ctx context.Context, input models.NewBugInput) return nil, err } - id := identity.ForContext(ctx, repo) - if id == nil { + id, err := graphqlidentity.ForContext(ctx, repo) + if err != nil { + return nil, err + } else if id == nil { return nil, ErrNotAuthenticated } - author := cache.NewIdentityCache(repo, id) - b, op, err := repo.NewBugRaw(author, time.Now().Unix(), input.Title, input.Message, input.Files, nil) + b, op, err := repo.NewBugRaw(id, time.Now().Unix(), input.Title, input.Message, input.Files, nil) if err != nil { return nil, err } @@ -71,13 +69,14 @@ func (r mutationResolver) AddComment(ctx context.Context, input models.AddCommen return nil, err } - id := identity.ForContext(ctx, repo) - if id == nil { + id, err := graphqlidentity.ForContext(ctx, repo) + if err != nil { + return nil, err + } else if id == nil { return nil, ErrNotAuthenticated } - author := cache.NewIdentityCache(repo, id) - op, err := b.AddCommentRaw(author, time.Now().Unix(), input.Message, input.Files, nil) + op, err := b.AddCommentRaw(id, time.Now().Unix(), input.Message, input.Files, nil) if err != nil { return nil, err } @@ -100,13 +99,14 @@ func (r mutationResolver) ChangeLabels(ctx context.Context, input *models.Change return nil, err } - id := identity.ForContext(ctx, repo) - if id == nil { + id, err := graphqlidentity.ForContext(ctx, repo) + if err != nil { + return nil, err + } else if id == nil { return nil, ErrNotAuthenticated } - author := cache.NewIdentityCache(repo, id) - results, op, err := b.ChangeLabelsRaw(author, time.Now().Unix(), input.Added, input.Removed, nil) + results, op, err := b.ChangeLabelsRaw(id, time.Now().Unix(), input.Added, input.Removed, nil) if err != nil { return nil, err } @@ -135,13 +135,14 @@ func (r mutationResolver) OpenBug(ctx context.Context, input models.OpenBugInput return nil, err } - id := identity.ForContext(ctx, repo) - if id == nil { + id, err := graphqlidentity.ForContext(ctx, repo) + if err != nil { + return nil, err + } else if id == nil { return nil, ErrNotAuthenticated } - author := cache.NewIdentityCache(repo, id) - op, err := b.OpenRaw(author, time.Now().Unix(), nil) + op, err := b.OpenRaw(id, time.Now().Unix(), nil) if err != nil { return nil, err } @@ -164,13 +165,14 @@ func (r mutationResolver) CloseBug(ctx context.Context, input models.CloseBugInp return nil, err } - id := identity.ForContext(ctx, repo) - if id == nil { + id, err := graphqlidentity.ForContext(ctx, repo) + if err != nil { + return nil, err + } else if id == nil { return nil, ErrNotAuthenticated } - author := cache.NewIdentityCache(repo, id) - op, err := b.CloseRaw(author, time.Now().Unix(), nil) + op, err := b.CloseRaw(id, time.Now().Unix(), nil) if err != nil { return nil, err } @@ -193,13 +195,14 @@ func (r mutationResolver) SetTitle(ctx context.Context, input models.SetTitleInp return nil, err } - id := identity.ForContext(ctx, repo) - if id == nil { + id, err := graphqlidentity.ForContext(ctx, repo) + if err != nil { + return nil, err + } else if id == nil { return nil, ErrNotAuthenticated } - author := cache.NewIdentityCache(repo, id) - op, err := b.SetTitleRaw(author, time.Now().Unix(), input.Title, nil) + op, err := b.SetTitleRaw(id, time.Now().Unix(), input.Title, nil) if err != nil { return nil, err } diff --git a/graphql/resolvers/repo.go b/graphql/resolvers/repo.go index ded98636..f68ed3e0 100644 --- a/graphql/resolvers/repo.go +++ b/graphql/resolvers/repo.go @@ -7,8 +7,8 @@ import ( "github.com/MichaelMure/git-bug/entity" "github.com/MichaelMure/git-bug/graphql/connections" "github.com/MichaelMure/git-bug/graphql/graph" + "github.com/MichaelMure/git-bug/graphql/graphqlidentity" "github.com/MichaelMure/git-bug/graphql/models" - "github.com/MichaelMure/git-bug/identity" "github.com/MichaelMure/git-bug/query" ) @@ -151,11 +151,13 @@ func (repoResolver) Identity(_ context.Context, obj *models.Repository, prefix s } func (repoResolver) UserIdentity(ctx context.Context, obj *models.Repository) (models.IdentityWrapper, error) { - id := identity.ForContext(ctx, obj.Repo) - if id == nil { + id, err := graphqlidentity.ForContext(ctx, obj.Repo) + if err != nil { + return nil, err + } else if id == nil { return nil, nil } - return models.NewLoadedIdentity(id), nil + return models.NewLoadedIdentity(id.Identity), nil } func (repoResolver) ValidLabels(_ context.Context, obj *models.Repository, after *string, before *string, first *int, last *int) (*models.LabelConnection, error) { |