File: connection.rst

package info (click to toggle)
python-graphene 3.4.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,120 kB
  • sloc: python: 8,935; makefile: 214; sh: 18
file content (45 lines) | stat: -rw-r--r-- 1,369 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
Connection
==========

A connection is a vitaminized version of a List that provides ways of
slicing and paginating through it. The way you create Connection types
in ``graphene`` is using ``relay.Connection`` and ``relay.ConnectionField``.

Quick example
-------------

If we want to create a custom Connection on a given node, we have to subclass the
``Connection`` class.

In the following example, ``extra`` will be an extra field in the connection,
and ``other`` an extra field in the Connection Edge.

.. code:: python

    class ShipConnection(Connection):
        extra = String()

        class Meta:
            node = Ship

        class Edge:
            other = String()

The ``ShipConnection`` connection class, will have automatically a ``pageInfo`` field,
and a ``edges`` field (which is a list of ``ShipConnection.Edge``).
This ``Edge`` will have a ``node`` field linking to the specified node
(in ``ShipConnection.Meta``) and the field ``other`` that we defined in the class.

Connection Field
----------------
You can create connection fields in any Connection, in case any ObjectType
that implements ``Node`` will have a default Connection.

.. code:: python

    class Faction(graphene.ObjectType):
        name = graphene.String()
        ships = relay.ConnectionField(ShipConnection)

        def resolve_ships(root, info):
            return []