How dpedwards/python-graphql-fastapi works
Project Type & Purpose
• This is a backend API project that demonstrates two approaches to API design: REST (via FastAPI) and GraphQL (via Strawberry).
• The PoC aims to show how both paradigms can coexist within a single application.
• Key technologies include FastAPI for REST, Strawberry for GraphQL, SQLAlchemy for database interactions, and Docker for containerization.
Analyzing the File Structure
• Top-level structure includes:
• Dockerfile and docker-compose.yml for container-based deployment.
• main.py, which is most likely the entry point for the application.
• requirements.txt for Python dependencies.
• A structured app/ directory hosting submodules:
– app/context.py (likely handles application-wide context/settings)
– app/database/ which includes initialization, the actual database handling (database.py), and an example environment file for configuration.
– app/graphql/ that defines the GraphQL schema and type definitions.
– app/models/ containing ORM models and possibly associated schemas.
– app/routers/ with routing definitions for the REST endpoints.
Extracting Insights from the README
• The README clearly contrasts REST and GraphQL approaches, showing that the application offers two ways for clients to interact with the API.
• It provides instructions to run both the Dockerized version and with Uvicorn directly, stressing the importance of both deployment and development setups.
a. Main Components:
• Client/UI Layer: This includes external clients (browsers, API consumers) interacting with the API.
• API Gateway / Application Server:
▪ FastAPI Main Application: Entry script (main.py) launching the service with Uvicorn.
▪ REST Endpoints: Defined in app/routers/ (FastAPI-dependent routing leading to Swagger docs at /docs).
▪ GraphQL Endpoint: Defined in app/graphql/ (the single endpoint at /graphql).
• Business Logic & Data Models:
▪ app/models/ for ORM models and business schema definitions.
▪ app/context.py for application-level configuration.
• Data Access Layer:
▪ app/database/ which handles database connectivity, connection objects, and environment configurations (leveraging SQLAlchemy).
• Persistence:
▪ The database (SQLite as indicated by test.db, with hints towards PostgreSQL/MySQL given in the README badges).
• Deployment & Containerization:
▪ Dockerfile and docker-compose.yml representing how the application is containerized and deployed.
b. Relationships and Interactions:
▪ Client calls hit the API server.
▪ The API server differentiates requests:
• Requests to /docs (Swagger) and REST endpoints are routed through FastAPI’s routers.
• Requests to /graphql are handled by Strawberry GraphQL, mapping queries/mutations to defined resolvers.
▪ Both routing implementations interact with the business logic (app/models, context settings) and further communicate with the database.
▪ Use Docker to package and deploy the entire setup, ensuring separation of concerns and consistent environment.
c. Architectural Patterns & Design Principles:
• Separation of concerns: clearly divided directories (routers for endpoints, models for data entities, and database for connection details).
• API Gateway Pattern: A single API service that bifurcates into REST and GraphQL based on endpoint routing.
• Containerization: Using Docker to encapsulate all components.
• Modular design: The project is modularly divided into contexts, models, routers, and GraphQL, which eases scaling and modifications.
• FastAPI (for REST),
• Strawberry GraphQL (for GraphQL support),
• Uvicorn (as the ASGI server),
• SQLAlchemy (for ORM and database interactions),
• Docker (for containerization),
• Possibly PostgreSQL/SQLite/MySQL as backends (as per badges and test.db).
Specific Elements
• "Client (Browser/API Consumer)"
• "FastAPI with REST Routes" (linked to /docs and other endpoints)
• "Strawberry GraphQL Service" (linked to /graphql)
• "Business Logic & Models (app/models, app/context.py)"
• "Database Access (app/database)"
• "Database (SQLite/PostgreSQL/MySQL)"
• "Docker Containerization" to indicate deployment housing.
• Data flow from client -> API server -> business logic -> Database.
• That both REST and GraphQL share the business logic layer.