# strawberry-graphql/strawberry-sqlalchemy architecture

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

- Interactive diagram: https://gitdiagram.com/strawberry-graphql/strawberry-sqlalchemy
- Repository: https://github.com/strawberry-graphql/strawberry-sqlalchemy

## Overview

##### Main Components
- Core Library (src/strawberry_sqlalchemy_mapper): This part encapsulates all the functionality that maps SQLAlchemy models to Strawberry types. It includes modules for handling fields, relationships, ordering, scalars, and error handling.
- Integration Layer: The library is intended to plug seamlessly into a GraphQL API setup. The README shows examples where a developer creates a custom GraphQLView and a Query type that relies on the auto-generated Strawberry types. This layer interacts with the SQLAlchemy session (and by extension, the database) and the Strawberry schema.
- External Dependencies: The operation of this library depends on external frameworks such as SQLAlchemy (for ORM) and Strawberry (for GraphQL API). Additionally, PostgreSQL is mentioned as a prerequisite, indicating that database interactions are part of the overall environment.
- Testing and Benchmarking: Located in the tests directory, these components ensure that the mapper works as expected, and benchmark scripts provide insight into performance.
- CI/CD and Build Infrastructure: With CI workflows, Docker configurations (in .devcontainer and .github folders), pre-commit hooks, and multiple configuration files (e.g., pyproject.toml, setup.py, noxfile.py), the project includes robust automation for testing, quality assurance, and deployment.

##### Relationships and Interactions
- The core mapping engine (inside src/) receives SQLAlchemy models via the decorator (@strawberry_sqlalchemy_mapper.type and .interface). It analyzes the models’ fields, relationships, association proxies, and hybrid properties.
- There is a clear connection from SQLAlchemy models to the auto-generated Strawberry types. Once the mapping is complete (triggered by the finalize() call), the generated types and interfaces are incorporated into the Strawberry GraphQL schema.
- The GraphQL API layer (as shown in the README’s GraphQLView example) depends on the mapping outcome and then passes the GraphQL queries to the underlying SQLAlchemy session, which in turn interacts with the database (e.g., PostgreSQL). This represents the data flow from the database to the GraphQL API.
- The CI/CD structure (GitHub Actions, Docker configurations) ensures that as code changes, automated tests and linting (via pre-commit and CI workflows) validate the integration points and maintain a high code quality standard.

##### Architectural Patterns and Design Principles
- Plugin/Decorator Pattern: The library leverages decorators to enhance types and interfaces, making it easy to attach mapping functionality directly onto Python classes.
- Separation of Concerns: The project separates core mapping logic (src/) from configuration/testing infrastructure (tests/, .github/, Dockerfiles) and documentation (docs/), ensuring that each component’s responsibilities are clear.
- Integration with External Tools: Leveraging GitHub Actions for continuous integration, Docker for containerized development environments, and pre-commit hooks for code quality enforces a modern, DevOps-aware development approach.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Runtime Components"
        SQLModels["SQLAlchemy Models"]:::external
        subgraph "Core Mapping Engine"
            CoreME["Core Mapping Engine"]:::core
            Field["Field Processing: Field"]:::core
            Loader["Loader"]:::core
            Mapper["Mapper"]:::core
            Ordering["Ordering"]:::core
            Relay["Relay"]:::core
            Scalars["Scalars"]:::core
            Utils["Utils"]:::core
            Exc["Error Handling: Exc"]:::core
        end
        GraphQL["Strawberry GraphQL API"]:::integration
        Session["SQLAlchemy Session"]:::integration
        Database["PostgreSQL Database"]:::database
        ExtSQLAlchemy["SQLAlchemy ORM"]:::external
        ExtStrawberry["Strawberry GraphQL Library"]:::external
        Documentation["Documentation & Examples (README)"]:::documentation
    end

    subgraph "Development & Automation"
        Testing["Testing & Benchmarking"]:::dev
        subgraph "CI/CD Pipeline"
            GH["GitHub Actions (.github)"]:::dev
            Docker["Docker Configs (.devcontainer)"]:::dev
            Nox["noxfile.py"]:::dev
            PyProject["pyproject.toml"]:::dev
            Setup["setup.py"]:::dev
            Tox["tox.ini"]:::dev
        end
    end

    SQLModels -->|"DecoratorRegistration"| CoreME
    CoreME -->|"SchemaFinalization"| GraphQL
    GraphQL -->|"ExecutesQuery"| Session
    Session -->|"DBInteraction"| Database

    SQLModels ---|"uses"| ExtSQLAlchemy
    GraphQL ---|"uses"| ExtStrawberry
    Documentation ---|"exampleUsage"| GraphQL

    Testing ---|"ensuresQuality"| CoreME
    Testing ---|"ensuresQuality"| GraphQL
    GH ---|"triggers"| Testing
    Docker ---|"setsUp"| GH

    click CoreME "https://github.com/strawberry-graphql/strawberry-sqlalchemy/tree/main/src/strawberry_sqlalchemy_mapper"
    click Field "https://github.com/strawberry-graphql/strawberry-sqlalchemy/blob/main/src/strawberry_sqlalchemy_mapper/field.py"
    click Loader "https://github.com/strawberry-graphql/strawberry-sqlalchemy/blob/main/src/strawberry_sqlalchemy_mapper/loader.py"
    click Mapper "https://github.com/strawberry-graphql/strawberry-sqlalchemy/blob/main/src/strawberry_sqlalchemy_mapper/mapper.py"
    click Ordering "https://github.com/strawberry-graphql/strawberry-sqlalchemy/blob/main/src/strawberry_sqlalchemy_mapper/ordering.py"
    click Relay "https://github.com/strawberry-graphql/strawberry-sqlalchemy/blob/main/src/strawberry_sqlalchemy_mapper/relay.py"
    click Scalars "https://github.com/strawberry-graphql/strawberry-sqlalchemy/blob/main/src/strawberry_sqlalchemy_mapper/scalars.py"
    click Utils "https://github.com/strawberry-graphql/strawberry-sqlalchemy/blob/main/src/strawberry_sqlalchemy_mapper/utils.py"
    click Exc "https://github.com/strawberry-graphql/strawberry-sqlalchemy/blob/main/src/strawberry_sqlalchemy_mapper/exc.py"

    click Documentation "https://github.com/strawberry-graphql/strawberry-sqlalchemy/blob/main/README.md"

    click Testing "https://github.com/strawberry-graphql/strawberry-sqlalchemy/tree/main/tests"
    click GH "https://github.com/strawberry-graphql/strawberry-sqlalchemy/blob/main/.github"
    click Docker "https://github.com/strawberry-graphql/strawberry-sqlalchemy/blob/main/.devcontainer"
    click Nox "https://github.com/strawberry-graphql/strawberry-sqlalchemy/blob/main/noxfile.py"
    click PyProject "https://github.com/strawberry-graphql/strawberry-sqlalchemy/blob/main/pyproject.toml"
    click Setup "https://github.com/strawberry-graphql/strawberry-sqlalchemy/blob/main/setup.py"
    click Tox "https://github.com/strawberry-graphql/strawberry-sqlalchemy/blob/main/tox.ini"

    classDef core fill:#ffeb3b,stroke:#fbc02d,stroke-width:2px;
    classDef integration fill:#bbdefb,stroke:#64b5f6,stroke-width:2px;
    classDef external fill:#c8e6c9,stroke:#388e3c,stroke-width:2px;
    classDef database fill:#ffe0b2,stroke:#ff9800,stroke-width:2px;
    classDef documentation fill:#d1c4e9,stroke:#7e57c2,stroke-width:2px;
    classDef dev fill:#ffcdd2,stroke:#e57373,stroke-width:2px;
```
