# alextanhongpin/go-graphql-template architecture

> Architecture diagram of the GitHub repository alextanhongpin/go-graphql-template, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-02). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/alextanhongpin/go-graphql-template
- Repository: https://github.com/alextanhongpin/go-graphql-template

## Overview

##### 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.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Layer
    subgraph "Client Layer"
        Client("Client"):::client
    end

    %% API & Middleware Layer
    subgraph "API & Middleware Layer"
        API("GraphQL API Server"):::api
        Middleware("Middleware (CORS,DataLoader,Auth)"):::middleware
    end

    %% GraphQL Layer
    subgraph "GraphQL Layer"
        GraphQL("GraphQL Layer"):::graphql
    end

    %% Domain Layer
    subgraph "Domain Layer"
        Domain("Domain (Account & User)"):::domain
    end

    %% Data Access Layer
    subgraph "Data Access Layer"
        DataAccess("Data Access (sqlc Repos)"):::data
        Migrations("Migration Scripts"):::data
        DB("(PostgreSQL Database)"):::database
    end

    %% Supporting Services
    subgraph "Supporting Services"
        Observability("Observability (Logger/Tracer)"):::support
        Redis("Redis Cache"):::redis
    end

    %% Connections
    Client -->|"sendsRequest"| API
    API -->|"invokes"| Middleware
    API -->|"routesTo"| GraphQL
    Middleware -->|"processes"| GraphQL
    GraphQL -->|"resolves"| Domain
    Domain -->|"calls"| DataAccess
    DataAccess -->|"executes"| DB
    Migrations -->|"appliedTo"| DB
    API -->|"logs"| Observability
    Domain -->|"caches"| Redis

    %% Click Events
    click API "https://github.com/alextanhongpin/go-graphql-template/tree/master/cmd/server"
    click GraphQL "https://github.com/alextanhongpin/go-graphql-template/tree/master/external/graph"
    click Domain "https://github.com/alextanhongpin/go-graphql-template/tree/master/domain"
    click DataAccess "https://github.com/alextanhongpin/go-graphql-template/tree/master/internal/postgres/sql"
    click Migrations "https://github.com/alextanhongpin/go-graphql-template/tree/master/internal/postgres/migrations"
    click Middleware "https://github.com/alextanhongpin/go-graphql-template/tree/master/external/middleware"
    click Observability "https://github.com/alextanhongpin/go-graphql-template/blob/master/internal/logger.go"
    click Redis "https://github.com/alextanhongpin/go-graphql-template/blob/master/internal/redis.go"

    %% Styles
    classDef client fill:#AED6F1,stroke:#1F618D,stroke-width:2px;
    classDef api fill:#FAD7A0,stroke:#D35400,stroke-width:2px;
    classDef middleware fill:#F9E79F,stroke:#B7950B,stroke-width:2px;
    classDef graphql fill:#A9DFBF,stroke:#27AE60,stroke-width:2px;
    classDef domain fill:#F5B7B1,stroke:#C0392B,stroke-width:2px;
    classDef data fill:#D2B4DE,stroke:#8E44AD,stroke-width:2px;
    classDef database fill:#F9E79F,stroke:#D4AC0D,stroke-width:2px;
    classDef support fill:#AED6F1,stroke:#1ABC9C,stroke-width:2px;
    classDef redis fill:#F4D03F,stroke:#D68910,stroke-width:2px;
```
