mirror of
https://github.com/graphql-python/graphene-django.git
synced 2024-11-24 10:34:10 +03:00
4517e32224
* 🔧 Add pre-commit config Similar to graphene and graphene-sqlalchemy * ⬆ Bump black * 👷 Lint on CI * ⬆ Bump flake8-black * 🔧 Keep excluding migrations * ⬆ Bump flake8 * 🔧 Remove black and flake8 from tox config * ⬆ Update pre-commit versions * Upgrade syntax to python 3.7+ * Format with pre-commit dedent docs/schema.py to allow formatting * Fix tests on python 3.7
52 lines
1.4 KiB
Python
52 lines
1.4 KiB
Python
import graphene
|
|
from graphene_django.types import DjangoObjectType
|
|
|
|
from .models import Category, Ingredient
|
|
|
|
|
|
class CategoryType(DjangoObjectType):
|
|
class Meta:
|
|
model = Category
|
|
fields = "__all__"
|
|
|
|
|
|
class IngredientType(DjangoObjectType):
|
|
class Meta:
|
|
model = Ingredient
|
|
fields = "__all__"
|
|
|
|
|
|
class Query:
|
|
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, context):
|
|
return Category.objects.all()
|
|
|
|
def resolve_all_ingredients(self, context):
|
|
# We can easily optimize query count in the resolve method
|
|
return Ingredient.objects.select_related("category").all()
|
|
|
|
def resolve_category(self, context, id=None, name=None):
|
|
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, context, id=None, name=None):
|
|
if id is not None:
|
|
return Ingredient.objects.get(pk=id)
|
|
|
|
if name is not None:
|
|
return Ingredient.objects.get(name=name)
|
|
|
|
return None
|