Improved Flask SQLAlchemy example

This commit is contained in:
Syrus Akbary 2016-01-23 13:12:01 -08:00
parent 553982c3fc
commit 5697ef98e9
3 changed files with 14 additions and 7 deletions

View File

@ -18,10 +18,16 @@ def init_db():
Base.metadata.drop_all(bind=engine) Base.metadata.drop_all(bind=engine)
Base.metadata.create_all(bind=engine) Base.metadata.create_all(bind=engine)
department = Department(name='Informatics') # Create the fixtures
db_session.add(department) engineering = Department(name='Engineering')
db_session.add(engineering)
hr = Department(name='Human Resources')
db_session.add(hr)
db_session.add(department) peter = Employee(name='Peter', department=engineering)
employee = Employee(name='Peter', department=department) db_session.add(peter)
db_session.add(employee) roy = Employee(name='Roy', department=engineering)
db_session.add(roy)
tracy = Employee(name='Tracy', department=hr)
db_session.add(tracy)
db_session.commit() db_session.commit()

View File

@ -1,4 +1,4 @@
graphene[sqlalchemy] graphene[sqlalchemy]
graphql_flask==1.0.0 graphql_flask==1.1.0
SQLAlchemy==1.0.11 SQLAlchemy==1.0.11
Flask==0.10.1 Flask==0.10.1

View File

@ -1,6 +1,6 @@
import graphene import graphene
from graphene import relay from graphene import relay
from graphene.contrib.sqlalchemy import SQLAlchemyNode from graphene.contrib.sqlalchemy import SQLAlchemyNode, SQLAlchemyConnectionField
from models import Department as DepartmentModel, Employee as EmployeeModel from models import Department as DepartmentModel, Employee as EmployeeModel
from database import db_session from database import db_session
@ -22,5 +22,6 @@ class Employee(SQLAlchemyNode):
class Query(graphene.ObjectType): class Query(graphene.ObjectType):
node = relay.NodeField(Department, Employee) node = relay.NodeField(Department, Employee)
all_employees = SQLAlchemyConnectionField(Employee)
schema.query = Query schema.query = Query