gofasta g service
Generates a service interface and its implementation for a given resource, along with all prerequisite layers (model, migration, repository interface, repository implementation, DTOs, and Wire provider). The service layer contains business logic and orchestrates calls to one or more repositories. Services never interact with the database directly — they delegate data access to repositories.
Aliases: svc
Usage
gofasta g service <Name> [field:type ...] [flags]Flags
| Flag | Default | Description |
|---|---|---|
--graphql | false | Also generate GraphQL schema and wire up the resolver |
--gql | false | Alias for --graphql |
Examples
Generate a service with all CRUD methods:
gofasta g service Product name:string price:floatGenerate a service with GraphQL support:
gofasta g service Product name:string price:float --graphqlWhat It Generates
Running gofasta g service Product name:string price:float creates the following files:
| File | Description |
|---|---|
app/models/product.model.go | GORM database model |
db/migrations/000006_create_products.up.sql | Create table migration |
db/migrations/000006_create_products.down.sql | Drop table migration |
app/repositories/interfaces/product_repository.go | Repository interface |
app/repositories/product.repository.go | Repository implementation |
app/services/interfaces/product_service.go | Service interface |
app/services/product.service.go | Service implementation |
app/dtos/product.dtos.go | Request/response DTOs |
app/di/providers/product.go | Wire DI provider set |
It also patches 2 existing files:
| File | Change |
|---|---|
app/di/container.go | Adds ProductService field |
app/di/wire.go | Adds ProductSet to the Wire build |
With --graphql, the service generator additionally:
- Generates a GraphQL schema file (
.gql) - Patches
resolver.goto add the service dependency - Runs
gqlgen generate
Service Interface
// app/services/interfaces/product_service.go
package interfaces
import "myapp/app/dtos"
type ProductService interface {
Create(req dtos.CreateProductRequest) (*dtos.ProductResponse, error)
FindAll(page, limit int) (*dtos.PaginatedProductResponse, error)
FindByID(id string) (*dtos.ProductResponse, error)
Update(id string, req dtos.UpdateProductRequest) (*dtos.ProductResponse, error)
Delete(id string) error
}Service Implementation
// app/services/product.service.go
package services
import (
"myapp/app/dtos"
"myapp/app/models"
repoInterfaces "myapp/app/repositories/interfaces"
svcInterfaces "myapp/app/services/interfaces"
)
type ProductServiceImpl struct {
repo repoInterfaces.ProductRepository
}
func NewProductService(repo repoInterfaces.ProductRepository) svcInterfaces.ProductService {
return &ProductServiceImpl{repo: repo}
}
func (s *ProductServiceImpl) Create(req dtos.CreateProductRequest) (*dtos.ProductResponse, error) {
product := &models.Product{
Name: req.Name,
Price: req.Price,
}
created, err := s.repo.Create(product)
if err != nil {
return nil, err
}
return dtos.ToProductResponse(created), nil
}
func (s *ProductServiceImpl) FindAll(page, limit int) (*dtos.PaginatedProductResponse, error) {
products, total, err := s.repo.FindAll(page, limit)
if err != nil {
return nil, err
}
return dtos.ToPaginatedProductResponse(products, total, page, limit), nil
}Architecture
The service sits between the controller and repository layers:
Controller --> Service --> Repository --> Database- Services receive repositories via dependency injection (constructor injection)
- Services accept DTOs as input and return DTOs as output
- Services convert between DTOs and models internally
- Business rules, validations, and data transformations belong in the service layer
Related
- gofasta g repository — generate the repository this service calls
- gofasta g controller — generate the controller that calls this service
- gofasta g dto — generate the DTOs this service uses
- gofasta g scaffold — generate all layers at once
Last updated on