-
Notifications
You must be signed in to change notification settings - Fork 228
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #101 from CaselIT/sortable_field
Added sort support
- Loading branch information
Showing
10 changed files
with
466 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,60 @@ | ||
import graphene | ||
from graphene import relay | ||
from graphene_sqlalchemy import SQLAlchemyConnectionField, SQLAlchemyObjectType | ||
from graphene_sqlalchemy import SQLAlchemyConnectionField, SQLAlchemyObjectType, utils | ||
from models import Department as DepartmentModel | ||
from models import Employee as EmployeeModel | ||
from models import Role as RoleModel | ||
|
||
|
||
class Department(SQLAlchemyObjectType): | ||
|
||
class Meta: | ||
model = DepartmentModel | ||
interfaces = (relay.Node, ) | ||
|
||
|
||
class Employee(SQLAlchemyObjectType): | ||
class DepartmentConnection(relay.Connection): | ||
class Meta: | ||
node = Department | ||
|
||
|
||
class Employee(SQLAlchemyObjectType): | ||
class Meta: | ||
model = EmployeeModel | ||
interfaces = (relay.Node, ) | ||
|
||
|
||
class Role(SQLAlchemyObjectType): | ||
class EmployeeConnection(relay.Connection): | ||
class Meta: | ||
node = Employee | ||
|
||
|
||
class Role(SQLAlchemyObjectType): | ||
class Meta: | ||
model = RoleModel | ||
interfaces = (relay.Node, ) | ||
|
||
|
||
class RoleConnection(relay.Connection): | ||
class Meta: | ||
node = Role | ||
|
||
|
||
SortEnumEmployee = utils.sort_enum_for_model(EmployeeModel, 'SortEnumEmployee', | ||
lambda c, d: c.upper() + ('_ASC' if d else '_DESC')) | ||
|
||
|
||
class Query(graphene.ObjectType): | ||
node = relay.Node.Field() | ||
all_employees = SQLAlchemyConnectionField(Employee) | ||
all_roles = SQLAlchemyConnectionField(Role) | ||
role = graphene.Field(Role) | ||
# Allow only single column sorting | ||
all_employees = SQLAlchemyConnectionField( | ||
EmployeeConnection, | ||
sort=graphene.Argument( | ||
SortEnumEmployee, | ||
default_value=utils.EnumValue('id_asc', EmployeeModel.id.asc()))) | ||
# Allows sorting over multiple columns, by default over the primary key | ||
all_roles = SQLAlchemyConnectionField(RoleConnection) | ||
# Disable sorting over this field | ||
all_departments = SQLAlchemyConnectionField(DepartmentConnection, sort=None) | ||
|
||
|
||
schema = graphene.Schema(query=Query, types=[Department, Employee, Role]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from graphene.relay import Connection | ||
import pytest | ||
|
||
from ..fields import SQLAlchemyConnectionField | ||
from ..types import SQLAlchemyObjectType | ||
from ..utils import sort_argument_for_model | ||
from .models import Pet as PetModel, Editor | ||
|
||
|
||
class Pet(SQLAlchemyObjectType): | ||
class Meta: | ||
model = PetModel | ||
|
||
|
||
class PetConn(Connection): | ||
class Meta: | ||
node = Pet | ||
|
||
|
||
def test_sort_added_by_default(): | ||
arg = SQLAlchemyConnectionField(PetConn) | ||
assert 'sort' in arg.args | ||
assert arg.args['sort'] == sort_argument_for_model(PetModel) | ||
|
||
|
||
def test_sort_can_be_removed(): | ||
arg = SQLAlchemyConnectionField(PetConn, sort=None) | ||
assert 'sort' not in arg.args | ||
|
||
|
||
def test_custom_sort(): | ||
arg = SQLAlchemyConnectionField(PetConn, sort=sort_argument_for_model(Editor)) | ||
assert arg.args['sort'] == sort_argument_for_model(Editor) | ||
|
||
|
||
def test_init_raises(): | ||
with pytest.raises(Exception, match='Cannot create sort'): | ||
SQLAlchemyConnectionField(Connection) |
Oops, something went wrong.