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
|
|
|
|
|
|
|
|
|
|
|
|
class RecipeIngredientType(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = RecipeIngredient
|
|
|
|
|
|
|
|
|
2018-06-05 22:53:47 +03:00
|
|
|
class Query(object):
|
2017-02-14 21:23:45 +03:00
|
|
|
recipe = graphene.Field(RecipeType,
|
|
|
|
id=graphene.Int(),
|
|
|
|
title=graphene.String())
|
|
|
|
all_recipes = graphene.List(RecipeType)
|
|
|
|
|
|
|
|
recipeingredient = graphene.Field(RecipeIngredientType,
|
|
|
|
id=graphene.Int())
|
|
|
|
all_recipeingredients = graphene.List(RecipeIngredientType)
|
|
|
|
|
2018-10-18 21:33:53 +03:00
|
|
|
def resolve_recipe(self, context, **kwargs):
|
|
|
|
id = kwargs.get('id')
|
|
|
|
title = kwargs.get('title')
|
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 21:33:53 +03:00
|
|
|
def resolve_recipeingredient(self, context, **kwargs):
|
|
|
|
id = kwargs.get('id')
|
2017-02-14 21:23:45 +03:00
|
|
|
|
|
|
|
if id is not None:
|
|
|
|
return RecipeIngredient.objects.get(pk=id)
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
2018-10-18 21:33:53 +03:00
|
|
|
def resolve_all_recipes(self, context, **kwargs):
|
2017-02-14 21:23:45 +03:00
|
|
|
return Recipe.objects.all()
|
|
|
|
|
2018-10-18 21:33:53 +03:00
|
|
|
def resolve_all_recipeingredients(self, context, **kwargs):
|
2017-02-14 21:23:45 +03:00
|
|
|
related = ['recipe', 'ingredient']
|
|
|
|
return RecipeIngredient.objects.select_related(*related).all()
|