Skip to content

Commit d4ecd50

Browse files
committed
Added simple flask example
1 parent b86b5a7 commit d4ecd50

File tree

7 files changed

+154
-0
lines changed

7 files changed

+154
-0
lines changed

examples/flask_sqlalchemy/README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
Cookbook Example Django Project
2+
===============================
3+
4+
This example project demos integration between Graphene, Flask and SQLAlchemy.
5+
The project contains two models, one named `Department` and another
6+
named `Employee`.
7+
8+
Getting started
9+
---------------
10+
11+
First you'll need to get the source of the project. Do this by cloning the
12+
whole Graphene repository:
13+
14+
```bash
15+
# Get the example project code
16+
git clone https://github.com/graphql-python/graphene.git
17+
cd graphene/examples/cookbook
18+
```
19+
20+
It is good idea (but not required) to create a virtual environment
21+
for this project. We'll do this using
22+
[virtualenv](http://docs.python-guide.org/en/latest/dev/virtualenvs/)
23+
to keep things simple,
24+
but you may also find something like
25+
[virtualenvwrapper](https://virtualenvwrapper.readthedocs.org/en/latest/)
26+
to be useful:
27+
28+
```bash
29+
# Create a virtualenv in which we can install the dependencies
30+
virtualenv env
31+
source env/bin/activate
32+
```
33+
34+
Now we can install our dependencies:
35+
36+
```bash
37+
pip install -r requirements.txt
38+
```
39+
40+
Now the following command will setup the database, and start the server:
41+
42+
```bash
43+
./app.py
44+
45+
```
46+
47+
48+
Now head on over to
49+
[http://127.0.0.1:5000/](http://127.0.0.1:5000/)
50+
and run some queries!

examples/flask_sqlalchemy/__init__.py

Whitespace-only changes.

examples/flask_sqlalchemy/app.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import json
2+
from flask import Flask
3+
from database import db_session, init_db
4+
5+
from schema import schema, Department
6+
7+
app = Flask(__name__)
8+
9+
10+
@app.route('/')
11+
def hello_world():
12+
query = '{node(id:"%s"){name, employees { edges { node {name} } } } }' % Department.global_id(1)
13+
return json.dumps(schema.execute(query).data)
14+
15+
16+
@app.teardown_appcontext
17+
def shutdown_session(exception=None):
18+
db_session.remove()
19+
20+
if __name__ == '__main__':
21+
init_db()
22+
app.run()

examples/flask_sqlalchemy/database.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from sqlalchemy import create_engine
2+
from sqlalchemy.orm import scoped_session, sessionmaker
3+
from sqlalchemy.ext.declarative import declarative_base
4+
5+
engine = create_engine('sqlite:///database.sqlite3', convert_unicode=True)
6+
db_session = scoped_session(sessionmaker(autocommit=False,
7+
autoflush=False,
8+
bind=engine))
9+
Base = declarative_base()
10+
Base.query = db_session.query_property()
11+
12+
13+
def init_db():
14+
# import all modules here that might define models so that
15+
# they will be registered properly on the metadata. Otherwise
16+
# you will have to import them first before calling init_db()
17+
from models import Department, Employee
18+
Base.metadata.drop_all(bind=engine)
19+
Base.metadata.create_all(bind=engine)
20+
21+
department = Department(name='Informatics')
22+
db_session.add(department)
23+
24+
db_session.add(department)
25+
employee = Employee(name='Peter', department=department)
26+
db_session.add(employee)
27+
db_session.commit()

examples/flask_sqlalchemy/models.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from database import Base
2+
from sqlalchemy import Column, DateTime, String, Integer, ForeignKey, func
3+
from sqlalchemy.orm import relationship, backref
4+
5+
6+
class Department(Base):
7+
__tablename__ = 'department'
8+
id = Column(Integer, primary_key=True)
9+
name = Column(String)
10+
11+
12+
class Employee(Base):
13+
__tablename__ = 'employee'
14+
id = Column(Integer, primary_key=True)
15+
name = Column(String)
16+
# Use default=func.now() to set the default hiring time
17+
# of an Employee to be the current time when an
18+
# Employee record was created
19+
hired_on = Column(DateTime, default=func.now())
20+
department_id = Column(Integer, ForeignKey('department.id'))
21+
# Use cascade='delete,all' to propagate the deletion of a Department onto its Employees
22+
department = relationship(
23+
Department,
24+
backref=backref('employees',
25+
uselist=True,
26+
cascade='delete,all'))
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
graphene[sqlalchemy]
2+
SQLAlchemy==1.0.11
3+
Flask==0.10.1

examples/flask_sqlalchemy/schema.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import graphene
2+
from graphene import relay
3+
from graphene.contrib.sqlalchemy import SQLAlchemyNode
4+
from models import Department as DepartmentModel, Employee as EmployeeModel
5+
6+
from database import db_session
7+
8+
schema = graphene.Schema(session=db_session)
9+
10+
11+
@schema.register
12+
class Department(SQLAlchemyNode):
13+
class Meta:
14+
model = DepartmentModel
15+
16+
17+
@schema.register
18+
class Employee(SQLAlchemyNode):
19+
class Meta:
20+
model = EmployeeModel
21+
22+
23+
class Query(graphene.ObjectType):
24+
node = relay.NodeField(Department, Employee)
25+
26+
schema.query = Query

0 commit comments

Comments
 (0)