Improved Promise connection abstraction

This commit is contained in:
Syrus Akbary 2016-10-27 02:41:36 +02:00
parent 16e9f221b5
commit 760ccc8358

View File

@ -118,29 +118,34 @@ class IterableConnectionField(Field):
).format(str(self), connection_type) ).format(str(self), connection_type)
return connection_type return connection_type
@classmethod
def resolve_connection(cls, connection_type, args, resolved):
if isinstance(resolved, connection_type):
return resolved
assert isinstance(resolved, Iterable), (
'Resolved value from the connection field have to be iterable or instance of {}. '
'Received "{}"'
).format(connection_type, resolved)
connection = connection_from_list(
resolved,
args,
connection_type=connection_type,
edge_type=connection_type.Edge,
pageinfo_type=PageInfo
)
connection.iterable = resolved
return connection
@classmethod @classmethod
def connection_resolver(cls, resolver, connection_type, root, args, context, info): def connection_resolver(cls, resolver, connection_type, root, args, context, info):
p = Promise.resolve(resolver(root, args, context, info)) resolved = resolver(root, args, context, info)
def resolve_connection(resolved): on_resolve = partial(cls.resolve_connection, connection_type, args)
if isinstance(resolved, connection_type): if isinstance(resolved, Promise):
return resolved return resolved.then(on_resolve)
assert isinstance(resolved, Iterable), ( return on_resolve(resolved)
'Resolved value from the connection field have to be iterable or instance of {}. '
'Received "{}"'
).format(connection_type, resolved)
connection = connection_from_list(
resolved,
args,
connection_type=connection_type,
edge_type=connection_type.Edge,
pageinfo_type=PageInfo
)
connection.iterable = resolved
return connection
return p.then(resolve_connection)
def get_resolver(self, parent_resolver): def get_resolver(self, parent_resolver):
resolver = super(IterableConnectionField, self).get_resolver(parent_resolver) resolver = super(IterableConnectionField, self).get_resolver(parent_resolver)