66from graphene .relay .connection import PageInfo
77from graphql_relay .connection .arrayconnection import connection_from_list_slice
88
9- from .utils import get_query
9+ from .utils import get_query , sort_argument_for_model
1010
1111
12- class SQLAlchemyConnectionField (ConnectionField ):
12+ class _UnsortedSQLAlchemyConnectionField (ConnectionField ):
1313
1414 @property
1515 def model (self ):
@@ -62,7 +62,17 @@ def get_resolver(self, parent_resolver):
6262 return partial (self .connection_resolver , parent_resolver , self .type , self .model )
6363
6464
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
6676
6777
6878def createConnectionField (_type ):
@@ -76,4 +86,4 @@ def registerConnectionFieldFactory(factoryMethod):
7686
7787def unregisterConnectionFieldFactory ():
7888 global __connectionFactory
79- __connectionFactory = SQLAlchemyConnectionField
89+ __connectionFactory = _UnsortedSQLAlchemyConnectionField
0 commit comments