Skip to Content

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

FlagDefaultDescription
--graphqlfalseAlso generate GraphQL schema and wire up the resolver
--gqlfalseAlias for --graphql

Examples

Generate a service with all CRUD methods:

gofasta g service Product name:string price:float

Generate a service with GraphQL support:

gofasta g service Product name:string price:float --graphql

What It Generates

Running gofasta g service Product name:string price:float creates the following files:

FileDescription
app/models/product.model.goGORM database model
db/migrations/000006_create_products.up.sqlCreate table migration
db/migrations/000006_create_products.down.sqlDrop table migration
app/repositories/interfaces/product_repository.goRepository interface
app/repositories/product.repository.goRepository implementation
app/services/interfaces/product_service.goService interface
app/services/product.service.goService implementation
app/dtos/product.dtos.goRequest/response DTOs
app/di/providers/product.goWire DI provider set

It also patches 2 existing files:

FileChange
app/di/container.goAdds ProductService field
app/di/wire.goAdds ProductSet to the Wire build

With --graphql, the service generator additionally:

  • Generates a GraphQL schema file (.gql)
  • Patches resolver.go to 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
Last updated on