-
Notifications
You must be signed in to change notification settings - Fork 1
Backend Development
Ethan Cavill edited this page Jul 9, 2024
·
1 revision
This guide provides an overview of the technologies used in our backend, their structure, and the development cycle. The goal is to help you understand the architecture and workflow of our backend development.
- GraphQL (Strawberry): Main API endpoint for front-end data querying.
- FastAPI: High-performance framework for APIs, running inside a Docker container.
- Redis: Caching and session management.
- Session-Based Authentication: Securely manages user sessions.
- SQLModel (SQLAlchemy Abstraction): Simplifies interactions with the Postgres database.
- Postgres: Primary database, running in a Docker instance.
- Amazon S3: File storage service.
- Docker: Provides isolated development environments ensuring consistency.
-
Create Model (SQLModel)
- Define data models using
SQLModel. - Example:
from sqlmodel import SQLModel, Field class User(SQLModel, table=True): id: int = Field(default=None, primary_key=True) name: str email: str
- Define data models using
-
Add to Schema (Strawberry)
- Create schema definitions using Strawberry.
- Example:
import strawberry from models import User as UserModel @strawberry.type class User: id: int name: str email: str
-
Define Input Types
- Define input types for mutations.
- Example:
@strawberry.input class UserInput: name: Optional[str] = None email: Optional[str] = None
-
Write Queries and Mutations
- Define queries and mutations in Strawberry.
- Example Mutation:
@strawberry.type class Mutation: @strawberry.mutation def create_user(self, input: UserInput) -> User: user = UserModel(name=input.name, email=input.email) # Save user to the database return user
- Example Query:
@strawberry.type class Query: @strawberry.field def get_user(self, id: int) -> User: # Fetch user from the database return user
-
Run Migrations
- Use Alembic for database migrations.
- Commands to run inside the backend Docker instance:
alembic revision --autogenerate -m "Description of changes" alembic upgrade head - To downgrade:
alembic downgrade -1
-
Common Issues:
- Multiple Heads: This usually indicates an issue such as improper downgrades or references. Resolve by ensuring proper downgrade and reference management.
-
Version Mismatch: If you encounter a version mismatch error, ensure the database is in sync with the latest migration files:
alembic current # Check current version alembic heads # Check latest versions
- Downgrading from Feature Branch: Be cautious when downgrading from a feature branch migration before checking out master. Ensure your migrations are clean and synced before switching branches.
-
Code Generation (GraphQL Codegen)
- Use GraphQL Codegen to generate frontend types based on your schemas.
- Run codegen to generate types automatically.
-
Test on Frontend
- Ensure frontend integrates seamlessly with the backend API.
- Validate GraphQL API endpoints and mutations.
- Backend: Access the API at http://localhost/api
- GraphQL Testing Playground: Use the GraphQL testing playground available at http://localhost/api/graphql
This guide provides a succinct overview of our backend development architecture and workflow. For detailed implementation, please refer to the specific files and documentation within the repository. Happy coding!