Skip to content

Commit ad90890

Browse files
authored
Merge pull request #17 from rodrigofeijao/feat/add-fastapi-example
Add fastapi basic example
2 parents ed36244 + 1881831 commit ad90890

File tree

12 files changed

+1028
-2
lines changed

12 files changed

+1028
-2
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1-
# examples
1+
# Examples
22

3-
Example on how to use Strawberry
3+
Examples on how to use Strawberry:
4+
5+
1. [Django Subscriptions](/django-subscriptions)
6+
2. [Django Subscriptions with RXDB](/django-subscriptions-rxdb/)
7+
3. [FastAPI](/fastapi/)
8+
4. [FastAPI with SQL-Alchemy](/fastapi-sqlalchemy)

fastapi/.projectroot

Whitespace-only changes.

fastapi/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Example of a GraphQL API using Strawberry and FastAPI
2+
3+
This examples shows you how to setup Strawberry with FastAPI
4+
5+
## How to use
6+
7+
1. Install dependencies
8+
9+
Use [poetry](https://python-poetry.org/) to install dependencies:
10+
11+
```bash
12+
poetry install
13+
```
14+
15+
2. Run the server
16+
17+
Run [uvicorn](https://www.uvicorn.org/) to run the server:
18+
19+
```bash
20+
poetry run uvicorn main:app --reload
21+
```
22+
23+
3. Access the GraphiQL IDE and explore the schema at [http://localhost:8000/graphql](http://localhost:8000/graphql)
24+
25+
## Example query
26+
27+
```graphql
28+
query AllTopRatedMovies {
29+
topRatedMovies {
30+
imageUrl
31+
imdbId
32+
imdbRating
33+
imdbRatingCount
34+
title
35+
year
36+
}
37+
}
38+
```

fastapi/api/__init__.py

Whitespace-only changes.

fastapi/api/definitions/__init__.py

Whitespace-only changes.

fastapi/api/definitions/movie.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import strawberry
2+
3+
4+
@strawberry.type
5+
class Movie:
6+
imdb_id: str = strawberry.field(description="This is the IMDB ID of the movie")
7+
title: str
8+
year: int
9+
image_url: str
10+
imdb_rating: float
11+
imdb_rating_count: str

fastapi/api/schema.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from typing import List
2+
3+
import json
4+
import strawberry
5+
6+
from .definitions.movie import Movie
7+
8+
9+
@strawberry.type
10+
class Query:
11+
@strawberry.field
12+
def top_rated_movies(self, info, limit: int = 250) -> List[Movie]:
13+
with open("../common-data/movies.json", "r") as file:
14+
data = json.load(file)
15+
16+
result = []
17+
18+
for i in range(limit):
19+
result.append(
20+
Movie(
21+
imdb_id=data[i]["imdb_id"],
22+
title=data[i]["title"],
23+
year=data[i]["year"],
24+
image_url=data[i]["image_url"],
25+
imdb_rating=data[i]["imdb_rating"],
26+
imdb_rating_count=data[i]["imdb_rating_count"],
27+
)
28+
)
29+
30+
return result
31+
32+
33+
schema = strawberry.Schema(Query)

fastapi/main/__init__.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from fastapi import FastAPI, Request
2+
from fastapi.templating import Jinja2Templates
3+
from fastapi.responses import HTMLResponse
4+
from strawberry.asgi import GraphQL
5+
6+
from api.schema import schema
7+
8+
app = FastAPI()
9+
10+
templates = Jinja2Templates(directory="templates")
11+
12+
13+
@app.get("/", response_class=HTMLResponse)
14+
async def render_index_page(request: Request):
15+
return templates.TemplateResponse("index.html", {"request": request})
16+
17+
18+
app.mount("/graphql", GraphQL(schema))

fastapi/poetry.lock

Lines changed: 882 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

fastapi/pyproject.toml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
[tool.poetry]
2+
name = "strawberry-graphql-fastapi-example"
3+
version = "0.1.0"
4+
description = "Example of a GraphQL API using Strawberry and FastAPI"
5+
authors = ["Rodrigo Feijao <[email protected]>"]
6+
7+
[tool.poetry.dependencies]
8+
python = "^3.11"
9+
uvicorn = {extras = ["standard"], version = "^0.24.0"}
10+
strawberry-graphql = {extras = ["asgi"], version = "^0.214.0"}
11+
fastapi = "^0.104.1"
12+
13+
14+
[tool.poetry.dev-dependencies]
15+
black = "^23.11"
16+
mypy = "^1.7.0"
17+
ruff = "^0.0.47"
18+
19+
[build-system]
20+
requires = ["poetry-core>=1.8.0"]
21+
build-backend = "poetry.core.masonry.api"
22+
23+
[tool.ruff]
24+
line-length = 88
25+
select = [
26+
"F401",
27+
"F403",
28+
"E501",
29+
]

0 commit comments

Comments
 (0)