diff options
author | Michael Muré <batolettre@gmail.com> | 2018-07-30 01:36:41 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2018-07-30 01:36:41 +0200 |
commit | e14f1b4264ec5196834db925202d9d9f93ac35c5 (patch) | |
tree | 754b5b34d9851d0190b4631d43d57d77ce55f284 /graphql | |
parent | 3cb0469a2200dea24aff1f414aa971d5e6fe9815 (diff) | |
download | git-bug-e14f1b4264ec5196834db925202d9d9f93ac35c5.tar.gz |
graphql: move the mutations to a root Mutation type
Diffstat (limited to 'graphql')
-rw-r--r-- | graphql/graph/gen_graph.go | 239 | ||||
-rw-r--r-- | graphql/resolvers/mutation.go | 35 | ||||
-rw-r--r-- | graphql/resolvers/repo_mutation.go | 20 | ||||
-rw-r--r-- | graphql/resolvers/root.go | 10 | ||||
-rw-r--r-- | graphql/schema.graphql | 7 |
5 files changed, 154 insertions, 157 deletions
diff --git a/graphql/graph/gen_graph.go b/graphql/graph/gen_graph.go index 01887dc8..aa4fcc15 100644 --- a/graphql/graph/gen_graph.go +++ b/graphql/graph/gen_graph.go @@ -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! +} +`) diff --git a/graphql/resolvers/mutation.go b/graphql/resolvers/mutation.go new file mode 100644 index 00000000..b06e94cc --- /dev/null +++ b/graphql/resolvers/mutation.go @@ -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 +} diff --git a/graphql/resolvers/repo_mutation.go b/graphql/resolvers/repo_mutation.go deleted file mode 100644 index 0c28cc0d..00000000 --- a/graphql/resolvers/repo_mutation.go +++ /dev/null @@ -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 -} diff --git a/graphql/resolvers/root.go b/graphql/resolvers/root.go index 36a9a391..2cf629cd 100644 --- a/graphql/resolvers/root.go +++ b/graphql/resolvers/root.go @@ -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{} -} diff --git a/graphql/schema.graphql b/graphql/schema.graphql index b481d2d7..52623301 100644 --- a/graphql/schema.graphql +++ b/graphql/schema.graphql @@ -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! } |