Specifies a static ORDER BY for a field. When the field also carries an @orderBy argument and the client supplies a value, the dynamic order wins; otherwise @defaultOrder applies. Without an explicit @defaultOrder, paginated fields default to ascending primary-key order.
SDL signature
directive @defaultOrder(
index: String,
fields: [FieldSort!],
primaryKey: Boolean = false,
direction: SortDirection = ASC
) on FIELD_DEFINITION
input FieldSort {
name: String!
collate: String
}
enum SortDirection { ASC, DESC }
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
|
|
none |
Name of a database index. The generator emits the index’s column list as the sort specification. Requires |
|
|
none |
Explicit column list. Each |
|
|
|
When |
|
|
|
Sort direction applied to every column in the spec. |
Exactly one of index, fields, or primaryKey must be set.
Canonical example
The example schema’s connection fields default to primary-key order:
type Query {
stores: [Store!]! @asConnection @defaultOrder(primaryKey: true)
filmsConnection(
first: Int, last: Int, after: String, before: String
): FilmsConnection! @defaultOrder(primaryKey: true)
filmsOrderedConnection(
rating: MpaaRating @field(name: "RATING"),
order: [FilmOrderBy] @orderBy,
first: Int, last: Int, after: String, before: String
): FilmsConnection! @defaultOrder(primaryKey: true)
}
stores and filmsConnection have no @orderBy argument: every page is sorted by primary key. filmsOrderedConnection accepts a dynamic order argument; when the client omits it, the field falls back to @defaultOrder(primaryKey: true). The keyset-pagination cursor binds to whichever order is active.
Field-based with collation:
type Query {
customers(first: Int = 100, after: String): CustomerConnection
@defaultOrder(fields: [
{name: "LAST_NAME", collate: "case_insensitive"},
{name: "FIRST_NAME", collate: "case_insensitive"}
])
}
Index-based with descending direction:
type Query {
films(orderBy: FilmOrder @orderBy, first: Int = 100, after: String): FilmConnection
@defaultOrder(index: "IDX_TITLE", direction: DESC)
}
Constraints
-
Exactly one of
index,fields, orprimaryKeymust be set. Multiple or none fails the build. -
Applies to fields only. Apply it to the connection-bearing field, not the synthesised
Connection/Edgetypes. -
When combined with
@orderByon the same field, the client’s argument value (when present) takes precedence;@defaultOrderis the fallback. -
directionis global across the spec. Per-column direction requires@orderBy(the enum input carries its ownSortDirection). -
indexrequires jOOQ index generation.fields[].nameresolves against the field’s return-type table, not the parent. -
Stable keyset pagination depends on the order being deterministic.
primaryKey: trueis the safe default; field-based orders need a tie-breaker (typically the PK) to avoid unstable cursors.
See also
-
@orderBysupplies the dynamic counterpart. -
@orderreuses theindex/fields/primaryKeyshape on enum values. -
@asConnectionfor connection synthesis;@defaultOrderis required for stable pagination on synthesised connections. -
How-to: Sort results covers tie-breakers, collation pitfalls, and pagination semantics.