Skip to content

Commit 22acaa8

Browse files
committed
add CreateCategory and CreateIngredient Mutations
1 parent fe4992b commit 22acaa8

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

cookbook/ingredients/schema.py

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ class Query(object):
2121
name=graphene.String())
2222
all_categories = graphene.List(CategoryType)
2323

24-
2524
ingredient = graphene.Field(IngredientType,
2625
id=graphene.Int(),
2726
name=graphene.String())
@@ -55,4 +54,38 @@ def resolve_ingredient(self, info, **kwargs):
5554
if name is not None:
5655
return Ingredient.objects.get(name=name)
5756

58-
return None
57+
return None
58+
59+
60+
class CreateCategory(graphene.Mutation):
61+
class Arguments:
62+
name = graphene.String(required=True)
63+
64+
category = graphene.Field(lambda: CategoryType)
65+
ok = graphene.Boolean()
66+
67+
def mutate(self, info, name):
68+
category = Category.objects.create(name=name)
69+
ok=True
70+
return CreateCategory(category=category,ok=ok)
71+
72+
73+
class CreateIngredient(graphene.Mutation):
74+
class Arguments:
75+
name = graphene.String(required=True)
76+
notes = graphene.String(required=True)
77+
category = graphene.Int(required=True)
78+
79+
ingredient = graphene.Field(lambda: IngredientType)
80+
ok = graphene.Boolean()
81+
82+
def mutate(self, info, **kwargs):
83+
kwargs['category'] = Category.objects.get(pk=kwargs['category'])
84+
ingredient = Ingredient.objects.create(**kwargs)
85+
ok = True
86+
return CreateIngredient(ingredient=ingredient,ok=ok)
87+
88+
89+
class Mutation(object):
90+
create_category = CreateCategory.Field()
91+
create_ingredient = CreateIngredient.Field()

cookbook/schema.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,8 @@ class Query(cookbook.ingredients.schema.Query, graphene.ObjectType):
99
pass
1010

1111

12-
schema = graphene.Schema(query=Query)
12+
class Mutation(cookbook.ingredients.schema.Mutation, graphene.ObjectType):
13+
pass
14+
15+
16+
schema = graphene.Schema(query=Query, mutation=Mutation)

db.sqlite3

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)