-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexample_app.py
159 lines (110 loc) · 4.22 KB
/
example_app.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import graphene
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from graphene import relay
from graphene_sqlalchemy import SQLAlchemyObjectType, SQLAlchemyConnectionField
from sqlalchemy import func
from sqlalchemy.orm import backref
from flask_graphql_rest import GraphQLREST
sa = SQLAlchemy(session_options={"autoflush": False})
# Define your models
class PublisherModel(sa.Model):
__tablename__ = 'publisher'
id = sa.Column(sa.Integer, primary_key=True)
created_at = sa.Column(sa.DateTime(timezone=True), default=func.now(), nullable=False)
name = sa.Column(sa.String(length=30))
class AuthorModel(sa.Model):
__tablename__ = 'author'
id = sa.Column(sa.Integer, primary_key=True)
created_at = sa.Column(sa.DateTime(timezone=True), default=func.now(), nullable=False)
name = sa.Column(sa.String(length=30))
class AuthorBookAssociationModel(sa.Model):
__tablename__ = 'author_book_association'
id = sa.Column(sa.Integer, primary_key=True)
created_at = sa.Column(sa.DateTime(timezone=True), default=func.now(), nullable=False)
author_id = sa.Column(sa.Integer, sa.ForeignKey('author.id'))
book_id = sa.Column(sa.Integer, sa.ForeignKey('book.id'))
class BookModel(sa.Model):
__tablename__ = 'book'
id = sa.Column(sa.Integer, primary_key=True)
title = sa.Column(sa.String(length=30))
publisher_id = sa.Column(sa.Integer,
sa.ForeignKey('publisher.id'),
nullable=True)
# orm relationships
publisher = sa.relationship(PublisherModel,
backref=backref('books', uselist=True),
uselist=False)
authors = sa.relationship(AuthorModel,
backref=backref('books', uselist=True),
secondary=AuthorBookAssociationModel.__table__)
# Define your Graphene objects
class Publisher(SQLAlchemyObjectType):
class Meta:
model = PublisherModel
interfaces = (relay.Node,)
class Author(SQLAlchemyObjectType):
class Meta:
model = AuthorModel
interfaces = (relay.Node,)
class Book(SQLAlchemyObjectType):
class Meta:
model = BookModel
interfaces = (relay.Node,)
class Query(graphene.ObjectType):
node = relay.Node.Field()
books = SQLAlchemyConnectionField(Book)
hello = graphene.String(name=graphene.String(default_value="stranger"))
def resolve_hello(self, info, name):
return 'Hello ' + name
class Person(graphene.ObjectType):
name = graphene.String()
age = graphene.Int()
class CreatePerson(graphene.Mutation):
class Arguments:
name = graphene.String()
age = graphene.Int()
ok = graphene.Boolean()
person = graphene.Field(lambda: Person)
def mutate(self, info, name, age):
person = Person(name=name, age=age)
ok = True
return CreatePerson(person=person, ok=ok)
class MyMutations(graphene.ObjectType):
create_person = CreatePerson.Field()
# Create Graphene Schema
schema = graphene.Schema(
query=Query,
mutation=MyMutations
)
def create_app():
app = Flask(__name__)
with app.app_context():
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://'
sa.init_app(app)
sa.create_all()
initialize_data()
graphene_rest = GraphQLREST(schema)
graphene_rest.init_app(app)
return app
def initialize_data():
publisher_1 = PublisherModel(name='Packt')
publisher_2 = PublisherModel(name='O\'Reilly Media')
sa.session.add(publisher_1)
sa.session.add(publisher_2)
author_1 = AuthorModel(name='Tarek Ziade')
author_2 = AuthorModel(name='David Mertz')
sa.session.add(author_1)
sa.session.add(author_2)
sa.session.flush()
book_1 = BookModel(title='Python Microservices Development', publisher=publisher_1)
book_1.authors.append(author_1)
book_2 = BookModel(title='Functional Programming in Python', publisher=publisher_2)
book_2.authors.append(author_2)
sa.session.add(book_1)
sa.session.add(book_2)
sa.session.commit()
if __name__ == '__main__':
app = create_app()
app.run(port=8005, debug=True)