2022-10-19 17:10:30 +03:00
|
|
|
import graphene
|
2019-11-01 02:31:31 +03:00
|
|
|
|
2022-10-19 17:10:30 +03:00
|
|
|
from graphene_django.types import DjangoObjectType
|
2019-11-01 02:31:31 +03:00
|
|
|
|
2022-10-19 17:10:30 +03:00
|
|
|
from cookbook.ingredients.models import Category, Ingredient
|
2019-11-01 02:31:31 +03:00
|
|
|
|
|
|
|
|
2022-10-19 17:10:30 +03:00
|
|
|
class CategoryType(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = Category
|
|
|
|
fields = "__all__"
|
2019-11-01 02:31:31 +03:00
|
|
|
|
|
|
|
|
2022-10-19 17:10:30 +03:00
|
|
|
class IngredientType(DjangoObjectType):
|
|
|
|
class Meta:
|
|
|
|
model = Ingredient
|
|
|
|
fields = "__all__"
|
2019-11-01 02:31:31 +03:00
|
|
|
|
|
|
|
|
2022-10-19 17:10:30 +03:00
|
|
|
class Query:
|
|
|
|
category = graphene.Field(CategoryType, id=graphene.Int(), name=graphene.String())
|
|
|
|
all_categories = graphene.List(CategoryType)
|
2019-11-01 02:31:31 +03:00
|
|
|
|
2022-10-19 17:10:30 +03:00
|
|
|
ingredient = graphene.Field(
|
|
|
|
IngredientType, id=graphene.Int(), name=graphene.String()
|
|
|
|
)
|
|
|
|
all_ingredients = graphene.List(IngredientType)
|
2019-11-01 02:31:31 +03:00
|
|
|
|
2022-10-19 17:10:30 +03:00
|
|
|
def resolve_all_categories(self, info, **kwargs):
|
|
|
|
return Category.objects.all()
|
2019-11-01 02:31:31 +03:00
|
|
|
|
2022-10-19 17:10:30 +03:00
|
|
|
def resolve_all_ingredients(self, info, **kwargs):
|
|
|
|
return Ingredient.objects.all()
|
2019-11-01 02:31:31 +03:00
|
|
|
|
2022-10-19 17:10:30 +03:00
|
|
|
def resolve_category(self, info, **kwargs):
|
|
|
|
id = kwargs.get("id")
|
|
|
|
name = kwargs.get("name")
|
2019-11-01 02:31:31 +03:00
|
|
|
|
2022-10-19 17:10:30 +03:00
|
|
|
if id is not None:
|
|
|
|
return Category.objects.get(pk=id)
|
2019-11-01 02:31:31 +03:00
|
|
|
|
2022-10-19 17:10:30 +03:00
|
|
|
if name is not None:
|
|
|
|
return Category.objects.get(name=name)
|
2019-11-01 02:31:31 +03:00
|
|
|
|
2022-10-19 17:10:30 +03:00
|
|
|
return None
|
2019-11-01 02:31:31 +03:00
|
|
|
|
2022-10-19 17:10:30 +03:00
|
|
|
def resolve_ingredient(self, info, **kwargs):
|
|
|
|
id = kwargs.get("id")
|
|
|
|
name = kwargs.get("name")
|
2019-11-01 02:31:31 +03:00
|
|
|
|
2022-10-19 17:10:30 +03:00
|
|
|
if id is not None:
|
|
|
|
return Ingredient.objects.get(pk=id)
|
2019-11-01 02:31:31 +03:00
|
|
|
|
2022-10-19 17:10:30 +03:00
|
|
|
if name is not None:
|
|
|
|
return Ingredient.objects.get(name=name)
|
2019-11-01 02:31:31 +03:00
|
|
|
|
2022-10-19 17:10:30 +03:00
|
|
|
return None
|