Skip to content

Commit

Permalink
entgql: add example for create edge with mutation input (#416)
Browse files Browse the repository at this point in the history
* entgql: add example for create edge with mutation input

Signed-off-by: Giau. Tran Minh <hello@giautm.dev>

* fix: should append instead of replace

Signed-off-by: Giau. Tran Minh <hello@giautm.dev>

Signed-off-by: Giau. Tran Minh <hello@giautm.dev>
  • Loading branch information
giautm authored Dec 11, 2022
1 parent 1123220 commit 4a57f9a
Show file tree
Hide file tree
Showing 14 changed files with 845 additions and 12 deletions.
6 changes: 6 additions & 0 deletions entgql/internal/todo/ent.resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,14 @@ func (r *queryResolver) Users(ctx context.Context, after *ent.Cursor, first *int
// Query returns QueryResolver implementation.
func (r *Resolver) Query() QueryResolver { return &queryResolver{r} }

// CreateCategoryInput returns CreateCategoryInputResolver implementation.
func (r *Resolver) CreateCategoryInput() CreateCategoryInputResolver {
return &createCategoryInputResolver{r}
}

// TodoWhereInput returns TodoWhereInputResolver implementation.
func (r *Resolver) TodoWhereInput() TodoWhereInputResolver { return &todoWhereInputResolver{r} }

type queryResolver struct{ *Resolver }
type createCategoryInputResolver struct{ *Resolver }
type todoWhereInputResolver struct{ *Resolver }
180 changes: 177 additions & 3 deletions entgql/internal/todo/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions entgql/internal/todo/todo.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,15 @@ extend type Query {
}

type Mutation {
createCategory(input: CreateCategoryInput!): Category!
createTodo(input: CreateTodoInput!): Todo!
clearTodos: Int!
}

type Custom {
info: String!
}

extend input CreateCategoryInput {
createTodos: [CreateTodoInput!]
}
22 changes: 22 additions & 0 deletions entgql/internal/todo/todo.resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ import (
"entgo.io/contrib/entgql/internal/todo/ent/todo"
)

func (r *mutationResolver) CreateCategory(ctx context.Context, input ent.CreateCategoryInput) (*ent.Category, error) {
return ent.FromContext(ctx).Category.Create().SetInput(input).Save(ctx)
}

func (r *mutationResolver) CreateTodo(ctx context.Context, input ent.CreateTodoInput) (*ent.Todo, error) {
return ent.FromContext(ctx).Todo.
Create().
Expand All @@ -43,6 +47,24 @@ func (r *queryResolver) Ping(ctx context.Context) (string, error) {
return "pong", nil
}

func (r *createCategoryInputResolver) CreateTodos(ctx context.Context, obj *ent.CreateCategoryInput, data []*ent.CreateTodoInput) error {
e := ent.FromContext(ctx)
builders := make([]*ent.TodoCreate, len(data))
for i, input := range data {
builders[i] = e.Todo.Create().SetInput(*input)
}
todos, err := e.Todo.CreateBulk(builders...).Save(ctx)
if err != nil {
return err
}
ids := make([]int, len(todos))
for i := range todos {
ids[i] = todos[i].ID
}
obj.TodoIDs = append(obj.TodoIDs, ids...)
return nil
}

func (r *todoWhereInputResolver) CreatedToday(ctx context.Context, obj *ent.TodoWhereInput, data *bool) error {
if data == nil {
return nil
Expand Down
Loading

0 comments on commit 4a57f9a

Please sign in to comment.