|
| 1 | +--- |
| 2 | +title: Relay |
| 3 | +description: A Relay implementation in Graphene |
| 4 | +--- |
| 5 | + |
| 6 | +# Relay |
| 7 | + |
| 8 | +Graphene have complete support of [Relay](https://facebook.github.io/relay/docs/graphql-relay-specification.html), and it offers some utils for working if from Python easy. |
| 9 | + |
| 10 | +## Nodes |
| 11 | + |
| 12 | +A `Node` is an Interface provided by `graphene.relay` that contains a single field `id` (which is a `ID!`). Any object that inherits from it have to implement a `get_node` method for retrieving a `Node` by an *id*. |
| 13 | + |
| 14 | +Example usage (taken from the [Starwars Relay example](https://github.com/graphql-python/graphene/blob/master/examples/starwars_relay/schema.py)): |
| 15 | + |
| 16 | +```python |
| 17 | +class Ship(relay.Node): |
| 18 | + '''A ship in the Star Wars saga''' |
| 19 | + name = graphene.String(description='The name of the ship.') |
| 20 | + |
| 21 | + @classmethod |
| 22 | + def get_node(cls, id, info): |
| 23 | + return get_ship(id) |
| 24 | +``` |
| 25 | + |
| 26 | +The `id` returned by the `Ship` type when you query it will be a scalar which contains the enough info for the server for knowing it's type and it's id. |
| 27 | + |
| 28 | +For example, the instance `Ship(id=1)` will return `U2hpcDox` as the id when you query it (which is the base64 encoding of `Ship:1`), and which could be useful later if we want to query a node by its id. |
| 29 | + |
| 30 | + |
| 31 | +## Connection |
| 32 | + |
| 33 | +A connection is a vitaminized version of a List that provides ways of slicing and paginating through it. The way you create Connection fields in `graphene` is using `relay.ConnectionField`. |
| 34 | + |
| 35 | +You can create connection fields in any ObjectType, but the connection **must** be linked to an object which inherits from `Node` (in this case, a `Ship`). |
| 36 | + |
| 37 | +```python |
| 38 | +class Faction(graphene.ObjectType): |
| 39 | + name = graphene.String() |
| 40 | + ships = relay.ConnectionField(Ship) |
| 41 | + |
| 42 | + def resolve_ships(self, args, info): |
| 43 | + return [] |
| 44 | +``` |
| 45 | + |
| 46 | +## Node Root field |
| 47 | + |
| 48 | +As is required in the [Relay specification](https://facebook.github.io/relay/graphql/objectidentification.htm#sec-Node-root-field), the server must implement a root field called `node` that returns a `Node` Interface. |
| 49 | + |
| 50 | +For this reason, `graphene` provides the field `relay.NodeField`, which links to any type in the Schema which inherits from `Node`. Example usage: |
| 51 | + |
| 52 | +```python |
| 53 | +class Query(graphene.ObjectType): |
| 54 | + node = relay.NodeField() |
| 55 | +``` |
| 56 | + |
| 57 | + |
| 58 | +## Useful links |
| 59 | + |
| 60 | +* [Getting started with Relay](https://facebook.github.io/relay/docs/graphql-relay-specification.html) |
| 61 | +* [Relay Global Identification Specification](https://facebook.github.io/relay/graphql/objectidentification.htm) |
| 62 | +* [Relay Cursor Connection Specification](https://facebook.github.io/relay/graphql/connections.htm) |
| 63 | +* [Relay input Object Mutation](https://facebook.github.io/relay/graphql/mutations.htm) |
0 commit comments