File tree Expand file tree Collapse file tree 2 files changed +18
-5
lines changed
examples/flask_sqlalchemy Expand file tree Collapse file tree 2 files changed +18
-5
lines changed Original file line number Diff line number Diff line change @@ -34,10 +34,13 @@ class Meta:
34
34
35
35
class Query (graphene .ObjectType ):
36
36
node = relay .Node .Field ()
37
+ # Supports sorting only over one field
37
38
all_employees = SQLAlchemyConnectionField (Employee , sort = graphene .Argument (SortEnumEmployee ,
38
39
default_value = utils .EnumValue ('id_asc' , EmployeeModel .id .asc ())))
39
- all_roles = SQLAlchemyConnectionField (Role , sort = utils .sort_argument_for_model (RoleModel ))
40
- all_departments = SQLAlchemyConnectionField (Department )
40
+ # Add sort over multiple fields, sorting by default over the primary key
41
+ all_roles = SQLAlchemyConnectionField (Role )
42
+ # Disable sorting over this field
43
+ all_departments = SQLAlchemyConnectionField (Department , sort = None )
41
44
42
45
43
46
schema = graphene .Schema (query = Query , types = [Department , Employee , Role ])
Original file line number Diff line number Diff line change 6
6
from graphene .relay .connection import PageInfo
7
7
from graphql_relay .connection .arrayconnection import connection_from_list_slice
8
8
9
- from .utils import get_query
9
+ from .utils import get_query , sort_argument_for_model
10
10
11
11
12
- class SQLAlchemyConnectionField (ConnectionField ):
12
+ class _UnsortedSQLAlchemyConnectionField (ConnectionField ):
13
13
14
14
@property
15
15
def model (self ):
@@ -62,7 +62,17 @@ def get_resolver(self, parent_resolver):
62
62
return partial (self .connection_resolver , parent_resolver , self .type , self .model )
63
63
64
64
65
- __connectionFactory = SQLAlchemyConnectionField
65
+ class SQLAlchemyConnectionField (_UnsortedSQLAlchemyConnectionField ):
66
+
67
+ def __init__ (self , type , * args , ** kwargs ):
68
+ if 'sort' not in kwargs :
69
+ kwargs .setdefault ('sort' , sort_argument_for_model (type ._meta .model ))
70
+ elif kwargs ['sort' ] is None :
71
+ del kwargs ['sort' ]
72
+ super (SQLAlchemyConnectionField , self ).__init__ (type , * args , ** kwargs )
73
+
74
+
75
+ __connectionFactory = _UnsortedSQLAlchemyConnectionField
66
76
67
77
68
78
def createConnectionField (_type ):
You can’t perform that action at this time.
0 commit comments