mirror of
https://github.com/graphql-python/graphene.git
synced 2025-07-23 22:39:58 +03:00
Added SimpleConnection to relay types
Sometimes you do not want a full blown relay connection, where you have `edges[...].node.attribute` but simply `edges[...].attribute`. According to https://github.com/graphql/graphql-relay-js/issues/27#issuecomment-142421989 relay has support for this simpler use-case as well if you do not need certain functionality such as deletion. This implements such a type that can be used instead of the default connection via the `connection_type` argument on `ConnectionField`
This commit is contained in:
parent
1711e6a529
commit
90c88abb5d
|
@ -103,6 +103,35 @@ class Connection(ObjectType):
|
|||
return self._connection_data
|
||||
|
||||
|
||||
class SimpleConnection(Connection):
|
||||
'''A connection without nodes to a list of items.'''
|
||||
|
||||
class Meta:
|
||||
type_name = 'SimpleConnection'
|
||||
|
||||
@classmethod
|
||||
@memoize
|
||||
def for_node(cls, node, edge_type=None):
|
||||
from graphene.relay.utils import is_node
|
||||
edge_type = edge_type or node
|
||||
assert is_node(node), 'ObjectTypes in a connection have to be Nodes'
|
||||
edges = List(edge_type, description='Information to aid in pagination.')
|
||||
return type(
|
||||
'%s%s' % (node._meta.type_name, cls._meta.type_name),
|
||||
(cls,),
|
||||
{'edge_type': edge_type, 'edges': edges})
|
||||
|
||||
@classmethod
|
||||
def from_list(cls, iterable, args, context, info):
|
||||
assert isinstance(
|
||||
iterable, Iterable), 'Resolved value from the connection field have to be iterable'
|
||||
connection = connection_from_list(
|
||||
iterable, args, simple_list=True, connection_type=cls,
|
||||
edge_type=cls.edge_type, pageinfo_type=PageInfo)
|
||||
connection.set_connection_data(iterable)
|
||||
return connection
|
||||
|
||||
|
||||
class NodeMeta(InterfaceMeta):
|
||||
|
||||
def construct_get_node(cls):
|
||||
|
|
Loading…
Reference in New Issue
Block a user