Marks a list field as a Relay cursor connection. The schema-transform stage rewrites the field’s return type from [T!]! to a synthesized TConnection carrying edges, nodes, pageInfo, and totalCount; the generator emits keyset-based seek/limit SQL with ROW_NUMBER() OVER (PARTITION BY …) for batched per-parent pagination. If the field doesn’t already declare pagination arguments, forward-pagination args (first: Int, after: String) are added automatically; backward (last/before) and dynamic @orderBy arguments coexist when present.
|
Names from inside the generator. This page names types from graphitron’s own classification model. They are accurate today, and worth knowing when a rejection message quotes one back at you, but they are not part of the authoring contract: the classification walk that produces them is being drained, and these names retire with it. What stays stable is the schema you write, the directive reference that describes it, and the diagnostics glossary. |
@asConnection works on three field shapes that the rewrite recognises distinctly: root list fields (one paginated query), child lists under a non-root type (per-parent paginated children), and union/interface lists (multi-table polymorphic pagination). A child connection is always fetched in a query of its own, batched across parents through a DataLoader, each parent paging its own rows; @splitQuery is inferred there, and writing it is redundant.
SDL signature
directive @asConnection(
defaultFirstValue: Int = 100
connectionName: String
) on FIELD_DEFINITION
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
|
|
|
Page size used when the client omits |
|
|
|
Deprecated. Override the synthesized connection type’s name. Each connection field should own its own type; sharing a Connection across fields conflates distinct semantic contexts (different parents, filters, orders) at the type level. The override remains supported as a transition mechanism for legacy schemas but is permanently discouraged; fields sharing a name must agree on its shape (see Constraints). See How-to: Cursor-paginated connections. |
Canonical examples
A bare list on a @table parent (Query.stores): @asConnection rewrites the return type to QueryStoresConnection, adds first:/after: arguments, and emits keyset pagination using @defaultOrder for the cursor:
type Query {
stores: [Store!]! @asConnection @defaultOrder(primaryKey: true)
}
A union-typed list (Address.occupantsConnection): the multi-table polymorphic emitter wraps the per-branch UNION ALL in a derived table so seek/limit and cursor pagination apply across the union; totalCount counts only this parent’s occupants:
type Address @table(name: "address") {
occupants: [AddressOccupant!]!
occupantsConnection: [AddressOccupant!]! @asConnection(defaultFirstValue: 5)
}
union AddressOccupant = Customer | Staff
Per-parent paginated children (Film.actorsConnection): a ROW_NUMBER() OVER (PARTITION BY parentInput.idx) envelope gives each parent its own paginated slice, and a DataLoader batches the dispatch:
type Film @table(name: "film") {
actorsConnection(first: Int, last: Int, after: String, before: String): ActorsConnection!
@reference(path: [{key : "film_actor_film_id_fkey"}, {key : "film_actor_actor_id_fkey"}])
@defaultOrder(primaryKey: true)
}
When the schema declares the connection type explicitly (the ActorsConnection! shape above), the rewrite reuses it instead of synthesising a fresh one, and the same holds for a declared Edge, PageInfo or facets type. A declared type is checked against what its shape requires, the Relay specification’s fields where it states them (edges and pageInfo on a connection, node and cursor on an edge, the four PageInfo fields) and graphitron’s own facets shape where none does; one that falls short fails the build with MINTED_TYPE_DECLARED_SHORT at the field. nodes and totalCount are optional. Otherwise a Connection type, an Edge type, and PageInfo are generated alongside the field, and their names are derived from the field’s coordinate: Query.stores above gets QueryStoresConnection and QueryStoresConnectionEdge. The deprecated connectionName: argument replaces the Connection name, and the Edge name follows it, so an override of SharedMoviesConnection mints SharedMoviesConnectionEdge. Both are the names the legacy generator emitted, so an upgraded schema publishes the connection types its clients already hold fragments against.
Federation directives on the field propagate onto the Connection, Edge, PageInfo and facet types graphitron generates for it. @shareable on any field makes the generated types shareable. @tag follows Apollo’s contract rule that a type’s tags also appear on every field returning it: a generated type carries the tags that every field it was generated for carries. A type generated for one field takes that field’s tags. A type shared by several fields (PageInfo, a Connection and Edge shared through connectionName:, a facet value type) takes only the tags they have in common, so a contract that excludes one of the fields' tags keeps the shared type for the others.
When the fields disagree, the shared type ends up with fewer tags than some of them, and the build reports shared-type-tags-narrowed. Contracts built by excluding tags want this outcome; disable the rule under <lint><disabledRules>. A contract built by including tags drops a type that carries none of them, so declare the type yourself with the tags it should have. A type you declare is used as written and inherits nothing.
Dynamic order via @orderBy composes naturally:
type Film @table(name: "film") {
actorsOrderedConnection(
order: [ActorOrderBy] @orderBy,
first: Int,
last: Int,
after: String,
before: String
): ActorsConnection!
@reference(path: [{key : "film_actor_film_id_fkey"}, {key : "film_actor_actor_id_fkey"}])
@defaultOrder(primaryKey: true)
}
input ActorOrderBy {
field: ActorSort!
direction: SortDirection
}
enum ActorSort {
ACTOR_ID @order(primaryKey: true)
LAST_NAME @order(fields: [{name : "last_name"}])
}
The cursor encodes the active order columns (the @orderBy selection if the client supplied one, otherwise @defaultOrder), including the primary key graphitron appends when those columns do not identify a row, so that rows tying on the listed columns are neither skipped nor repeated between pages; decoding rejects cursors whose embedded sort doesn’t match the request’s effective order. See Stable cursors and tie-breakers.
Constraints
-
Applies to list-typed fields. The runtime cardinality must be a list; single-cardinality
@asConnectionis rejected at classify time. -
The field must have a stable order: the primary key of the table its target type is bound to, an
@defaultOrderon the field, or a non-null, single-valued@orderByargument. A nullable@orderByargument a client may leave out does not count; without one of these, keyset pagination has no anchor and the build fails withLIST_ORDER_UNDETERMINED. -
A type the field would generate and the schema declares must have the fields its shape requires; see above and
MINTED_TYPE_DECLARED_SHORT. -
Every field generating a name must generate it the same way. Two fields sharing a
connectionName:over different element types, or aconnectionName:naming a type the field also generates in another role (PageInfo, or its own Edge), coin one name two ways; graphitron generates none of the contested names and fails the build withMINTED_NAME_CONTESTEDat each such field. The default<ParentType><FieldName>Connectionshape never does this, parent and field names jointly disambiguating. -
defaultFirstValue:is the page size used when the client omitsfirst:; it is not a cap. The cap is the<maxAllowedPageSize>plugin parameter: afirst:orlast:above it is served at it, withhasNextPage(orhasPreviousPage)true. See How-to: Cursor-paginated connections. -
A child connection is always materialized per parent through a
DataLoaderkeyed on the parent’s PK or FK, each parent paging its own rows.@splitQueryis inferred there; written beside@asConnectionon a child list, it is redundant and draws theSPLIT_QUERY_REDUNDANTwarning. -
On a field returning a union or interface, at least one implementer must be
@table-bound: the connection pages each table-bound participant’s rows, so one with none (an interface’s own@tablenot counting) has nothing to page and is rejected at build time (FIELD_WITHOUT_FETCHER). -
Inline
TableFieldplacement is not allowed under interface-or-union return types unless the surrounding type axis supports the multi-table emitter; the generator rejects mismatched placements at classify time. (See Graphitron Development Principles for the "no generator-side invariant goes unchecked at validate time" rule that backs this.)
See also
-
@splitQueryis the batched fetch a child connection always takes, inferred rather than written. -
@defaultOrderanchors the cursor when the client omits@orderBy. -
@orderBysupplies dynamic order; the cursor encodes the active selection. -
How-to: Cursor-paginated connections covers the one-field-one-connection rule,
totalCountperformance, and what the rewrite does about page-size caps.