-
Notifications
You must be signed in to change notification settings - Fork 10
/
mutations.py
142 lines (104 loc) · 3.72 KB
/
mutations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# encoding=utf-8
from contextlib import contextmanager
import graphene
from sqlalchemy.exc import SQLAlchemyError
from models import User, db_session, Todo, TodoItem
from objects import UserObject
@contextmanager
def make_session_scope(Session=db_session):
session = Session()
session.expire_on_commit = False
try:
yield session
except SQLAlchemyError:
session.rollback()
raise
finally:
session.close()
class CreateUser(graphene.Mutation):
# Your inputs
class Input:
username = graphene.String()
email = graphene.String()
# What you are returning
user = graphene.Field(lambda: UserObject)
ok = graphene.Boolean()
# You might why this is not a classmethod
# I honestly do not know the answer
def mutate(self, args, request, info):
with make_session_scope() as sess:
u = User(**args)
sess.add(u)
sess.commit()
return CreateUser(user=u, ok=True)
class DeleteUser(graphene.Mutation):
class Input:
id = graphene.Int()
ok = graphene.Boolean()
def mutate(self, args, request, info):
with make_session_scope() as sess:
if args.get('id'):
sess.query(User) \
.filter(User.id == args.get('id')) \
.delete(synchronize_session=False)
sess.commit()
return DeleteUser(ok=True)
else:
return DeleteUser(ok=False)
class CreateTodo(graphene.Mutation):
class Input:
user_id = graphene.Int()
todo_title = graphene.String()
user = graphene.Field(lambda: UserObject)
id = graphene.Int()
ok = graphene.Boolean()
def mutate(self, args, request, info):
with make_session_scope() as sess:
todo = Todo(title=args.get('todo_title'), user_id=args.get('user_id'))
sess.add(todo)
sess.commit()
u = sess.query(User).get(args.get('user_id'))
return CreateTodo(user=u, ok=True, id=todo.id)
class DeleteTodo(graphene.Mutation):
class Input:
todo_id = graphene.Int()
ok = graphene.Boolean()
def mutate(self, args, request, info):
with make_session_scope() as sess:
sess.query(TodoItem).filter_by(todo_id=args.get('todo_id')).delete()
sess.query(Todo).filter_by(id=args.get('todo_id')).delete()
sess.commit()
return DeleteUser(ok=True)
class AddTodoItem(graphene.Mutation):
class Input:
todo_id = graphene.Int()
description = graphene.String()
ok = graphene.Boolean()
user = graphene.Field(UserObject)
def mutate(self, args, request, info):
with make_session_scope() as sess:
todo_item = TodoItem(**args, is_done=False)
sess.add(todo_item)
sess.commit()
todo = sess.query(Todo).get(args.get('todo_id'))
return AddTodoItem(ok=True, user=todo.user)
class DeleteTodoItem(graphene.Mutation):
class Input:
todo_item_id = graphene.Int()
ok = graphene.Boolean()
def mutate(self, args, request, info):
with make_session_scope() as sess:
sess.query(TodoItem).filter_by(id=args.get('todo_item_id')).delete()
sess.commit()
return DeleteTodoItem(ok=True)
class CompleteTodoItem(graphene.Mutation):
class Input:
todo_item_id = graphene.Int()
ok = graphene.Boolean()
def mutate(self, args, request, info):
with make_session_scope() as sess:
todo_item = sess.query(TodoItem).get(args.get('todo_item_id'))
todo_item.is_done = True
sess.add(todo_item)
sess.commit()
return CompleteTodoItem(ok=True)