Global Object IDs🔗

In this section, we'll cover how to add support for object refetching and client caching using the Relay Global Object Identification specification.

For Relay-compliant clients, see the Connection section for adding support for pagination with Connections.

Node Interface🔗

Your QueryTypes can implement the Node interface to add support for Global Object IDs.

1
2
3
4
5
6
7
from undine import QueryType
from undine.relay import Node

from .models import Task


class TaskType(QueryType[Task], interfaces=[Node]): ...

This will add an id field to the TaskType for resolving Global Object IDs.

1
2
3
4
5
6
7
8
9
interface Node {
  id: ID!
}

type TaskType implements Node {
  id: ID!
  name: String!
  # Rest of the fields...
}

Note that most Django models already contain an id field for as the primary key of the table, and that implementing this interface will override it with the Global Object ID field. To access the model id field, you can use the pk field instead.

Node Entrypoint🔗

A Global Object ID can be used for refetching objects from a special Node Entrypoint.

from undine import Entrypoint, QueryType
from undine.relay import Connection, Node

from .models import Task


class TaskType(QueryType[Task], interfaces=[Node]): ...


class Query(QueryType):
    node = Entrypoint(Node)
    tasks = Entrypoint(Connection(TaskType))

To use this Entrypoint, we must first query the schema in some other way, for example using the tasks Connection Entrypoint in the above example. Then, we can use the fetched Global Object ID to refetch the Task from the Node Entrypoint.

1
2
3
4
5
6
7
8
query {
  node(id: "U3Vyc29yOnVzZXJuYW1lOjE=") {
    id
    ... on TaskType {
      name
    }
  }
}

Note that Global Object IDs (e.g. U3Vyc29yOnVzZXJuYW1lOjE= in the above example) are meant to be opaque to the client, meaning they aren't supposed to know what they contain or how to parse them.