diff options
author | Michael Muré <batolettre@gmail.com> | 2019-06-23 21:36:42 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-06-23 21:36:42 +0200 |
commit | 54e95cf36f4e50d4175f78410f3dc504a7e34179 (patch) | |
tree | 356011d5d539d259a696fe00fe6edb174b7ec268 /graphql/resolvers/mutation.go | |
parent | 5bae915b53ed2bb4f222db2fdc73852099b96fd2 (diff) | |
parent | 17cbe457d2d782bee914680b38fa101c9327b811 (diff) | |
download | git-bug-54e95cf36f4e50d4175f78410f3dc504a7e34179.tar.gz |
Merge pull request #171 from MichaelMure/graphql-mutation-relay
Graphql mutation relay
Diffstat (limited to 'graphql/resolvers/mutation.go')
-rw-r--r-- | graphql/resolvers/mutation.go | 131 |
1 files changed, 93 insertions, 38 deletions
diff --git a/graphql/resolvers/mutation.go b/graphql/resolvers/mutation.go index d10d2ea3..b4f8845a 100644 --- a/graphql/resolvers/mutation.go +++ b/graphql/resolvers/mutation.go @@ -6,7 +6,7 @@ import ( "github.com/MichaelMure/git-bug/bug" "github.com/MichaelMure/git-bug/cache" "github.com/MichaelMure/git-bug/graphql/graph" - "github.com/MichaelMure/git-bug/util/git" + "github.com/MichaelMure/git-bug/graphql/models" ) var _ graph.MutationResolver = &mutationResolver{} @@ -15,138 +15,193 @@ type mutationResolver struct { cache *cache.MultiRepoCache } -func (r mutationResolver) getRepo(repoRef *string) (*cache.RepoCache, error) { - if repoRef != nil { - return r.cache.ResolveRepo(*repoRef) +func (r mutationResolver) getRepo(ref *string) (*cache.RepoCache, error) { + if ref != nil { + return r.cache.ResolveRepo(*ref) } return r.cache.DefaultRepo() } -func (r mutationResolver) NewBug(ctx context.Context, repoRef *string, title string, message string, files []git.Hash) (*bug.Snapshot, error) { - repo, err := r.getRepo(repoRef) +func (r mutationResolver) NewBug(ctx context.Context, input models.NewBugInput) (*models.NewBugPayload, error) { + repo, err := r.getRepo(input.RepoRef) if err != nil { return nil, err } - b, err := repo.NewBugWithFiles(title, message, files) + b, op, err := repo.NewBugWithFiles(input.Title, input.Message, input.Files) if err != nil { return nil, err } - return b.Snapshot(), nil + return &models.NewBugPayload{ + ClientMutationID: input.ClientMutationID, + Bug: b.Snapshot(), + Operation: op, + }, nil } -func (r mutationResolver) Commit(ctx context.Context, repoRef *string, prefix string) (*bug.Snapshot, error) { - repo, err := r.getRepo(repoRef) +func (r mutationResolver) AddComment(ctx context.Context, input models.AddCommentInput) (*models.AddCommentPayload, error) { + repo, err := r.getRepo(input.RepoRef) if err != nil { return nil, err } - b, err := repo.ResolveBugPrefix(prefix) + b, err := repo.ResolveBugPrefix(input.Prefix) if err != nil { return nil, err } - err = b.Commit() + op, err := b.AddCommentWithFiles(input.Message, input.Files) + if err != nil { + return nil, err + } + + return &models.AddCommentPayload{ + ClientMutationID: input.ClientMutationID, + Bug: b.Snapshot(), + Operation: op, + }, nil +} + +func (r mutationResolver) ChangeLabels(ctx context.Context, input *models.ChangeLabelInput) (*models.ChangeLabelPayload, error) { + repo, err := r.getRepo(input.RepoRef) + if err != nil { + return nil, err + } + + b, err := repo.ResolveBugPrefix(input.Prefix) if err != nil { return nil, err } - return b.Snapshot(), nil + results, op, err := b.ChangeLabels(input.Added, input.Removed) + if err != nil { + return nil, err + } + + resultsPtr := make([]*bug.LabelChangeResult, len(results)) + for i, result := range results { + resultsPtr[i] = &result + } + + return &models.ChangeLabelPayload{ + ClientMutationID: input.ClientMutationID, + Bug: b.Snapshot(), + Operation: op, + Results: resultsPtr, + }, nil } -func (r mutationResolver) AddComment(ctx context.Context, repoRef *string, prefix string, message string, files []git.Hash) (*bug.Snapshot, error) { - repo, err := r.getRepo(repoRef) +func (r mutationResolver) OpenBug(ctx context.Context, input models.OpenBugInput) (*models.OpenBugPayload, error) { + repo, err := r.getRepo(input.RepoRef) if err != nil { return nil, err } - b, err := repo.ResolveBugPrefix(prefix) + b, err := repo.ResolveBugPrefix(input.Prefix) if err != nil { return nil, err } - _, err = b.AddCommentWithFiles(message, files) + op, err := b.Open() if err != nil { return nil, err } - return b.Snapshot(), nil + return &models.OpenBugPayload{ + ClientMutationID: input.ClientMutationID, + Bug: b.Snapshot(), + Operation: op, + }, nil } -func (r mutationResolver) ChangeLabels(ctx context.Context, repoRef *string, prefix string, added []string, removed []string) (*bug.Snapshot, error) { - repo, err := r.getRepo(repoRef) +func (r mutationResolver) CloseBug(ctx context.Context, input models.CloseBugInput) (*models.CloseBugPayload, error) { + repo, err := r.getRepo(input.RepoRef) if err != nil { return nil, err } - b, err := repo.ResolveBugPrefix(prefix) + b, err := repo.ResolveBugPrefix(input.Prefix) if err != nil { return nil, err } - _, _, err = b.ChangeLabels(added, removed) + op, err := b.Close() if err != nil { return nil, err } - return b.Snapshot(), nil + return &models.CloseBugPayload{ + ClientMutationID: input.ClientMutationID, + Bug: b.Snapshot(), + Operation: op, + }, nil } -func (r mutationResolver) Open(ctx context.Context, repoRef *string, prefix string) (*bug.Snapshot, error) { - repo, err := r.getRepo(repoRef) +func (r mutationResolver) SetTitle(ctx context.Context, input models.SetTitleInput) (*models.SetTitlePayload, error) { + repo, err := r.getRepo(input.RepoRef) if err != nil { return nil, err } - b, err := repo.ResolveBugPrefix(prefix) + b, err := repo.ResolveBugPrefix(input.Prefix) if err != nil { return nil, err } - _, err = b.Open() + op, err := b.SetTitle(input.Title) if err != nil { return nil, err } - return b.Snapshot(), nil + return &models.SetTitlePayload{ + ClientMutationID: input.ClientMutationID, + Bug: b.Snapshot(), + Operation: op, + }, nil } -func (r mutationResolver) Close(ctx context.Context, repoRef *string, prefix string) (*bug.Snapshot, error) { - repo, err := r.getRepo(repoRef) +func (r mutationResolver) Commit(ctx context.Context, input models.CommitInput) (*models.CommitPayload, error) { + repo, err := r.getRepo(input.RepoRef) if err != nil { return nil, err } - b, err := repo.ResolveBugPrefix(prefix) + b, err := repo.ResolveBugPrefix(input.Prefix) if err != nil { return nil, err } - _, err = b.Close() + err = b.Commit() if err != nil { return nil, err } - return b.Snapshot(), nil + return &models.CommitPayload{ + ClientMutationID: input.ClientMutationID, + Bug: b.Snapshot(), + }, nil } -func (r mutationResolver) SetTitle(ctx context.Context, repoRef *string, prefix string, title string) (*bug.Snapshot, error) { - repo, err := r.getRepo(repoRef) +func (r mutationResolver) CommitAsNeeded(ctx context.Context, input models.CommitAsNeededInput) (*models.CommitAsNeededPayload, error) { + repo, err := r.getRepo(input.RepoRef) if err != nil { return nil, err } - b, err := repo.ResolveBugPrefix(prefix) + b, err := repo.ResolveBugPrefix(input.Prefix) if err != nil { return nil, err } - _, err = b.SetTitle(title) + err = b.CommitAsNeeded() if err != nil { return nil, err } - return b.Snapshot(), nil + return &models.CommitAsNeededPayload{ + ClientMutationID: input.ClientMutationID, + Bug: b.Snapshot(), + }, nil } |