graphql: move the mutations to a root Mutation type

This commit is contained in:
Michael Muré 2018-07-30 01:36:41 +02:00
parent 3cb0469a22
commit e14f1b4264
No known key found for this signature in database
GPG Key ID: A4457C029293126F
5 changed files with 154 additions and 157 deletions

View File

@ -40,13 +40,13 @@ type Resolvers interface {
LabelChangeOperation_date(ctx context.Context, obj *operations.LabelChangeOperation) (time.Time, error)
Mutation_newBug(ctx context.Context, repoRef *string, title string, message string) (bug.Snapshot, error)
Query_defaultRepository(ctx context.Context) (*models.Repository, error)
Query_repository(ctx context.Context, id string) (*models.Repository, error)
Repository_allBugs(ctx context.Context, obj *models.Repository, input models.ConnectionInput) (models.BugConnection, error)
Repository_bug(ctx context.Context, obj *models.Repository, prefix string) (*bug.Snapshot, error)
Repository_mutation(ctx context.Context, obj *models.Repository) (models.RepositoryMutation, error)
RepositoryMutation_newBug(ctx context.Context, obj *models.RepositoryMutation, title string, message string) (bug.Snapshot, error)
SetStatusOperation_date(ctx context.Context, obj *operations.SetStatusOperation) (time.Time, error)
SetStatusOperation_status(ctx context.Context, obj *operations.SetStatusOperation) (models.Status, error)
@ -59,9 +59,9 @@ type ResolverRoot interface {
Bug() BugResolver
CreateOperation() CreateOperationResolver
LabelChangeOperation() LabelChangeOperationResolver
Mutation() MutationResolver
Query() QueryResolver
Repository() RepositoryResolver
RepositoryMutation() RepositoryMutationResolver
SetStatusOperation() SetStatusOperationResolver
SetTitleOperation() SetTitleOperationResolver
}
@ -80,6 +80,9 @@ type CreateOperationResolver interface {
type LabelChangeOperationResolver interface {
Date(ctx context.Context, obj *operations.LabelChangeOperation) (time.Time, error)
}
type MutationResolver interface {
NewBug(ctx context.Context, repoRef *string, title string, message string) (bug.Snapshot, error)
}
type QueryResolver interface {
DefaultRepository(ctx context.Context) (*models.Repository, error)
Repository(ctx context.Context, id string) (*models.Repository, error)
@ -87,10 +90,6 @@ type QueryResolver interface {
type RepositoryResolver interface {
AllBugs(ctx context.Context, obj *models.Repository, input models.ConnectionInput) (models.BugConnection, error)
Bug(ctx context.Context, obj *models.Repository, prefix string) (*bug.Snapshot, error)
Mutation(ctx context.Context, obj *models.Repository) (models.RepositoryMutation, error)
}
type RepositoryMutationResolver interface {
NewBug(ctx context.Context, obj *models.RepositoryMutation, title string, message string) (bug.Snapshot, error)
}
type SetStatusOperationResolver interface {
Date(ctx context.Context, obj *operations.SetStatusOperation) (time.Time, error)
@ -128,6 +127,10 @@ func (s shortMapper) LabelChangeOperation_date(ctx context.Context, obj *operati
return s.r.LabelChangeOperation().Date(ctx, obj)
}
func (s shortMapper) Mutation_newBug(ctx context.Context, repoRef *string, title string, message string) (bug.Snapshot, error) {
return s.r.Mutation().NewBug(ctx, repoRef, title, message)
}
func (s shortMapper) Query_defaultRepository(ctx context.Context) (*models.Repository, error) {
return s.r.Query().DefaultRepository(ctx)
}
@ -144,14 +147,6 @@ func (s shortMapper) Repository_bug(ctx context.Context, obj *models.Repository,
return s.r.Repository().Bug(ctx, obj, prefix)
}
func (s shortMapper) Repository_mutation(ctx context.Context, obj *models.Repository) (models.RepositoryMutation, error) {
return s.r.Repository().Mutation(ctx, obj)
}
func (s shortMapper) RepositoryMutation_newBug(ctx context.Context, obj *models.RepositoryMutation, title string, message string) (bug.Snapshot, error) {
return s.r.RepositoryMutation().NewBug(ctx, obj, title, message)
}
func (s shortMapper) SetStatusOperation_date(ctx context.Context, obj *operations.SetStatusOperation) (time.Time, error) {
return s.r.SetStatusOperation().Date(ctx, obj)
}
@ -189,7 +184,19 @@ func (e *executableSchema) Query(ctx context.Context, op *query.Operation) *grap
}
func (e *executableSchema) Mutation(ctx context.Context, op *query.Operation) *graphql.Response {
return graphql.ErrorResponse(ctx, "mutations are not supported")
ec := executionContext{graphql.GetRequestContext(ctx), e.resolvers}
buf := ec.RequestMiddleware(ctx, func(ctx context.Context) []byte {
data := ec._Mutation(ctx, op.Selections)
var buf bytes.Buffer
data.MarshalGQL(&buf)
return buf.Bytes()
})
return &graphql.Response{
Data: buf,
Errors: ec.Errors,
}
}
func (e *executableSchema) Subscription(ctx context.Context, op *query.Operation) func() *graphql.Response {
@ -962,6 +969,90 @@ func (ec *executionContext) _LabelChangeOperation_removed(ctx context.Context, f
return arr1
}
var mutationImplementors = []string{"Mutation"}
// nolint: gocyclo, errcheck, gas, goconst
func (ec *executionContext) _Mutation(ctx context.Context, sel []query.Selection) graphql.Marshaler {
fields := graphql.CollectFields(ec.Doc, sel, mutationImplementors, ec.Variables)
ctx = graphql.WithResolverContext(ctx, &graphql.ResolverContext{
Object: "Mutation",
})
out := graphql.NewOrderedMap(len(fields))
for i, field := range fields {
out.Keys[i] = field.Alias
switch field.Name {
case "__typename":
out.Values[i] = graphql.MarshalString("Mutation")
case "newBug":
out.Values[i] = ec._Mutation_newBug(ctx, field)
default:
panic("unknown field " + strconv.Quote(field.Name))
}
}
return out
}
func (ec *executionContext) _Mutation_newBug(ctx context.Context, field graphql.CollectedField) graphql.Marshaler {
args := map[string]interface{}{}
var arg0 *string
if tmp, ok := field.Args["repoRef"]; ok {
var err error
var ptr1 string
if tmp != nil {
ptr1, err = graphql.UnmarshalString(tmp)
arg0 = &ptr1
}
if err != nil {
ec.Error(ctx, err)
return graphql.Null
}
}
args["repoRef"] = arg0
var arg1 string
if tmp, ok := field.Args["title"]; ok {
var err error
arg1, err = graphql.UnmarshalString(tmp)
if err != nil {
ec.Error(ctx, err)
return graphql.Null
}
}
args["title"] = arg1
var arg2 string
if tmp, ok := field.Args["message"]; ok {
var err error
arg2, err = graphql.UnmarshalString(tmp)
if err != nil {
ec.Error(ctx, err)
return graphql.Null
}
}
args["message"] = arg2
rctx := graphql.GetResolverContext(ctx)
rctx.Object = "Mutation"
rctx.Args = args
rctx.Field = field
rctx.PushField(field.Alias)
defer rctx.Pop()
resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) {
return ec.resolvers.Mutation_newBug(ctx, args["repoRef"].(*string), args["title"].(string), args["message"].(string))
})
if err != nil {
ec.Error(ctx, err)
return graphql.Null
}
if resTmp == nil {
return graphql.Null
}
res := resTmp.(bug.Snapshot)
return ec._Bug(ctx, field.Selections, &res)
}
var operationConnectionImplementors = []string{"OperationConnection"}
// nolint: gocyclo, errcheck, gas, goconst
@ -1338,8 +1429,6 @@ func (ec *executionContext) _Repository(ctx context.Context, sel []query.Selecti
out.Values[i] = ec._Repository_allBugs(ctx, field, obj)
case "bug":
out.Values[i] = ec._Repository_bug(ctx, field, obj)
case "mutation":
out.Values[i] = ec._Repository_mutation(ctx, field, obj)
default:
panic("unknown field " + strconv.Quote(field.Name))
}
@ -1433,110 +1522,6 @@ func (ec *executionContext) _Repository_bug(ctx context.Context, field graphql.C
})
}
func (ec *executionContext) _Repository_mutation(ctx context.Context, field graphql.CollectedField, obj *models.Repository) graphql.Marshaler {
ctx = graphql.WithResolverContext(ctx, &graphql.ResolverContext{
Object: "Repository",
Args: nil,
Field: field,
})
return graphql.Defer(func() (ret graphql.Marshaler) {
defer func() {
if r := recover(); r != nil {
userErr := ec.Recover(ctx, r)
ec.Error(ctx, userErr)
ret = graphql.Null
}
}()
resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) {
return ec.resolvers.Repository_mutation(ctx, obj)
})
if err != nil {
ec.Error(ctx, err)
return graphql.Null
}
if resTmp == nil {
return graphql.Null
}
res := resTmp.(models.RepositoryMutation)
return ec._RepositoryMutation(ctx, field.Selections, &res)
})
}
var repositoryMutationImplementors = []string{"RepositoryMutation"}
// nolint: gocyclo, errcheck, gas, goconst
func (ec *executionContext) _RepositoryMutation(ctx context.Context, sel []query.Selection, obj *models.RepositoryMutation) graphql.Marshaler {
fields := graphql.CollectFields(ec.Doc, sel, repositoryMutationImplementors, ec.Variables)
out := graphql.NewOrderedMap(len(fields))
for i, field := range fields {
out.Keys[i] = field.Alias
switch field.Name {
case "__typename":
out.Values[i] = graphql.MarshalString("RepositoryMutation")
case "newBug":
out.Values[i] = ec._RepositoryMutation_newBug(ctx, field, obj)
default:
panic("unknown field " + strconv.Quote(field.Name))
}
}
return out
}
func (ec *executionContext) _RepositoryMutation_newBug(ctx context.Context, field graphql.CollectedField, obj *models.RepositoryMutation) graphql.Marshaler {
args := map[string]interface{}{}
var arg0 string
if tmp, ok := field.Args["title"]; ok {
var err error
arg0, err = graphql.UnmarshalString(tmp)
if err != nil {
ec.Error(ctx, err)
return graphql.Null
}
}
args["title"] = arg0
var arg1 string
if tmp, ok := field.Args["message"]; ok {
var err error
arg1, err = graphql.UnmarshalString(tmp)
if err != nil {
ec.Error(ctx, err)
return graphql.Null
}
}
args["message"] = arg1
ctx = graphql.WithResolverContext(ctx, &graphql.ResolverContext{
Object: "RepositoryMutation",
Args: args,
Field: field,
})
return graphql.Defer(func() (ret graphql.Marshaler) {
defer func() {
if r := recover(); r != nil {
userErr := ec.Recover(ctx, r)
ec.Error(ctx, userErr)
ret = graphql.Null
}
}()
resTmp, err := ec.ResolverMiddleware(ctx, func(ctx context.Context) (interface{}, error) {
return ec.resolvers.RepositoryMutation_newBug(ctx, obj, args["title"].(string), args["message"].(string))
})
if err != nil {
ec.Error(ctx, err)
return graphql.Null
}
if resTmp == nil {
return graphql.Null
}
res := resTmp.(bug.Snapshot)
return ec._Bug(ctx, field.Selections, &res)
})
}
var setStatusOperationImplementors = []string{"SetStatusOperation", "Operation", "Authored"}
// nolint: gocyclo, errcheck, gas, goconst
@ -2702,8 +2687,6 @@ type Bug {
type Repository {
allBugs(input: ConnectionInput!): BugConnection!
bug(prefix: String!): Bug
mutation: RepositoryMutation!
}
type Query {
@ -2711,7 +2694,7 @@ type Query {
repository(id: String!): Repository
}
type RepositoryMutation {
newBug(title: String!, message: String!): Bug!
}`)
type Mutation {
newBug(repoRef: String, title: String!, message: String!): Bug!
}
`)

View File

@ -0,0 +1,35 @@
package resolvers
import (
"context"
"github.com/MichaelMure/git-bug/bug"
"github.com/MichaelMure/git-bug/cache"
)
type mutationResolver struct {
cache cache.Cacher
}
func (r mutationResolver) getRepo(repoRef *string) (cache.RepoCacher, error) {
if repoRef != nil {
return r.cache.ResolveRepo(*repoRef)
}
return r.cache.DefaultRepo()
}
func (r mutationResolver) NewBug(ctx context.Context, repoRef *string, title string, message string) (bug.Snapshot, error) {
repo, err := r.getRepo(repoRef)
if err != nil {
return bug.Snapshot{}, err
}
b, err := repo.NewBug(title, message)
if err != nil {
return bug.Snapshot{}, err
}
snap := b.Snapshot()
return *snap, nil
}

View File

@ -1,20 +0,0 @@
package resolvers
import (
"context"
"github.com/MichaelMure/git-bug/bug"
"github.com/MichaelMure/git-bug/graphql/models"
)
type repoMutationResolver struct{}
func (repoMutationResolver) NewBug(ctx context.Context, obj *models.RepositoryMutation, title string, message string) (bug.Snapshot, error) {
b, err := obj.Repo.NewBug(title, message)
if err != nil {
return bug.Snapshot{}, err
}
snap := b.Snapshot()
return *snap, nil
}

View File

@ -21,6 +21,12 @@ func (r Backend) Query() graph.QueryResolver {
}
}
func (r Backend) Mutation() graph.MutationResolver {
return &mutationResolver{
cache: &r.RootCache,
}
}
func (Backend) AddCommentOperation() graph.AddCommentOperationResolver {
return &addCommentOperationResolver{}
}
@ -48,7 +54,3 @@ func (Backend) SetStatusOperation() graph.SetStatusOperationResolver {
func (Backend) SetTitleOperation() graph.SetTitleOperationResolver {
return &setTitleOperationResolver{}
}
func (r Backend) RepositoryMutation() graph.RepositoryMutationResolver {
return &repoMutationResolver{}
}

View File

@ -166,8 +166,6 @@ type Bug {
type Repository {
allBugs(input: ConnectionInput!): BugConnection!
bug(prefix: String!): Bug
mutation: RepositoryMutation!
}
type Query {
@ -175,7 +173,6 @@ type Query {
repository(id: String!): Repository
}
type RepositoryMutation {
newBug(title: String!, message: String!): Bug!
type Mutation {
newBug(repoRef: String, title: String!, message: String!): Bug!
}