I have project using gqlgen , and I am trying to migrate to using DDD pattern for this project, in my resolvers.go
:
type Resolver struct{}
type mutationResolver struct{ *Resolver }
type queryResolver struct{ *Resolver }
func New() *Resolver{
return &Resolver{}
}
func (r *mutationResolver) DeleteDocument(ctx context.Context, id string) (bool, error) {
panic("not inplemented yet")
}
here for DeleteDocument
was generated by gqlgen
and in that DeleteDocument
I do some logi and do data manipulation, I was using a function on another file like this:
/*
folder_document
- delete_document.go
*/
func DeleteDoc(ctx context.Context, id string) (bool, error) {
// some logic
// after pass logic delete document
// return true if success
}
// and then on `resolvers.go` for function DeleteDocument
func (r *mutationResolver) DeleteDocument(ctx context.Context, id string) (bool, error) {
success, err := folder_document.DeleteDoc(ctx, id)
// return .....
}
you can see that I do logic and manipulation data in one function, and it might gonna have 10 - 100 lines or so, and it gonna hard for my team to read my code for it
so I am thinking about split it into struct like ddd pattern , example:
/*
folder_document
- repository.go
- service.go
- interfaces.go
*/
// interfaces.go
type DocumentRepo interface {
DeleteDoc(ctx context.Context, id string) (bool, error)
}
type DocumentService interface {
DeleteNewDoc(ctx context.Context, id string) (bool, error)
}
// repository.go
type documentRepository {
db *mongo.Client or *sql.db
}
func (useDB *documentRepository) DeleteDoc(ctx context.Context, id string) (bool, error) {
// manipulation data
}
// service.go
type documentService struct {
docRepo DocumentRepo
}
func (userService *documentService) DeleteNewDoc(ctx context.Context, id string) (bool, error){
_, _ = userService.docRepo.DeleteDoc(ctx, id)
}
it is possible to use that service in file resolvers.go
??
here is how I run the service on main.go
:
func graphqlHandler() echo.HandlerFunc {
config := resolver.Config{
Resolvers: resolver.New(),
}
h := handler.NewDefaultServer(resolver.NewExecutableSchema(config))
return func(c echo.Context) error {
h.ServeHTTP(c.Response(), c.Request())
return nil
}
u can see that resolver.New()
it got from resolvers.go
Aucun commentaire:
Enregistrer un commentaire