99from graphene .relay .connection import PageInfo
1010from graphql_relay .connection .arrayconnection import connection_from_list_slice
1111
12+ from .batching import get_batch_resolver
1213from .utils import get_query
1314
1415
@@ -33,14 +34,8 @@ def model(self):
3334 return self .type ._meta .node ._meta .model
3435
3536 @classmethod
36- def get_query (cls , model , info , sort = None , ** args ):
37- query = get_query (model , info .context )
38- if sort is not None :
39- if isinstance (sort , six .string_types ):
40- query = query .order_by (sort .value )
41- else :
42- query = query .order_by (* (col .value for col in sort ))
43- return query
37+ def get_query (cls , model , info , ** args ):
38+ return get_query (model , info .context )
4439
4540 @classmethod
4641 def resolve_connection (cls , connection_type , model , info , args , resolved ):
@@ -78,6 +73,7 @@ def get_resolver(self, parent_resolver):
7873 return partial (self .connection_resolver , parent_resolver , self .type , self .model )
7974
8075
76+ # TODO Rename this to SortableSQLAlchemyConnectionField
8177class SQLAlchemyConnectionField (UnsortedSQLAlchemyConnectionField ):
8278 def __init__ (self , type , * args , ** kwargs ):
8379 if "sort" not in kwargs and issubclass (type , Connection ):
@@ -95,6 +91,32 @@ def __init__(self, type, *args, **kwargs):
9591 del kwargs ["sort" ]
9692 super (SQLAlchemyConnectionField , self ).__init__ (type , * args , ** kwargs )
9793
94+ @classmethod
95+ def get_query (cls , model , info , sort = None , ** args ):
96+ query = get_query (model , info .context )
97+ if sort is not None :
98+ if isinstance (sort , six .string_types ):
99+ query = query .order_by (sort .value )
100+ else :
101+ query = query .order_by (* (col .value for col in sort ))
102+ return query
103+
104+
105+ class BatchSQLAlchemyConnectionField (UnsortedSQLAlchemyConnectionField ):
106+ """
107+ This is currently experimental.
108+ The API and behavior may change in future versions.
109+ Use at your own risk.
110+ """
111+ def get_resolver (self , parent_resolver ):
112+ return partial (self .connection_resolver , self .resolver , self .type , self .model )
113+
114+ @classmethod
115+ def from_relationship (cls , relationship , registry , ** field_kwargs ):
116+ model = relationship .mapper .entity
117+ model_type = registry .get_type_for_model (model )
118+ return cls (model_type ._meta .connection , resolver = get_batch_resolver (relationship ), ** field_kwargs )
119+
98120
99121def default_connection_field_factory (relationship , registry , ** field_kwargs ):
100122 model = relationship .mapper .entity
0 commit comments