A simple GraphQL API built with FastAPI and Strawberry for managing items in a SQLite database.
- Query all items or fetch a specific item by ID
- Create new items with name and price
- GraphQL playground for testing queries and mutations
- SQLite database for persistent storage
- Python 3.8+
- pip or uv package manager
- Clone or download this project
- Install dependencies:
Or using uv:
pip install -r requirements.txt
uv add -r requirements.txt
Before running the app, initialize the database:
python setup_db.pyThis script:
- Creates the SQLite database file (
items.db) - Creates the
itemstable with columns:id,name, andprice - Populates the database with sample data (Apple and Banana items)
Note: Running this script multiple times will not overwrite existing data (uses CREATE TABLE IF NOT EXISTS), so it's safe to run multiple times.
Start the FastAPI server:
python main.pyThe server will run on http://localhost:8000
Access the GraphQL playground at:
http://localhost:8000/graphql
Get all items:
query {
items {
id
name
price
}
}Get a specific item by ID:
query {
item(id: 1) {
id
name
price
}
}Create a new item:
mutation {
createItem(name: "Laptop", price: 999.99) {
id
name
price
}
}app.py- GraphQL schema and resolversmain.py- FastAPI application entry pointsetup_db.py- Database initialization scriptrequirements.txt- Python dependenciespyproject.toml- Project configuration
The app uses SQLite with the database file items.db. The items table has the following structure:
| Column | Type | Description |
|---|---|---|
| id | INTEGER | Primary key |
| name | TEXT | Item name |
| price | REAL | Item price |
- fastapi - Web framework
- strawberry-graphql - GraphQL implementation
- uvicorn - ASGI server
- sqlite3 - Database (built-in)