From 90c88abb5d8f99f43efb245e56fda7ad15040405 Mon Sep 17 00:00:00 2001 From: Markus Padourek Date: Fri, 3 Jun 2016 10:27:54 +0100 Subject: [PATCH] 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` --- graphene/relay/types.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/graphene/relay/types.py b/graphene/relay/types.py index 3ab55770..ae3af0ad 100644 --- a/graphene/relay/types.py +++ b/graphene/relay/types.py @@ -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):