Update cookbook for async testing

This commit is contained in:
Josh Warwick 2023-05-04 15:26:33 +01:00
parent 64d311d770
commit bdb8e84446
2 changed files with 34 additions and 4 deletions

View File

@ -1,16 +1,40 @@
import asyncio
from asgiref.sync import sync_to_async
from cookbook.recipes.models import Recipe, RecipeIngredient from cookbook.recipes.models import Recipe, RecipeIngredient
from graphene import Node from graphene import Node, String, Field
from graphene_django.filter import DjangoFilterConnectionField from graphene_django.filter import DjangoFilterConnectionField
from graphene_django.types import DjangoObjectType from graphene_django.types import DjangoObjectType
class RecipeNode(DjangoObjectType): class RecipeNode(DjangoObjectType):
async_field = String()
class Meta: class Meta:
model = Recipe model = Recipe
interfaces = (Node,) interfaces = (Node,)
fields = "__all__" fields = "__all__"
filter_fields = ["title", "amounts"] filter_fields = ["title", "amounts"]
async def resolve_async_field(self, info):
await asyncio.sleep(2)
return "success"
class RecipeType(DjangoObjectType):
async_field = String()
class Meta:
model = Recipe
fields = "__all__"
filter_fields = ["title", "amounts"]
skip_registry = True
async def resolve_async_field(self, info):
await asyncio.sleep(2)
return "success"
class RecipeIngredientNode(DjangoObjectType): class RecipeIngredientNode(DjangoObjectType):
class Meta: class Meta:
@ -27,7 +51,13 @@ class RecipeIngredientNode(DjangoObjectType):
class Query: class Query:
recipe = Node.Field(RecipeNode) recipe = Node.Field(RecipeNode)
raw_recipe = Field(RecipeType)
all_recipes = DjangoFilterConnectionField(RecipeNode) all_recipes = DjangoFilterConnectionField(RecipeNode)
recipeingredient = Node.Field(RecipeIngredientNode) recipeingredient = Node.Field(RecipeIngredientNode)
all_recipeingredients = DjangoFilterConnectionField(RecipeIngredientNode) all_recipeingredients = DjangoFilterConnectionField(RecipeIngredientNode)
@staticmethod
@sync_to_async
def resolve_raw_recipe(self, info):
return Recipe.objects.first()

View File

@ -1,9 +1,9 @@
from django.urls import re_path from django.urls import re_path
from django.contrib import admin from django.contrib import admin
from django.views.decorators.csrf import csrf_exempt
from graphene_django.views import GraphQLView from graphene_django.views import AsyncGraphQLView
urlpatterns = [ urlpatterns = [
re_path(r"^admin/", admin.site.urls), re_path(r"^admin/", admin.site.urls),
re_path(r"^graphql$", GraphQLView.as_view(graphiql=True)), re_path(r"^graphql$", csrf_exempt(AsyncGraphQLView.as_view(graphiql=True))),
] ]