mirror of
https://github.com/graphql-python/graphene.git
synced 2024-11-11 04:07:16 +03:00
update flask sqlalchemy examples
This commit is contained in:
parent
1f9252f26a
commit
74a4043899
|
@ -12,14 +12,19 @@ default_query = '''
|
|||
allEmployees {
|
||||
edges {
|
||||
node {
|
||||
id
|
||||
name
|
||||
id,
|
||||
name,
|
||||
department {
|
||||
id,
|
||||
name
|
||||
},
|
||||
role {
|
||||
id,
|
||||
name
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
}'''.strip()
|
||||
|
||||
GraphQL(app, schema=schema, default_query=default_query)
|
||||
|
|
|
@ -14,7 +14,7 @@ def init_db():
|
|||
# import all modules here that might define models so that
|
||||
# they will be registered properly on the metadata. Otherwise
|
||||
# you will have to import them first before calling init_db()
|
||||
from models import Department, Employee
|
||||
from models import Department, Employee, Role
|
||||
Base.metadata.drop_all(bind=engine)
|
||||
Base.metadata.create_all(bind=engine)
|
||||
|
||||
|
@ -24,10 +24,15 @@ def init_db():
|
|||
hr = Department(name='Human Resources')
|
||||
db_session.add(hr)
|
||||
|
||||
peter = Employee(name='Peter', department=engineering)
|
||||
manager = Role(name='manager')
|
||||
db_session.add(manager)
|
||||
engineer = Role(name='engineer')
|
||||
db_session.add(engineer)
|
||||
|
||||
peter = Employee(name='Peter', department=engineering, role=engineer)
|
||||
db_session.add(peter)
|
||||
roy = Employee(name='Roy', department=engineering)
|
||||
roy = Employee(name='Roy', department=engineering, role=engineer)
|
||||
db_session.add(roy)
|
||||
tracy = Employee(name='Tracy', department=hr)
|
||||
tracy = Employee(name='Tracy', department=hr, role=manager)
|
||||
db_session.add(tracy)
|
||||
db_session.commit()
|
||||
|
|
|
@ -10,6 +10,12 @@ class Department(Base):
|
|||
name = Column(String)
|
||||
|
||||
|
||||
class Role(Base):
|
||||
__tablename__ = 'roles'
|
||||
role_id = Column(Integer, primary_key=True)
|
||||
name = Column(String)
|
||||
|
||||
|
||||
class Employee(Base):
|
||||
__tablename__ = 'employee'
|
||||
id = Column(Integer, primary_key=True)
|
||||
|
@ -19,9 +25,15 @@ class Employee(Base):
|
|||
# Employee record was created
|
||||
hired_on = Column(DateTime, default=func.now())
|
||||
department_id = Column(Integer, ForeignKey('department.id'))
|
||||
role_id = Column(Integer, ForeignKey('roles.role_id'))
|
||||
# Use cascade='delete,all' to propagate the deletion of a Department onto its Employees
|
||||
department = relationship(
|
||||
Department,
|
||||
backref=backref('employees',
|
||||
uselist=True,
|
||||
cascade='delete,all'))
|
||||
role = relationship(
|
||||
Role,
|
||||
backref=backref('roles',
|
||||
uselist=True,
|
||||
cascade='delete,all'))
|
||||
|
|
|
@ -4,6 +4,7 @@ from graphene.contrib.sqlalchemy import (SQLAlchemyConnectionField,
|
|||
SQLAlchemyNode)
|
||||
from models import Department as DepartmentModel
|
||||
from models import Employee as EmployeeModel
|
||||
from models import Role as RoleModel
|
||||
|
||||
schema = graphene.Schema()
|
||||
|
||||
|
@ -22,8 +23,18 @@ class Employee(SQLAlchemyNode):
|
|||
model = EmployeeModel
|
||||
|
||||
|
||||
@schema.register
|
||||
class Role(SQLAlchemyNode):
|
||||
|
||||
class Meta:
|
||||
model = RoleModel
|
||||
identifier = 'role_id'
|
||||
|
||||
|
||||
class Query(graphene.ObjectType):
|
||||
node = relay.NodeField()
|
||||
node = relay.NodeField(Employee)
|
||||
all_employees = SQLAlchemyConnectionField(Employee)
|
||||
all_roles = SQLAlchemyConnectionField(Role)
|
||||
role = relay.NodeField(Role)
|
||||
|
||||
schema.query = Query
|
||||
|
|
Loading…
Reference in New Issue
Block a user