Skip to content

Commit

Permalink
Improved Promise connection abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
syrusakbary committed Oct 27, 2016
1 parent 16e9f22 commit 760ccc8
Showing 1 changed file with 26 additions and 21 deletions.
47 changes: 26 additions & 21 deletions graphene/relay/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,29 +118,34 @@ def type(self):
).format(str(self), 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
def connection_resolver(cls, resolver, connection_type, root, args, context, info):
p = Promise.resolve(resolver(root, args, context, info))

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_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)
resolved = resolver(root, args, context, info)

on_resolve = partial(cls.resolve_connection, connection_type, args)
if isinstance(resolved, Promise):
return resolved.then(on_resolve)

return on_resolve(resolved)

def get_resolver(self, parent_resolver):
resolver = super(IterableConnectionField, self).get_resolver(parent_resolver)
Expand Down

0 comments on commit 760ccc8

Please sign in to comment.