Skip to content

Commit 760ccc8

Browse files
committed
Improved Promise connection abstraction
1 parent 16e9f22 commit 760ccc8

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

graphene/relay/connection.py

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -118,29 +118,34 @@ def type(self):
118118
).format(str(self), connection_type)
119119
return connection_type
120120

121+
@classmethod
122+
def resolve_connection(cls, connection_type, args, resolved):
123+
if isinstance(resolved, connection_type):
124+
return resolved
125+
126+
assert isinstance(resolved, Iterable), (
127+
'Resolved value from the connection field have to be iterable or instance of {}. '
128+
'Received "{}"'
129+
).format(connection_type, resolved)
130+
connection = connection_from_list(
131+
resolved,
132+
args,
133+
connection_type=connection_type,
134+
edge_type=connection_type.Edge,
135+
pageinfo_type=PageInfo
136+
)
137+
connection.iterable = resolved
138+
return connection
139+
121140
@classmethod
122141
def connection_resolver(cls, resolver, connection_type, root, args, context, info):
123-
p = Promise.resolve(resolver(root, args, context, info))
124-
125-
def resolve_connection(resolved):
126-
if isinstance(resolved, connection_type):
127-
return resolved
128-
129-
assert isinstance(resolved, Iterable), (
130-
'Resolved value from the connection field have to be iterable or instance of {}. '
131-
'Received "{}"'
132-
).format(connection_type, resolved)
133-
connection = connection_from_list(
134-
resolved,
135-
args,
136-
connection_type=connection_type,
137-
edge_type=connection_type.Edge,
138-
pageinfo_type=PageInfo
139-
)
140-
connection.iterable = resolved
141-
return connection
142-
143-
return p.then(resolve_connection)
142+
resolved = resolver(root, args, context, info)
143+
144+
on_resolve = partial(cls.resolve_connection, connection_type, args)
145+
if isinstance(resolved, Promise):
146+
return resolved.then(on_resolve)
147+
148+
return on_resolve(resolved)
144149

145150
def get_resolver(self, parent_resolver):
146151
resolver = super(IterableConnectionField, self).get_resolver(parent_resolver)

0 commit comments

Comments
 (0)