This section covers building the backend API server using Golang with both REST and GraphQL endpoints.
- Go 1.21: Modern Go version
- Gin: HTTP web framework
- GORM: ORM library for database operations
- PostgreSQL: Relational database
- GraphQL-Go: GraphQL implementation
- UUID: Unique identifier generation
backend/
├── internal/
│ ├── api/
│ │ ├── rest/ # REST API handlers
│ │ ├── graphql/ # GraphQL schema and resolvers
│ │ └── routes.go # Route setup
│ ├── config/ # Configuration management
│ ├── database/ # Database connection and setup
│ └── models/ # Data models
├── go.mod # Go module definition
└── main.go # Application entry point
- RESTful API endpoints
- GraphQL API with schema-first approach
- Shared business logic
- PostgreSQL with GORM
- Auto-migration support
- Connection pooling
- Separation of concerns
- Dependency injection
- Environment-based configuration
GET /api/health # Health check
GET /api/users # List all users
POST /api/users # Create new user
GET /api/users/:id # Get user by ID
PUT /api/users/:id # Update user
DELETE /api/users/:id # Delete user
POST /graphql # GraphQL endpoint
GET /graphql # GraphiQL playground
# Install dependencies
cd backend && go mod tidy
# Run development server
go run main.go
# Build binary
go build -o bin/server main.go
# Run tests
go test ./...DATABASE_URL=postgres://user:password@localhost:5432/tutorial_db?sslmode=disable
PORT=8080
ENVIRONMENT=developmenttype User {
id: ID!
name: String!
email: String!
created_at: String!
updated_at: String!
}
type Query {
users: [User!]!
user(id: ID!): User
health: Health!
}
type Mutation {
addUser(input: UserInput!): User!
updateUser(id: ID!, input: UserInput!): User!
deleteUser(id: ID!): Boolean!
}Continue to Docker Configuration to learn about containerizing the application.