This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| DROP DATABASE IF EXISTS golang_clean_architecture_ent_gqlgen_test; | |
| CREATE DATABASE golang_clean_architecture_ent_gqlgen_test CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func (r *userRepository) CreateWithTodo(ctx context.Context, input model.CreateUserInput) (*model.User, error) { | |
| client := WithTransactionalMutation(ctx) | |
| todo, err := client. | |
| Todo. | |
| Create(). | |
| Save(ctx) | |
| if err != nil { | |
| return nil, model.NewDBError(err) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package repository | |
| import ( | |
| "context" | |
| "golang-clean-architecture-ent-gqlgen/ent" | |
| ) | |
| // WithTransactionalMutation automatically wrap the GraphQL mutations with a database transaction. | |
| // This allows the ent.Client to commit at the end, or rollback the transaction in case of a GraphQL error. | |
| func WithTransactionalMutation(ctx context.Context) *ent.Client { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| srv.Use(entgql.Transactioner{TxOpener: client}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func (r *mutationResolver) CreateUser(ctx context.Context, input ent.CreateUserInput) (*ent.User, error) { | |
| u, err := r.controller.User.Create(ctx, input) | |
| if err != nil { | |
| return nil, handler.HandleError(ctx, err) | |
| } | |
| return u, nil | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package handler | |
| import ( | |
| "context" | |
| "errors" | |
| "golang-clean-architecture-ent-gqlgen/pkg/entity/model" | |
| "github.com/99designs/gqlgen/graphql" | |
| "github.com/vektah/gqlparser/v2/gqlerror" | |
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func (r *userRepository) Create(ctx context.Context, input model.CreateUserInput) (*model.User, error) { | |
| u, err := r.client.User.Create().SetInput(input).Save(ctx) | |
| if err != nil { | |
| return nil, model.NewDBError(err) | |
| } | |
| return u, nil | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package model | |
| import ( | |
| "fmt" | |
| "golang-clean-architecture-ent-gqlgen/pkg/util/environment" | |
| "github.com/pkg/errors" | |
| ) | |
| const ( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func (r *queryResolver) Users(ctx context.Context, after *ent.Cursor, first *int, before *ent.Cursor, last *int, where *ent.UserWhereInput) (*ent.UserConnection, error) { | |
| us, err := r.controller.User.List(ctx, after, first, before, last, where) | |
| if err != nil { | |
| return nil, err | |
| } | |
| return us, nil | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func (r *userRepository) List(ctx context.Context, after *model.Cursor, first *int, before *model.Cursor, last *int, where *model.UserWhereInput) (*model.UserConnection, error) { | |
| us, err := r.client. | |
| User. | |
| Query(). | |
| Paginate(ctx, after, first, before, last, ent.WithUserFilter(where.Filter)) | |
| if err != nil { | |
| return nil, err | |
| } | |
| return us, nil | |
| } |