2017-02-14 21:23:45 +03:00
|
|
|
import graphene
|
|
|
|
from graphene_django.types import DjangoObjectType
|
|
|
|
|
2018-10-18 21:33:53 +03:00
|
|
|
from .models import Recipe, RecipeIngredient
|
2017-02-14 21:23:45 +03:00
|
|
|
|
|
|
|
|
|
|
|
class RecipeType(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = Recipe
|
2020-06-11 13:09:52 +03:00
|
|
|
fields = "__all__"
|
2017-02-14 21:23:45 +03:00
|
|
|
|
|
|
|
|
|
|
|
class RecipeIngredientType(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = RecipeIngredient
|
2020-06-11 13:09:52 +03:00
|
|
|
fields = "__all__"
|
2017-02-14 21:23:45 +03:00
|
|
|
|
|
|
|
|
2018-06-05 22:53:47 +03:00
|
|
|
class Query(object):
|
2019-06-11 06:54:30 +03:00
|
|
|
recipe = graphene.Field(RecipeType, id=graphene.Int(), title=graphene.String())
|
2017-02-14 21:23:45 +03:00
|
|
|
all_recipes = graphene.List(RecipeType)
|
|
|
|
|
2019-06-11 06:54:30 +03:00
|
|
|
recipeingredient = graphene.Field(RecipeIngredientType, id=graphene.Int())
|
2017-02-14 21:23:45 +03:00
|
|
|
all_recipeingredients = graphene.List(RecipeIngredientType)
|
|
|
|
|
2018-10-18 22:27:23 +03:00
|
|
|
def resolve_recipe(self, context, id=None, title=None):
|
2017-02-14 21:23:45 +03:00
|
|
|
if id is not None:
|
|
|
|
return Recipe.objects.get(pk=id)
|
|
|
|
|
|
|
|
if title is not None:
|
|
|
|
return Recipe.objects.get(title=title)
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
2018-10-18 22:27:23 +03:00
|
|
|
def resolve_recipeingredient(self, context, id=None):
|
2017-02-14 21:23:45 +03:00
|
|
|
if id is not None:
|
|
|
|
return RecipeIngredient.objects.get(pk=id)
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
2018-10-18 22:27:23 +03:00
|
|
|
def resolve_all_recipes(self, context):
|
2017-02-14 21:23:45 +03:00
|
|
|
return Recipe.objects.all()
|
|
|
|
|
2018-10-18 22:27:23 +03:00
|
|
|
def resolve_all_recipeingredients(self, context):
|
2019-06-11 06:54:30 +03:00
|
|
|
related = ["recipe", "ingredient"]
|
2017-02-14 21:23:45 +03:00
|
|
|
return RecipeIngredient.objects.select_related(*related).all()
|