-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
65 lines (45 loc) · 1.73 KB
/
database.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
from flask_sqlalchemy import SQLAlchemy
import graphene
from graphene_sqlalchemy import SQLAlchemyObjectType, SQLAlchemyConnectionField
from flask_graphql import GraphQLView
from datetime import datetime
db = SQLAlchemy(db)
class TodoModel(db.Model):
__tablename__ = 'todos'
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100), nullable=False)
description = db.Column(db.String(500), nullable=False)
done = db.Column(db.Boolean)
created_at = db.Column(db.DateTime, default=datetime.utcnow())
def __repr__(self):
return f"<Todo {self.id}>"
class TodoObject(SQLAlchemyObjectType):
class Meta:
model = TodoModel
interfaces = (graphene.relay.Node, )
def fetch_todos_from_database():
todos = TodoModel.query.all()
return [Todo(
id=todo.id,
title=todo.title,
completed=todo.completed
) for todo in todos]
class Query(graphene.ObjectType):
node = graphene.relay.Node.Field()
all_todos = SQLAlchemyConnectionField(TodoObject.connection)
class CreateTodoMutation(graphene.Mutation):
class Arguments:
title = graphene.String(required=True)
description = graphene.String(required=True)
done = graphene.Boolean(required=True)
todo = graphene.Field(lambda: TodoObject)
def mutate(self, info, title, description, done):
todo = TodoModel(title=title, description=description, done=done)
db.session.add(todo)
db.session.commit()
return CreateTodoMutation(todo=todo)
class Mutation(graphene.ObjectType):
create_todo = CreateTodoMutation.Field()
def get_todos():
todos = TodoModel.query.all()
return todos