Skip to content

Commit 9f24986

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

File tree

12 files changed

+1081
-2
lines changed

12 files changed

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

Lines changed: 7 additions & 0 deletions
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

Lines changed: 43 additions & 0 deletions
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

Lines changed: 12 additions & 0 deletions
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

Lines changed: 16 additions & 0 deletions
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

Lines changed: 11 additions & 0 deletions
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

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

0 commit comments

Comments
 (0)