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 graphene import Node
from graphene import Node, String, Field
from graphene_django.filter import DjangoFilterConnectionField
from graphene_django.types import DjangoObjectType
class RecipeNode(DjangoObjectType):
async_field = String()
class Meta:
model = Recipe
interfaces = (Node,)
fields = "__all__"
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 Meta:
@ -27,7 +51,13 @@ class RecipeIngredientNode(DjangoObjectType):
class Query:
recipe = Node.Field(RecipeNode)
raw_recipe = Field(RecipeType)
all_recipes = DjangoFilterConnectionField(RecipeNode)
recipeingredient = Node.Field(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.contrib import admin
from graphene_django.views import GraphQLView
from django.views.decorators.csrf import csrf_exempt
from graphene_django.views import AsyncGraphQLView
urlpatterns = [
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))),
]