Skip to content

Instantly share code, notes, and snippets.

@chris001177
Created June 27, 2019 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chris001177/143bab4c4d6ccb6738f4738f12335d1f to your computer and use it in GitHub Desktop.
Save chris001177/143bab4c4d6ccb6738f4738f12335d1f to your computer and use it in GitHub Desktop.
# cookbook/ingredients/schema.py
import graphene
from graphene_django.types import DjangoObjectType
from ingredients.models import Category, Ingredient
class CategoryType(DjangoObjectType):
class Meta:
model = Category
class IngredientType(DjangoObjectType):
class Meta:
model = Ingredient
class Query(object):
all_categories = graphene.List(CategoryType)
all_ingredients = graphene.List(IngredientType)
def resolve_all_categories(self, info, **kwargs):
return Category.objects.all()
def resolve_all_ingredients(self, info, **kwargs):
# We can easily optimize query count in the resolve method
return Ingredient.objects.select_related('category').all()
# Add New Category Mutation
class AddCategory(graphene.Mutation):
class Arguments:
# The input arguments for this mutation
categoryName = graphene.String(required=True)
# The class attributes define the response of the mutation
category = graphene.Field(CategoryType)
def mutate(self, info, categoryName):
_category = Category.objects.create(name=categoryName)
# Notice we return an instance of this mutation
return AddCategory(category=_category)
class Mutation(object):
add_category = AddCategory.Field()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment