11from functools import partial
2-
2+ from promise import is_thenable , Promise
33from sqlalchemy .orm .query import Query
44
55from graphene .relay import ConnectionField
@@ -19,39 +19,38 @@ def model(self):
1919 def get_query (cls , model , info , ** args ):
2020 return get_query (model , info .context )
2121
22- @property
23- def type (self ):
24- from .types import SQLAlchemyObjectType
25- _type = super (ConnectionField , self ).type
26- assert issubclass (_type , SQLAlchemyObjectType ), (
27- "SQLAlchemyConnectionField only accepts SQLAlchemyObjectType types"
28- )
29- assert _type ._meta .connection , "The type {} doesn't have a connection" .format (_type .__name__ )
30- return _type ._meta .connection
31-
3222 @classmethod
33- def connection_resolver (cls , resolver , connection , model , root , info , ** args ):
34- iterable = resolver (root , info , ** args )
35- if iterable is None :
36- iterable = cls .get_query (model , info , ** args )
37- if isinstance (iterable , Query ):
38- _len = iterable .count ()
23+ def resolve_connection (cls , connection_type , model , info , args , resolved ):
24+ if resolved is None :
25+ resolved = cls .get_query (model , info , ** args )
26+ if isinstance (resolved , Query ):
27+ _len = resolved .count ()
3928 else :
40- _len = len (iterable )
29+ _len = len (resolved )
4130 connection = connection_from_list_slice (
42- iterable ,
31+ resolved ,
4332 args ,
4433 slice_start = 0 ,
4534 list_length = _len ,
4635 list_slice_length = _len ,
47- connection_type = connection ,
36+ connection_type = connection_type ,
4837 pageinfo_type = PageInfo ,
49- edge_type = connection .Edge ,
38+ edge_type = connection_type .Edge ,
5039 )
51- connection .iterable = iterable
40+ connection .iterable = resolved
5241 connection .length = _len
5342 return connection
5443
44+ @classmethod
45+ def connection_resolver (cls , resolver , connection_type , model , root , info , ** args ):
46+ resolved = resolver (root , info , ** args )
47+
48+ on_resolve = partial (cls .resolve_connection , connection_type , model , info , args )
49+ if is_thenable (resolved ):
50+ return Promise .resolve (resolved ).then (on_resolve )
51+
52+ return on_resolve (resolved )
53+
5554 def get_resolver (self , parent_resolver ):
5655 return partial (self .connection_resolver , parent_resolver , self .type , self .model )
5756
0 commit comments