# sdshah09/go-graphql architecture

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

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

## Overview

##### Project Type and Purpose
- This is a backend GraphQL service written in Go, using gqlgen, that offers a single API layer with dual-database support (PostgreSQL and MongoDB) and includes logging (Fluent-Bit) and containerization (Docker/Docker-Compose).
- Its primary role is to expose CRUD operations on Patient entities via GraphQL, routing each request to the appropriate database based on an input flag.

##### File Structure Analysis
- Top level:
- server.go: entry point and HTTP server initialization.
- gqlgen.yml + graph/: GraphQL schema, generated models, resolvers.
- config/: centralized environment and app configuration.
- utils/: two subpackages (postgres, mongodb) encapsulating each database’s connection and CRUD logic.
- docker-compose.yml + Dockerfile: container orchestration.
- fluent-bit/: sidecar log collector configuration.
- tools.go, go.mod/go.sum: code tools and dependency management.

##### README Insights
- Describes features (dual DB support, GraphQL playground).
- Emphasizes environment-based configuration (.env) for database URIs.
- Points out code generation step via gqlgen.
- Illustrates database schemas and sample GraphQL queries/mutations.

a. Main Components:
- Client / GraphQL Playground (HTTP clients / frontend)
- GraphQL API Layer (server.go + gqlgen + resolvers)
- Configuration Manager (config package reading .env)
- Business Logic / Resolver Layer (graph/resolver.go)
- Database Adapter Layer (utils/postgres + utils/mongodb)
- Data Stores: PostgreSQL, MongoDB
- Logging Pipeline: Fluent-Bit sidecar, Docker logs folder
- Container Runtime: Docker Engine + Docker-Compose orchestration

b. Relationships & Data Flow:
1. Client → HTTP POST/GET → GraphQL API
2. GraphQL API → Resolver Logic → Database Adapter
3. Database Adapter → SQL driver or Mongo driver → Data Store
4. All components emit logs → Docker log driver → Fluent-Bit → target sink (e.g., stdout, external aggregator)
5. Configuration Manager feeds DB credentials & server port into API and adapters

c. Architectural Patterns & Principles:
- Layered architecture (API → service/resolver → data access layer)
- Dependency Injection via config structs
- Code generation (gqlgen) for type safety in schema
- Single-responsibility: each package has a clear concern

d. Key Technologies & Frameworks:
- Go modules, gqlgen for GraphQL
- PostgreSQL (via utils/postgres)
- MongoDB (via utils/mongodb)
- Docker & Docker-Compose for containerization
- Fluent-Bit for log aggregation

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Client Layer"
        Client["Client / GraphQL Playground"]
    end

    subgraph "Configuration Layer"
        Config["config/config.go"]:::config
    end

    subgraph "API Layer"
        Server["server.go"]:::api
        GQLConfig["gqlgen.yml"]:::api
        SchemaDef["graph/schema.graphqls"]:::api
        GenCode["graph/generated.go"]:::code
        ModelsGen["graph/model/models_gen.go"]:::code
    end

    subgraph "Resolver Layer"
        ResolverImpl["graph/resolver.go"]:::api
        SchemaRes["graph/schema.resolvers.go"]:::api
    end

    subgraph "Adapter Layer"
        PGUtils["utils/postgres/utils.go"]:::api
        PGTest["utils/postgres/testdb.go"]:::api
        MongoUtils["utils/mongodb/utils.go"]:::api
    end

    subgraph "Data Stores"
        Postgres["PostgreSQL (5432)"]:::datastore
        Mongo["MongoDB (27017)"]:::datastore
    end

    subgraph "Container Orchestration"
        Dockerfile["Dockerfile"]:::infra
        Compose["docker-compose.yml"]:::infra
        Tools["tools.go"]:::infra
        GoMod["go.mod"]:::infra
        GoSum["go.sum"]:::infra
    end

    subgraph "Logging Pipeline"
        FluentConf["fluent-bit/conf/fluent-bit.conf"]:::logging
        Parsers["fluent-bit/conf/parsers.conf"]:::logging
        DockerLog["logs/docker.log"]:::logging
    end

    Client -->|"GraphQL over HTTP"| Server
    Server -->|"reads config"| Config
    Server -->|"uses schema"| SchemaDef
    Server -->|"gqlgen config"| GQLConfig
    SchemaDef --> GenCode
    GenCode --> ModelsGen
    Server --> ResolverImpl
    ResolverImpl --> SchemaRes
    ResolverImpl --> PGUtils
    ResolverImpl --> MongoUtils
    Config --> PGUtils
    Config --> MongoUtils
    PGUtils -->|"SQL queries"| Postgres
    MongoUtils -->|"Mongo CRUD"| Mongo
    Server -.->|"JSON logs"| DockerLog
    DockerLog --> FluentConf
    FluentConf --> Parsers
    Compose --> Server
    Compose --> Postgres
    Compose --> Mongo
    Compose --> FluentConf

    click Server "https://github.com/sdshah09/go-graphql/blob/master/server.go"
    click GQLConfig "https://github.com/sdshah09/go-graphql/blob/master/gqlgen.yml"
    click SchemaDef "https://github.com/sdshah09/go-graphql/blob/master/graph/schema.graphqls"
    click GenCode "https://github.com/sdshah09/go-graphql/blob/master/graph/generated.go"
    click ModelsGen "https://github.com/sdshah09/go-graphql/blob/master/graph/model/models_gen.go"
    click ResolverImpl "https://github.com/sdshah09/go-graphql/blob/master/graph/resolver.go"
    click SchemaRes "https://github.com/sdshah09/go-graphql/blob/master/graph/schema.resolvers.go"
    click Config "https://github.com/sdshah09/go-graphql/blob/master/config/config.go"
    click PGUtils "https://github.com/sdshah09/go-graphql/blob/master/utils/postgres/utils.go"
    click PGTest "https://github.com/sdshah09/go-graphql/blob/master/utils/postgres/testdb.go"
    click MongoUtils "https://github.com/sdshah09/go-graphql/blob/master/utils/mongodb/utils.go"
    click Dockerfile "https://github.com/sdshah09/go-graphql/tree/master/Dockerfile"
    click Compose "https://github.com/sdshah09/go-graphql/blob/master/docker-compose.yml"
    click FluentConf "https://github.com/sdshah09/go-graphql/blob/master/fluent-bit/conf/fluent-bit.conf"
    click Parsers "https://github.com/sdshah09/go-graphql/blob/master/fluent-bit/conf/parsers.conf"
    click DockerLog "https://github.com/sdshah09/go-graphql/blob/master/logs/docker.log"
    click Tools "https://github.com/sdshah09/go-graphql/blob/master/tools.go"
    click GoMod "https://github.com/sdshah09/go-graphql/blob/master/go.mod"
    click GoSum "https://github.com/sdshah09/go-graphql/blob/master/go.sum"

    classDef api fill:#cfe2ff,stroke:#333,stroke-width:1px
    classDef config fill:#FFDAB9,stroke:#333,stroke-width:1px
    classDef datastore fill:#d5f5e3,stroke:#333,stroke-width:1px
    classDef logging fill:#d3d3d3,stroke:#333,stroke-width:1px
    classDef infra fill:#fff3cd,stroke:#333,stroke-width:1px
    classDef code fill:#e6e6fa,stroke:#333,stroke-width:1px
```
