Relay nodes and connections
Strawberry provides the main Relay patterns for GraphQL schemas: stable node IDs, node lookup, and cursor-based connections. These patterns give clients one way to refetch objects and move through lists without knowing how the server stores its data. See Federation and Relay support for the wider product view and Federation support for Apollo Federation features.
Node model
Node identity
The Node interface marks an object as refetchable through a global id: ID! field. A node type normally inherits from relay.Node and marks one source field with relay.NodeID. Strawberry keeps that source field out of the GraphQL schema and uses it to build the public ID. A type can instead override resolve_id when its ID needs custom logic.
The source ID belongs to the application data. The public ID also carries the GraphQL type name, so the same source value on two node types does not produce the same global ID.
flowchart LR
Source[Application object and local ID] --> Node[Node type]
Node --> Global[Global GraphQL ID]
Global --> Refetch[node lookup]
Refetch --> Object[Resolved node]
References: strawberry/…/types.py• L330-351 strawberry/…/types.py• L354-427
Global ID lifecycle
A Relay global ID is the Base64 form of TypeName:NodeID. Strawberry exposes this value as ID by default. The Python GlobalID value gives application code access to the type name and source ID, plus sync and async helpers that resolve the ID to a node.
The ID resolver first finds the named GraphQL type and then asks that node type to load the source ID. required controls whether a missing object is an error. ensure_type also checks that the result has the type expected by the operation. The schema configuration can restore the older GlobalID GraphQL scalar name when a client contract needs it.
| Value | Role |
|---|---|
type_name | GraphQL node type encoded in the ID |
node_id | Application ID encoded in the ID |
GlobalID | Python value that parses and resolves both parts |
ID | Default GraphQL scalar used for the public field and input |
References: strawberry/…/types.py• L71-327 strawberry/…/__init__.py• L2-14
Node lookup fields
The relay.node() helper adds a field that turns one or more global IDs into nodes. A single-node field can be required or optional. A list field can accept IDs for different node types, and its result follows the order of the requested IDs. Optional forms preserve missing objects as null.
Each node type supplies resolve_nodes as the lookup contract. The resolver receives source IDs as strings and must return nodes in the same order. It can return missing entries when lookup is optional. The single-node path uses the same contract through resolve_node unless the node type provides a more direct lookup.
References: strawberry/…/types.py• L507-626 strawberry/…/__init__.py• L1-14
Connection model
Connection and edge types
A Relay connection represents a paged list as a relationship with page data and per-item edges. Strawberry models it with a generic Connection[NodeType] that contains page_info and edges. Each Edge[NodeType] contains the item in node and the opaque cursor used to continue through the list.
Connection and edge types are GraphQL types. A custom connection can add fields such as counts or summaries. A custom edge can add data about the relationship while retaining its cursor and node fields.
The default list connection also uses the query selection to avoid resolving list edges when the client requests neither edges nor pageInfo. A query that needs either part receives the normal edge and page data.
flowchart TB
Field[Connection field] --> Connection[Connection]
Connection --> PageInfo[PageInfo]
Connection --> Edges[Edge list]
Edges --> Edge[Edge]
Edge --> Cursor[Cursor]
Edge --> Node[Node]
References: strawberry/…/types.py• L629-716 strawberry/…/types.py• L718-953 strawberry/…/utils.py• L73-98 strawberry/…/__init__.py• L2-14
Pagination fields
The connection field accepts four standard arguments. The returned PageInfo describes the visible window and supplies cursors for the next request.
| Field or argument | Meaning |
|---|---|
after | Start after this cursor |
before | Stop before this cursor |
first | Keep the first requested number of items |
last | Keep the last requested number of items |
startCursor | Cursor for the first returned edge, or null for an empty page |
endCursor | Cursor for the last returned edge, or null for an empty page |
hasNextPage | Whether more items exist after the returned page |
hasPreviousPage | Whether more items exist before the returned page |
The standard GraphQL names are camel-cased from the Python fields. Clients use endCursor with after for forward paging and startCursor with before for backward paging.
References: strawberry/…/types.py• L629-656 strawberry/…/types.py• L718-750
Connection pagination
The relay.connection() helper turns a resolver that returns nodes into a connection field. It adds the standard pagination arguments and passes the resolver's iterable to the selected connection type. Resolvers may return synchronous or asynchronous iterables. They may also accept extra arguments for filtering before pagination.
ListConnection supplies the standard list pagination behavior. The base Connection is the extension point for a different paging rule. A custom subclass implements resolve_connection and builds its own edges and PageInfo. A connection may also override resolve_node when its source items need conversion to the public node type.
Strawberry limits the number of items that a connection can resolve. The schema-wide relay_max_results setting defaults to 100, and a field can set a more specific max_results value.
References: strawberry/…/types.py• L679-750 strawberry/…/types.py• L753-953 strawberry/…/__init__.py• L1-14
Relay utilities
Cursor encoding
The public to_base64 and from_base64 helpers encode and decode the two-part values used by global IDs and connection cursors. to_base64 accepts a GraphQL type name, a Strawberry type, or a type definition. from_base64 returns the type part and the source value and rejects values that do not contain the expected separator.
Global IDs use the node type name. List connections use a connection cursor prefix so their position cursors remain distinct from node IDs. A custom edge can set its own prefix, but all cursors sent to and read from that connection must use the same prefix.
References: strawberry/…/types.py• L658-677 strawberry/…/utils.py• L15-70 strawberry/…/__init__.py• L14-33
Pagination window
The pagination helper converts before, after, first, and last into a bounded result window. It also applies the schema or field limit and reserves one extra item when needed to decide whether hasNextPage is true. Invalid cursor prefixes and negative or over-limit counts fail before the connection is built.
This shared rule keeps the default connection behavior consistent for lists, generators, and async sources. The default list connection also checks whether the query asks for edges or pageInfo before it resolves edge nodes. A custom connection owns its own pagination rule and must keep its cursor and PageInfo values consistent with that rule.
References: strawberry/…/utils.py• L73-183 strawberry/…/types.py• L770-953 strawberry/…/__init__.py• L1-33
Change checklist
When these files or sections are changed, remember to consider these:
- Changing a node's GraphQL name or source ID changes every generated global ID. Check stored IDs, client caches, and links that use those values.
- Changing a custom edge cursor prefix changes the accepted
beforeandaftervalues. Treat the change as a cursor contract change for existing clients. - Changing
NodeID,resolve_id, orresolve_typenamechanges the public node identity without changing the object fields. - Changing
resolve_nodesmust preserve input order and the required-versus-optional missing-node contract. - Changing connection or edge generics can change the generated connection, edge, and node types together. Check client queries that select those types.
- Changing
relay_max_resultsor a field-levelmax_resultschanges how many items a request can receive and how clients must advance pages. - Changing
relay_use_legacy_global_idchanges the GraphQL scalar name forGlobalIDinputs and outputs. Check clients that declare variables with that scalar. - Changing connection selection checks can change whether node resolvers run for queries that request only connection metadata or custom fields.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
A Node type has no NodeID field or has more than one | Schema creation raises a node ID annotation error | Mark exactly one source field with relay.NodeID, or override resolve_id |
A global ID cannot be decoded or names a non-Node GraphQL type | Node resolution fails with a global ID value error | Create IDs with the node type name and use to_base64 or GlobalID parsing |
| A required node ID is missing | The lookup raises instead of returning a node | Use optional node fields when absence is valid, and return None only for optional lookups |
A connection field is not typed as a Connection subclass | Schema creation raises a Relay annotation error | Use relay.Connection[T], relay.ListConnection[T], or a connection subclass |
| A connection resolver returns a non-iterable value | Schema creation raises a resolver annotation error | Annotate and return a list, iterable, iterator, generator, or async equivalent of the node type |
| A cursor has the wrong prefix or a count is negative or above the limit | Pagination rejects the request | Keep cursor prefixes stable and keep first and last within the configured limit |
Key files
| File | Why to read it |
|---|---|
| strawberry/…/types.py | Defines Node, NodeID, GlobalID, PageInfo, Edge, Connection, and the default list connection behavior |
| strawberry/…/fields.py | Defines the node() and connection() schema helpers and their resolver contracts |
| strawberry/…/utils.py | Defines Base64 ID and cursor helpers, selection checks, and the shared pagination window rules |
| strawberry/…/__init__.py | Shows the public Relay API exported from strawberry.relay |
| strawberry/…/config.py | Defines the schema-wide result limit and legacy global ID switch |
| docs/federation-and-relay.md | Places Relay nodes and connections in the wider schema feature set |
| docs/graphql-execution.md | Explains how Relay field results enter GraphQL execution |