mirror of
https://github.com/graphql-python/graphene.git
synced 2025-03-05 20:35:46 +03:00
Added ability to return a Connection instance in the connection resolver
This commit is contained in:
parent
ad953f01a7
commit
c792923429
|
@ -117,21 +117,25 @@ class IterableConnectionField(Field):
|
||||||
).format(str(self), connection_type)
|
).format(str(self), connection_type)
|
||||||
return connection_type
|
return connection_type
|
||||||
|
|
||||||
@staticmethod
|
@classmethod
|
||||||
def connection_resolver(resolver, connection, root, args, context, info):
|
def connection_resolver(cls, resolver, connection, root, args, context, info):
|
||||||
iterable = resolver(root, args, context, info)
|
resolved = resolver(root, args, context, info)
|
||||||
assert isinstance(iterable, Iterable), (
|
|
||||||
'Resolved value from the connection field have to be iterable. '
|
if isinstance(resolved, connection):
|
||||||
|
return resolved
|
||||||
|
|
||||||
|
assert isinstance(resolved, Iterable), (
|
||||||
|
'Resolved value from the connection field have to be iterable or instance of {}. '
|
||||||
'Received "{}"'
|
'Received "{}"'
|
||||||
).format(iterable)
|
).format(connection, resolved)
|
||||||
connection = connection_from_list(
|
connection = connection_from_list(
|
||||||
iterable,
|
resolved,
|
||||||
args,
|
args,
|
||||||
connection_type=connection,
|
connection_type=connection,
|
||||||
edge_type=connection.Edge,
|
edge_type=connection.Edge,
|
||||||
pageinfo_type=PageInfo
|
pageinfo_type=PageInfo
|
||||||
)
|
)
|
||||||
connection.iterable = iterable
|
connection.iterable = resolved
|
||||||
return connection
|
return connection
|
||||||
|
|
||||||
def get_resolver(self, parent_resolver):
|
def get_resolver(self, parent_resolver):
|
||||||
|
|
|
@ -3,7 +3,7 @@ from collections import OrderedDict
|
||||||
from graphql_relay.utils import base64
|
from graphql_relay.utils import base64
|
||||||
|
|
||||||
from ...types import ObjectType, Schema, String
|
from ...types import ObjectType, Schema, String
|
||||||
from ..connection import ConnectionField
|
from ..connection import ConnectionField, PageInfo
|
||||||
from ..node import Node
|
from ..node import Node
|
||||||
|
|
||||||
letter_chars = ['A', 'B', 'C', 'D', 'E']
|
letter_chars = ['A', 'B', 'C', 'D', 'E']
|
||||||
|
@ -19,11 +19,26 @@ class Letter(ObjectType):
|
||||||
|
|
||||||
class Query(ObjectType):
|
class Query(ObjectType):
|
||||||
letters = ConnectionField(Letter)
|
letters = ConnectionField(Letter)
|
||||||
|
connection_letters = ConnectionField(Letter)
|
||||||
|
|
||||||
|
node = Node.Field()
|
||||||
|
|
||||||
def resolve_letters(self, args, context, info):
|
def resolve_letters(self, args, context, info):
|
||||||
return list(letters.values())
|
return list(letters.values())
|
||||||
|
|
||||||
node = Node.Field()
|
def resolve_connection_letters(self, args, context, info):
|
||||||
|
return Letter.Connection(
|
||||||
|
page_info=PageInfo(
|
||||||
|
has_next_page=True,
|
||||||
|
has_previous_page=False
|
||||||
|
),
|
||||||
|
edges=[
|
||||||
|
Letter.Connection.Edge(
|
||||||
|
node=Letter(id=0, letter='A'),
|
||||||
|
cursor='a-cursor'
|
||||||
|
),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
schema = Schema(Query)
|
schema = Schema(Query)
|
||||||
|
@ -176,3 +191,40 @@ def test_returns_all_elements_if_cursors_are_on_the_outside():
|
||||||
|
|
||||||
def test_returns_no_elements_if_cursors_cross():
|
def test_returns_no_elements_if_cursors_cross():
|
||||||
check('before: "{}" after: "{}"'.format(base64('arrayconnection:%s' % 2), base64('arrayconnection:%s' % 4)), '')
|
check('before: "{}" after: "{}"'.format(base64('arrayconnection:%s' % 2), base64('arrayconnection:%s' % 4)), '')
|
||||||
|
|
||||||
|
|
||||||
|
def test_connection_type_nodes():
|
||||||
|
result = schema.execute('''
|
||||||
|
{
|
||||||
|
connectionLetters {
|
||||||
|
edges {
|
||||||
|
node {
|
||||||
|
id
|
||||||
|
letter
|
||||||
|
}
|
||||||
|
cursor
|
||||||
|
}
|
||||||
|
pageInfo {
|
||||||
|
hasPreviousPage
|
||||||
|
hasNextPage
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
''')
|
||||||
|
|
||||||
|
assert not result.errors
|
||||||
|
assert result.data == {
|
||||||
|
'connectionLetters': {
|
||||||
|
'edges': [{
|
||||||
|
'node': {
|
||||||
|
'id': 'TGV0dGVyOjA=',
|
||||||
|
'letter': 'A',
|
||||||
|
},
|
||||||
|
'cursor': 'a-cursor',
|
||||||
|
}],
|
||||||
|
'pageInfo': {
|
||||||
|
'hasPreviousPage': False,
|
||||||
|
'hasNextPage': True,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user