import graphene from graphene_django.types import DjangoObjectType from cookbook.ingredients.models import Category, Ingredient class CategoryType(DjangoObjectType): class Meta: model = Category class IngredientType(DjangoObjectType): class Meta: model = Ingredient class Query(graphene.AbstractType): category = graphene.Field(CategoryType, id=graphene.Int(), name=graphene.String()) all_categories = graphene.List(CategoryType) ingredient = graphene.Field(IngredientType, id=graphene.Int(), name=graphene.String()) all_ingredients = graphene.List(IngredientType) def resolve_all_categories(self, args, context, info): return Category.objects.all() def resolve_all_ingredients(self, args, context, info): # We can easily optimize query count in the resolve method return Ingredient.objects.select_related('category').all() def resolve_category(self, args, context, info): id = args.get('id') name = args.get('name') if id is not None: return Category.objects.get(pk=id) if name is not None: return Category.objects.get(name=name) return None def resolve_ingredient(self, args, context, info): id = args.get('id') name = args.get('name') if id is not None: return Ingredient.objects.get(pk=id) if name is not None: return Ingredient.objects.get(name=name) return None