How alextanhongpin/go-graphql-template works
Main Components
• API Server: Highlight the entry point of the service, represented by the main files in the cmd/server directory (main.go, wire.go, wire_gen.go). This is where the HTTP server for GraphQL is spun up.
• GraphQL Layer: Focus on the external/graph directory, which includes schema definitions, resolvers, queries, and mutations. This layer maps incoming GraphQL queries into calls within the business logic.
• Domain Layer: Include the domain folder, which consists of subdirectories for business entities such as account and user. The domain layer encapsulates the core business logic, validations, and service methods.
• Data Access Layer: Represent the repository components that interact with the database. Files generated by sqlc in the internal/postgres/sql and related migration files indicate that this layer interacts with a PostgreSQL database.
• Middleware & Infrastructure: Incorporate components from external/middleware (for CORS, DataLoader, authz, etc.) and external/security/session for security and session handling. Also, include internal elements like logger, tracer, and validator modules.
• Supporting Services: Include auxiliary components like redis (internal/redis.go) for caching, and external packages (pkg/logger, pkg/pageview) that provide shared utilities.
Relationships and Interactions
• The GraphQL Layer (processing the schema, resolving queries/mutations).
• Middleware chains for authentication, logging, and data loading.
• The GraphQL resolvers should call into the Domain Layer’s services (located in domain/account and domain/user).
• The Domain Layer, in turn, interacts with the Data Access Layer. This involves SQL operations defined in the repository files that communicate with the PostgreSQL database.
Architectural Patterns and Design Principles
• Separation of concerns with a layered architecture: API/HTTP Layer, GraphQL Layer, Domain Business Logic, and Data Access.
• Use of dependency injection (hinted by wire.go and wire_gen.go) to manage component interconnections in a clean, testable manner.
• Use of middleware for cross-cutting concerns (authorization, CORS, DataLoader) that intercept requests and responses.
Incorporate Relevant Technologies and Frameworks
• Go language as the primary runtime.
• GraphQL libraries and schema management (as provided in external/graph/schema).
• sqlc for repository code generation.
• PostgreSQL as the primary database along with migration scripts in internal/postgres/migrations.
• Redis for caching (internal/redis.go) and middleware utilities.
• Logging/tracing integrated with Jaeger (mentioned in the README).
Elements
• Use clear labels to identify each component such as “GraphQL Server”, “Middleware (CORS, DataLoader, Auth)”, “Domain – Account & User”, “Repository/Data Access”, “PostgreSQL Database”, “Redis/Caching”, and “Observability Components (Logging/Tracing)”.
• Optionally, group the components into layers (presentation/API, business/domain, persistence) with clearly demarcated boundaries.
• Additionally, add “Logger/Tracer” components (internal/logger, internal/tracer, pkg/logger) wired into the flow.