File tree Expand file tree Collapse file tree 2 files changed +34
-4
lines changed
examples/cookbook/cookbook Expand file tree Collapse file tree 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+
15from cookbook .recipes .models import Recipe , RecipeIngredient
2- from graphene import Node
6+ from graphene import Node , String , Field
37from graphene_django .filter import DjangoFilterConnectionField
48from graphene_django .types import DjangoObjectType
59
610
711class RecipeNode (DjangoObjectType ):
12+ async_field = String ()
13+
814 class Meta :
915 model = Recipe
1016 interfaces = (Node ,)
1117 fields = "__all__"
1218 filter_fields = ["title" , "amounts" ]
1319
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+
1438
1539class RecipeIngredientNode (DjangoObjectType ):
1640 class Meta :
@@ -27,7 +51,13 @@ class Meta:
2751
2852class Query :
2953 recipe = Node .Field (RecipeNode )
54+ raw_recipe = Field (RecipeType )
3055 all_recipes = DjangoFilterConnectionField (RecipeNode )
3156
3257 recipeingredient = Node .Field (RecipeIngredientNode )
3358 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 11from django .urls import re_path
22from 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
55
66urlpatterns = [
77 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 ) )),
99]
You can’t perform that action at this time.
0 commit comments