| 1 |
# Relay |
| 2 |
|
| 3 |
*apischema* provides some facilities to implement a GraphQL server following [*Relay* GraphQL server specification](https://relay.dev/docs/en/graphql-server-specification). They are included in the module `apischema.graphql.relay`. |
| 4 |
|
| 5 |
!!! note |
| 6 |
These facilities are independent of each others — you could keep only the mutations part and use your own identification and connection system for example. |
| 7 |
|
| 8 |
|
| 9 |
## (Global) Object Identification |
| 10 |
|
| 11 |
*apischema* defines a generic `relay.Node[Id]` interface which can be used which can be used as base class of all identified resources. This class contains a unique generic field of type `Id`, which will be automatically converted into an `ID!` in the schema. The `Id` type chosen has to be serializable into a string-convertible value (it can register [conversions](../conversions.md) if needed). |
| 12 |
|
| 13 |
Each node has to implement the `classmethod` `get_by_id(cls: type[T], id: Id, info: graphql.GraphQLResolveInfo=None) -> T`. |
| 14 |
|
| 15 |
All nodes defined can be retrieved using `relay.nodes`, while the `node` query is defined as `relay.node`. `relay.nodes()` can be passed to `graphql_schema` [`types` parameter](schema.md#additional-types) in order to add them in the schema even if they don't appear in any resolvers. |
| 16 |
|
| 17 |
|
| 18 |
```python |
| 19 |
{!relay_node.py!} |
| 20 |
``` |
| 21 |
|
| 22 |
!!! warning |
| 23 |
For now, even if its result is not used, `relay.nodes` must be called before generating the schema. |
| 24 |
|
| 25 |
### Global ID |
| 26 |
|
| 27 |
*apischema* defines a `relay.GlobalId` type with the following signature : |
| 28 |
|
| 29 |
```python |
| 30 |
@dataclass |
| 31 |
class GlobalId(Generic[Node]): |
| 32 |
id: str |
| 33 |
node_class: type[Node] |
| 34 |
``` |
| 35 |
In fact, it is `GlobalId` type which is serialized and deserialized as an `ID!`, not the `Id` parameter of the `Node` class; *apischema* automatically add a [field converter](../conversions.md#field-conversions) to make the conversion between the `Id` (for example an `UUID`) of a given node and the corresponding `GlobalId`. |
| 36 |
|
| 37 |
Node instance global id can be retrieved with `global_id` property. |
| 38 |
|
| 39 |
```python |
| 40 |
{!relay_global_id.py!} |
| 41 |
``` |
| 42 |
|
| 43 |
### Id encoding |
| 44 |
|
| 45 |
*Relay* specifications encourage the use of base64 encoding, so *apischema* defines a `relay.base64_encoding` that you can pass to `graphql_schema` `id_encoding` parameter. |
| 46 |
|
| 47 |
## Connections |
| 48 |
|
| 49 |
*apischema* provides a generic `relay.Connection[Node, Cursor, Edge]` type, which can be used directly without subclassing it; it's also possible to subclass it to add fields to a given connection (or to all the connection which will subclass the subclass). `relay.Edge[Node, Cursor]` can also be subclassed to add fields to the edges. |
| 50 |
|
| 51 |
`Connection` dataclass has the following declaration: |
| 52 |
```python |
| 53 |
@dataclass |
| 54 |
class Connection(Generic[Node, Cursor, Edge]): |
| 55 |
edges: Optional[Sequence[Optional[Edge]]] |
| 56 |
has_previous_page: bool = field(default=False, metadata=skip) |
| 57 |
has_next_page: bool = field(default=False, metadata=skip) |
| 58 |
start_cursor: Optional[Cursor] = field(default=None, metadata=skip) |
| 59 |
end_cursor: Optional[Cursor] = field(default=None, metadata=skip) |
| 60 |
|
| 61 |
@resolver |
| 62 |
def page_info(self) -> PageInfo[Cursor]: |
| 63 |
... |
| 64 |
``` |
| 65 |
|
| 66 |
The `pageInfo` field is computed by a resolver; it uses the cursors of the first and the last edge when they are not provided. |
| 67 |
|
| 68 |
Here is an example of `Connection` use: |
| 69 |
|
| 70 |
```python |
| 71 |
{!relay_connection.py!} |
| 72 |
``` |
| 73 |
|
| 74 |
### Custom connections/edges |
| 75 |
|
| 76 |
Connections can be customized by simply subclassing `relay.Connection` class and adding the additional fields. |
| 77 |
|
| 78 |
For the edges, `relay.Edge` can be subclassed too, and the subclass has then to be passed as type argument to the generic connection. |
| 79 |
|
| 80 |
|
| 81 |
```python |
| 82 |
{!relay_connection_subclass.py!} |
| 83 |
``` |
| 84 |
|
| 85 |
## Mutations |
| 86 |
|
| 87 |
*Relay* compliant mutations can be declared with a dataclass subclassing the `relay.Mutation` class; its fields will be put in the payload type of the mutation. |
| 88 |
|
| 89 |
This class must implement a `classmethod`/`staticmethod` name `mutate`; it can be synchronous or asynchronous. The arguments of the method will correspond to the input type fields. |
| 90 |
|
| 91 |
The mutation will be named after the name of the mutation class. |
| 92 |
|
| 93 |
All the mutations declared can be retrieved with `relay.mutations`, in order to be passed to `graphql_schema`. |
| 94 |
|
| 95 |
```python |
| 96 |
{!relay_mutation.py!} |
| 97 |
``` |
| 98 |
|
| 99 |
### ClientMutationId |
| 100 |
|
| 101 |
As you can see in the previous example, the field named `clientMutationId` is automatically added to the input and the payload types. |
| 102 |
|
| 103 |
The forward of the mutation id from the input to the payload is automatically handled. It's value can be accessed by declaring a parameter of type `relay.ClientMutationId` — even if the parameter is not named `client_mutation_id`, it will be renamed internally. |
| 104 |
|
| 105 |
This feature is controlled by a `Mutation` class variable `_client_mutation_id`, with 3 possible values: |
| 106 |
|
| 107 |
- `None` (automatic, the default): `clientMutationId` field will be nullable unless it's declared as a required parameter (without default value) in the `mutate` method. |
| 108 |
- `False`: their will be no `clientMutationId` field added (having a dedicated parameter will raise an error) |
| 109 |
- `True`: `clientMutationId` is added and forced to be non-null. |
| 110 |
|
| 111 |
```python |
| 112 |
{!relay_client_mutation_id.py!} |
| 113 |
``` |
| 114 |
|
| 115 |
### Error handling and other resolver arguments |
| 116 |
|
| 117 |
*Relay* mutation are [operations](schema.md#operations), so they can be configured with the same parameters. As they are declared as classes, parameters will be passed as class variables, prefixed by `_` (`error_handler` becomes `_error_handler`) |
| 118 |
|
| 119 |
!!! note |
| 120 |
Because parameters are class variables, you can reuse them by setting their value in a base class; for example, to share a same `error_handler` in a group of mutations. |
| 121 |
|
| 122 |
|