diff --git a/examples/cookbook/cookbook/recipes/schema.py b/examples/cookbook/cookbook/recipes/schema.py index ea5ed38..82f7f1d 100644 --- a/examples/cookbook/cookbook/recipes/schema.py +++ b/examples/cookbook/cookbook/recipes/schema.py @@ -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() diff --git a/examples/cookbook/cookbook/urls.py b/examples/cookbook/cookbook/urls.py index e9e69cd..541cd2d 100644 --- a/examples/cookbook/cookbook/urls.py +++ b/examples/cookbook/cookbook/urls.py @@ -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))), ]