How strawberry-graphql/strawberry-sqlalchemy works
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.