Created
September 17, 2016 23:22
-
-
Save jadehopepunk/a4c843a2eab723783081a082fd98ebad to your computer and use it in GitHub Desktop.
django graphene cookbook schema
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# cookbook/ingredients/schema.py | |
from graphene import relay, ObjectType | |
from graphene.contrib.django.filter import DjangoFilterConnectionField | |
from graphene.contrib.django.types import DjangoNode | |
from cookbook.ingredients.models import Category, Ingredient | |
# Graphene will automatically map the Category model's fields onto the CategoryNode. | |
# This is configured in the CategoryNode's Meta class (as you can see below) | |
class CategoryNode(DjangoNode): | |
class Meta: | |
model = Category | |
filter_fields = ['name', 'ingredients'] | |
filter_order_by = ['name'] | |
class IngredientNode(DjangoNode): | |
class Meta: | |
model = Ingredient | |
# Allow for some more advanced filtering here | |
filter_fields = { | |
'name': ['exact', 'icontains', 'istartswith'], | |
'notes': ['exact', 'icontains'], | |
'category': ['exact'], | |
'category__name': ['exact'], | |
} | |
filter_order_by = ['name', 'category__name'] | |
class Query(ObjectType): | |
category = relay.NodeField(CategoryNode) | |
all_categories = DjangoFilterConnectionField(CategoryNode) | |
ingredient = relay.NodeField(IngredientNode) | |
all_ingredients = DjangoFilterConnectionField(IngredientNode) | |
class Meta: | |
abstract = True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment