From b2f8572c4493205535558fb9320689aaf4774dc1 Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Sun, 16 Jun 2019 21:29:49 +0200 Subject: graphql: change mutations to respect the Relay specification https://facebook.github.io/relay/graphql/mutations.htm This specification also allow to expose a mutationId for fire and forget, as well as the created operation. --- graphql/resolvers/label.go | 23 ++++++++ graphql/resolvers/mutation.go | 125 +++++++++++++++++++++++++++++----------- graphql/resolvers/operations.go | 2 +- graphql/resolvers/root.go | 12 ++-- 4 files changed, 122 insertions(+), 40 deletions(-) (limited to 'graphql/resolvers') diff --git a/graphql/resolvers/label.go b/graphql/resolvers/label.go index 7619f748..690bf7f6 100644 --- a/graphql/resolvers/label.go +++ b/graphql/resolvers/label.go @@ -2,10 +2,12 @@ package resolvers import ( "context" + "fmt" "image/color" "github.com/MichaelMure/git-bug/bug" "github.com/MichaelMure/git-bug/graphql/graph" + "github.com/MichaelMure/git-bug/graphql/models" ) var _ graph.LabelResolver = &labelResolver{} @@ -20,3 +22,24 @@ func (labelResolver) Color(ctx context.Context, obj *bug.Label) (*color.RGBA, er rgba := obj.RGBA() return &rgba, nil } + +var _ graph.LabelChangeResultResolver = &labelChangeResultResolver{} + +type labelChangeResultResolver struct{} + +func (labelChangeResultResolver) Status(ctx context.Context, obj *bug.LabelChangeResult) (models.LabelChangeStatus, error) { + switch obj.Status { + case bug.LabelChangeAdded: + return models.LabelChangeStatusAdded, nil + case bug.LabelChangeRemoved: + return models.LabelChangeStatusRemoved, nil + case bug.LabelChangeDuplicateInOp: + return models.LabelChangeStatusDuplicateInOp, nil + case bug.LabelChangeAlreadySet: + return models.LabelChangeStatusAlreadyExist, nil + case bug.LabelChangeDoesntExist: + return models.LabelChangeStatusDoesntExist, nil + } + + return "", fmt.Errorf("unknown status") +} diff --git a/graphql/resolvers/mutation.go b/graphql/resolvers/mutation.go index d10d2ea3..9ed4e780 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{} @@ -23,130 +23,185 @@ func (r mutationResolver) getRepo(repoRef *string) (*cache.RepoCache, error) { 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 } diff --git a/graphql/resolvers/operations.go b/graphql/resolvers/operations.go index 90817567..19b2b17f 100644 --- a/graphql/resolvers/operations.go +++ b/graphql/resolvers/operations.go @@ -63,5 +63,5 @@ func convertStatus(status bug.Status) (models.Status, error) { return models.StatusClosed, nil } - return "", fmt.Errorf("Unknown status") + return "", fmt.Errorf("unknown status") } diff --git a/graphql/resolvers/root.go b/graphql/resolvers/root.go index f6a4a57b..8873b723 100644 --- a/graphql/resolvers/root.go +++ b/graphql/resolvers/root.go @@ -30,6 +30,10 @@ func (r RootResolver) Mutation() graph.MutationResolver { } } +func (RootResolver) Repository() graph.RepositoryResolver { + return &repoResolver{} +} + func (RootResolver) Bug() graph.BugResolver { return &bugResolver{} } @@ -86,10 +90,6 @@ func (RootResolver) LabelChangeOperation() graph.LabelChangeOperationResolver { return &labelChangeOperation{} } -func (RootResolver) Repository() graph.RepositoryResolver { - return &repoResolver{} -} - func (RootResolver) SetStatusOperation() graph.SetStatusOperationResolver { return &setStatusOperationResolver{} } @@ -97,3 +97,7 @@ func (RootResolver) SetStatusOperation() graph.SetStatusOperationResolver { func (RootResolver) SetTitleOperation() graph.SetTitleOperationResolver { return &setTitleOperationResolver{} } + +func (r RootResolver) LabelChangeResult() graph.LabelChangeResultResolver { + return &labelChangeResultResolver{} +} -- cgit From 9f4da4ce4fbbf23798b80289af170c9c42ac73eb Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Sun, 23 Jun 2019 21:28:01 +0200 Subject: cache: consistently use "ref" to fetch a repository --- graphql/resolvers/mutation.go | 6 +++--- graphql/resolvers/query.go | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'graphql/resolvers') diff --git a/graphql/resolvers/mutation.go b/graphql/resolvers/mutation.go index 9ed4e780..b4f8845a 100644 --- a/graphql/resolvers/mutation.go +++ b/graphql/resolvers/mutation.go @@ -15,9 +15,9 @@ 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() diff --git a/graphql/resolvers/query.go b/graphql/resolvers/query.go index 80b5a896..aa4e1d85 100644 --- a/graphql/resolvers/query.go +++ b/graphql/resolvers/query.go @@ -27,8 +27,8 @@ func (r rootQueryResolver) DefaultRepository(ctx context.Context) (*models.Repos }, nil } -func (r rootQueryResolver) Repository(ctx context.Context, id string) (*models.Repository, error) { - repo, err := r.cache.ResolveRepo(id) +func (r rootQueryResolver) Repository(ctx context.Context, ref string) (*models.Repository, error) { + repo, err := r.cache.ResolveRepo(ref) if err != nil { return nil, err -- cgit