File tree Expand file tree Collapse file tree 5 files changed +39
-13
lines changed Expand file tree Collapse file tree 5 files changed +39
-13
lines changed Original file line number Diff line number Diff line change @@ -27,17 +27,12 @@ poetry run uvicorn main:app --reload
27
27
``` graphql
28
28
query AllTopRatedMovies {
29
29
topRatedMovies {
30
- id
31
30
imageUrl
32
31
imdbId
33
32
imdbRating
34
33
imdbRatingCount
35
34
title
36
35
year
37
- director {
38
- id
39
- name
40
- }
41
36
}
42
37
}
43
38
```
Original file line number Diff line number Diff line change 3
3
4
4
@strawberry .type
5
5
class Movie :
6
- id : int
7
6
imdb_id : str
8
7
title : str
9
8
year : int
Original file line number Diff line number Diff line change 1
1
from typing import List
2
2
3
+ import json
3
4
import strawberry
4
5
5
6
from .definitions .movie import Movie
8
9
@strawberry .type
9
10
class Query :
10
11
@strawberry .field
11
- # TODO: Add a resolver for this field
12
12
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
14
31
15
32
16
33
schema = strawberry .Schema (Query )
Original file line number Diff line number Diff line change 1
- from fastapi import FastAPI
1
+ from fastapi import FastAPI , Request
2
+ from fastapi .templating import Jinja2Templates
3
+ from fastapi .responses import HTMLResponse
2
4
from strawberry .asgi import GraphQL
3
5
4
6
from api .schema import schema
5
7
6
- graphql_app = GraphQL (schema )
7
-
8
8
app = FastAPI ()
9
- app .mount ("/graphql" , graphql_app )
10
9
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 ))
Original file line number Diff line number Diff line change
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 >
You can’t perform that action at this time.
0 commit comments