Skip to content

Commit bdb8e84

Browse files
committed
Update cookbook for async testing
1 parent 64d311d commit bdb8e84

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

examples/cookbook/cookbook/recipes/schema.py

+31-1
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,40 @@
1+
import asyncio
2+
3+
from asgiref.sync import sync_to_async
4+
15
from cookbook.recipes.models import Recipe, RecipeIngredient
2-
from graphene import Node
6+
from graphene import Node, String, Field
37
from graphene_django.filter import DjangoFilterConnectionField
48
from graphene_django.types import DjangoObjectType
59

610

711
class 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

1539
class RecipeIngredientNode(DjangoObjectType):
1640
class Meta:
@@ -27,7 +51,13 @@ class Meta:
2751

2852
class 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()

examples/cookbook/cookbook/urls.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from django.urls import re_path
22
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
55

66
urlpatterns = [
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
]

0 commit comments

Comments
 (0)