From ea69be61174a7afcc3d61f36f30422fe75cbc687 Mon Sep 17 00:00:00 2001 From: Markus Padourek Date: Wed, 26 Oct 2016 10:50:27 +0100 Subject: [PATCH] Add support to return a promise for connections. --- graphene/relay/connection.py | 37 +++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/graphene/relay/connection.py b/graphene/relay/connection.py index e63478e5..05f4a68a 100644 --- a/graphene/relay/connection.py +++ b/graphene/relay/connection.py @@ -118,25 +118,28 @@ class IterableConnectionField(Field): return connection_type @classmethod - def connection_resolver(cls, resolver, connection, root, args, context, info): - resolved = resolver(root, args, context, info) + def connection_resolver(cls, resolver, connection_type, root, args, context, info): + p = Promise.resolve(resolver(root, args, context, info)) - if isinstance(resolved, connection): - return resolved + def resolve_connection(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, resolved) - connection = connection_from_list( - resolved, - args, - connection_type=connection, - edge_type=connection.Edge, - pageinfo_type=PageInfo - ) - connection.iterable = resolved - return connection + 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 + + return p.then(resolve_connection) def get_resolver(self, parent_resolver): resolver = super(IterableConnectionField, self).get_resolver(parent_resolver)