Proof of Concept (PoC) showcasing both FastAPI for REST and Strawberry for GraphQL. This example demonstrates how you can implement both approaches in a single project.
- Ensure you have Python 3.11 or higher installed.
-
Clone the repository:
https://github.com/dpedwards/python-graphql-fastapi.git
-
Change into the project directory:
cd python-graphql-fastapi
-
Create and activate a virtual environment:
python -m venv venv
source venv/bin/activate
venv\Scripts\activate
-
Install dependencies:
pip install -r requirements.txt
Build:
docker compose up
Rebuild (optional):
docker compose down
docker compose up --build
Clean (optional):
docker system prune -a --volumes
Run the API server with the following command:
uvicorn main:app --reload
Visit http://localhost:8000/docs in your browser to access the Swagger documentation and explore available API endpoints.
Visit http://localhost:8000/graphql in your browser to access the GraphQL Playground.
query MyQuery {
getHeaders {
active
buyerId
headerId
name
salesRepId
lines {
creationDate
headerId
itemId
lineId
marketId
name
items {
description
itemId
name
}
markets {
location
marketId
name
}
}
salesRep {
resourceId
salesRepId
resource {
name
resourceId
}
}
buyers {
buyerId
name
}
}
}
{
"data": {
"getHeaders": [
{
"active": "Y",
"buyerId": 1003,
"headerId": 1,
"name": "Header Name 1",
"salesRepId": 102,
"lines": {
"creationDate": "2023-01-01",
"headerId": 1,
"itemId": 701,
"lineId": 55001,
"marketId": 2201,
"name": "Line Name 1",
"items": {
"description": "gold",
"itemId": 701,
"name": "gold"
},
"markets": {
"location": "Europe",
"marketId": 2201,
"name": "NotSo Wet Market"
}
},
"salesRep": {
"resourceId": 12,
"salesRepId": 102,
"resource": {
"name": "Jaba Maba",
"resourceId": 12
}
},
"buyers": {
"buyerId": 1003,
"name": "Simon Sims"
}
},
{
"active": "Y",
"buyerId": 1002,
"headerId": 2,
"name": "Header Name 2",
"salesRepId": 105,
"lines": {
"creationDate": "2022-01-05",
"headerId": 2,
"itemId": 701,
"lineId": 55003,
"marketId": 2203,
"name": "Line Name 3",
"items": {
"description": "gold",
"itemId": 701,
"name": "gold"
},
"markets": {
"location": "Africa",
"marketId": 2203,
"name": "Rainy Days Market"
}
},
"salesRep": {
"resourceId": 15,
"salesRepId": 105,
"resource": {
"name": "Viper Song",
"resourceId": 15
}
},
"buyers": {
"buyerId": 1002,
"name": "Bobby DropTable"
}
},
]
}
}
mutation MyMutation {
updateHeader(header: {name: "Mutation Update", headerId: 6, salesRepId: 1, buyerId: 2, active: "yes"}) {
name
salesRepId
buyerId
active
}
}
Result:
{
"data": {
"updateHeader": {
"name": "Mutation Update",
"salesRepId": 1,
"buyerId": 2,
"active": "yes"
}
}
}
mutation MyMutation {
createHeader(
header: {headerId: 7, name: "TEST", buyerId: 1002, active: "Y", salesRepId: 105}
) {
name
salesRepId
buyerId
active
}
}
Result:
{
"data": {
"createHeader": {
"name": "TEST",
"salesRepId": 105,
"buyerId": 1002,
"active": "Y"
}
}
}