Skip to content

Commit 9f24986

Browse files
committed
feat: Add fastapi simple example
1 parent ed36244 commit 9f24986

12 files changed

+1081
-2
lines changed

README.md

+7-2
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/.flake8

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[flake8]
2+
max-line-length = 89
3+
exclude=.venv,.git
4+
ignore = W503
5+
extend-ignore =
6+
# See https://github.com/PyCQA/pycodestyle/issues/373
7+
E203,

fastapi/.projectroot

Whitespace-only changes.

fastapi/README.md

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
id
31+
imageUrl
32+
imdbId
33+
imdbRating
34+
imdbRatingCount
35+
title
36+
year
37+
director {
38+
id
39+
name
40+
}
41+
}
42+
}
43+
```

fastapi/api/__init__.py

Whitespace-only changes.

fastapi/api/definitions/__init__.py

Whitespace-only changes.

fastapi/api/definitions/movie.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import strawberry
2+
3+
4+
@strawberry.type
5+
class Movie:
6+
id: int
7+
imdb_id: str
8+
title: str
9+
year: int
10+
image_url: str
11+
imdb_rating: float
12+
imdb_rating_count: str

fastapi/api/schema.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from typing import List
2+
3+
import strawberry
4+
5+
from .definitions.movie import Movie
6+
7+
8+
@strawberry.type
9+
class Query:
10+
@strawberry.field
11+
# TODO: Add a resolver for this field
12+
def top_rated_movies(self, info, limit: int = 250) -> List[Movie]:
13+
return []
14+
15+
16+
schema = strawberry.Schema(Query)

fastapi/main/__init__.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from fastapi import FastAPI
2+
from strawberry.asgi import GraphQL
3+
4+
from api.schema import schema
5+
6+
graphql_app = GraphQL(schema)
7+
8+
app = FastAPI()
9+
app.mount("/graphql", graphql_app)
10+
11+
# TODO: add route page for /

fastapi/poetry.lock

+960
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

fastapi/pyproject.toml

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
flake8 = "^6.1.0"
17+
flake8-black = "^0.3.6"
18+
flake8-bugbear = "^23.9.16"
19+
mypy = "^1.7.0"
20+
21+
[build-system]
22+
requires = ["poetry-core>=1.8.0"]
23+
build-backend = "poetry.core.masonry.api"

fastapi/setup.cfg

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[mypy]
2+
plugins = strawberry.ext.mypy_plugin

0 commit comments

Comments
 (0)