File tree 2 files changed +34
-4
lines changed
examples/cookbook/cookbook
2 files changed +34
-4
lines changed Original file line number Diff line number Diff line change
1
+ import asyncio
2
+
3
+ from asgiref .sync import sync_to_async
4
+
1
5
from cookbook .recipes .models import Recipe , RecipeIngredient
2
- from graphene import Node
6
+ from graphene import Node , String , Field
3
7
from graphene_django .filter import DjangoFilterConnectionField
4
8
from graphene_django .types import DjangoObjectType
5
9
6
10
7
11
class RecipeNode (DjangoObjectType ):
12
+ async_field = String ()
13
+
8
14
class Meta :
9
15
model = Recipe
10
16
interfaces = (Node ,)
11
17
fields = "__all__"
12
18
filter_fields = ["title" , "amounts" ]
13
19
20
+ async def resolve_async_field (self , info ):
21
+ await asyncio .sleep (2 )
22
+ return "success"
23
+
24
+
25
+ class RecipeType (DjangoObjectType ):
26
+ async_field = String ()
27
+
28
+ class Meta :
29
+ model = Recipe
30
+ fields = "__all__"
31
+ filter_fields = ["title" , "amounts" ]
32
+ skip_registry = True
33
+
34
+ async def resolve_async_field (self , info ):
35
+ await asyncio .sleep (2 )
36
+ return "success"
37
+
14
38
15
39
class RecipeIngredientNode (DjangoObjectType ):
16
40
class Meta :
@@ -27,7 +51,13 @@ class Meta:
27
51
28
52
class Query :
29
53
recipe = Node .Field (RecipeNode )
54
+ raw_recipe = Field (RecipeType )
30
55
all_recipes = DjangoFilterConnectionField (RecipeNode )
31
56
32
57
recipeingredient = Node .Field (RecipeIngredientNode )
33
58
all_recipeingredients = DjangoFilterConnectionField (RecipeIngredientNode )
59
+
60
+ @staticmethod
61
+ @sync_to_async
62
+ def resolve_raw_recipe (self , info ):
63
+ return Recipe .objects .first ()
Original file line number Diff line number Diff line change 1
1
from django .urls import re_path
2
2
from django .contrib import admin
3
-
4
- from graphene_django .views import GraphQLView
3
+ from django . views . decorators . csrf import csrf_exempt
4
+ from graphene_django .views import AsyncGraphQLView
5
5
6
6
urlpatterns = [
7
7
re_path (r"^admin/" , admin .site .urls ),
8
- re_path (r"^graphql$" , GraphQLView .as_view (graphiql = True )),
8
+ re_path (r"^graphql$" , csrf_exempt ( AsyncGraphQLView .as_view (graphiql = True ) )),
9
9
]
You can’t perform that action at this time.
0 commit comments