Skip to content

Commit e689a6a

Browse files
committed
resolver added
1 parent 9f24986 commit e689a6a

File tree

5 files changed

+39
-13
lines changed

5 files changed

+39
-13
lines changed

fastapi/README.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,12 @@ poetry run uvicorn main:app --reload
2727
```graphql
2828
query AllTopRatedMovies {
2929
topRatedMovies {
30-
id
3130
imageUrl
3231
imdbId
3332
imdbRating
3433
imdbRatingCount
3534
title
3635
year
37-
director {
38-
id
39-
name
40-
}
4136
}
4237
}
4338
```

fastapi/api/definitions/movie.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
@strawberry.type
55
class Movie:
6-
id: int
76
imdb_id: str
87
title: str
98
year: int

fastapi/api/schema.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from typing import List
22

3+
import json
34
import strawberry
45

56
from .definitions.movie import Movie
@@ -8,9 +9,25 @@
89
@strawberry.type
910
class Query:
1011
@strawberry.field
11-
# TODO: Add a resolver for this field
1212
def top_rated_movies(self, info, limit: int = 250) -> List[Movie]:
13-
return []
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
1431

1532

1633
schema = strawberry.Schema(Query)

fastapi/main/__init__.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1-
from fastapi import FastAPI
1+
from fastapi import FastAPI, Request
2+
from fastapi.templating import Jinja2Templates
3+
from fastapi.responses import HTMLResponse
24
from strawberry.asgi import GraphQL
35

46
from api.schema import schema
57

6-
graphql_app = GraphQL(schema)
7-
88
app = FastAPI()
9-
app.mount("/graphql", graphql_app)
109

11-
# TODO: add route page for /
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/templates/index.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<html>
2+
<head>
3+
<title>Example of a GraphQL API using Strawberry and FastAPI</title>
4+
</head>
5+
<body>
6+
<a href="/graphql">Strawberry GraphQL</a>
7+
</body>
8+
</html>

0 commit comments

Comments
 (0)