Attaches a sort specification to an enum value used by an @orderBy argument. Exactly one of index, fields, or primaryKey must be set on each enum value; together they tell the generator which columns to emit in ORDER BY.
SDL signature
directive @order(
index: String,
fields: [FieldSort!],
primaryKey: Boolean = false
) on ENUM_VALUE
input FieldSort {
name: String
path: [ReferenceElement!]
method: ExternalCodeReference
collate: String
direction: SortDirection
}
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
|
|
none |
Name of a database index. The generator looks up the index on the parent table at build time and emits its column list. Index code generation must be enabled in jOOQ ( |
|
|
none |
One or more sort specifications. Each |
|
|
|
When |
Canonical example
enum FilmSort {
FILM_ID @order(primaryKey: true)
TITLE @order(fields: [{name: "title"}])
}
enum ActorSort {
ACTOR_ID @order(primaryKey: true)
FIRST_NAME @order(fields: [{name: "first_name"}])
LAST_NAME @order(fields: [{name: "last_name"}])
}
FILM_ID @order(primaryKey: true) sorts by film.film_id, the primary key of the bound table. TITLE @order(fields: [{name: "title"}]) sorts by the title column without requiring a database index. On a connection, a value whose columns do not identify a row has the primary key appended for the cursor, so TITLE pages on title, film_id; see Stable cursors and tie-breakers. A column that admits NULL sorts its NULL rows last in either direction; see Where NULL sorts.
Index-based sorting (with optional multi-column index):
enum FilmOrderByField {
LANGUAGE @order(index: "IDX_FK_LANGUAGE_ID")
TITLE @order(index: "IDX_TITLE")
STORE_ID_FILM_ID @order(index: "idx_store_id_film_id")
}
Field-based sorting with collation:
enum PersonOrderByField {
NAME_NORSK @order(fields: [
{name: "LAST_NAME", collate: "xdanish_ai"},
{name: "FIRST_NAME", collate: "xdanish_ai"}
])
STATUS @order(fields: [{name: "ACTIVE"}])
}
Sorting by a related value
path: sorts by a column on a table reached through to-one hops, in the element shape of @reference(path:); method: sorts by a key a static method derives, with @externalField's calling convention (the field’s table in, a Field<X> out):
enum FilmLanguageSort {
ORIGINAL_LANGUAGE @order(fields: [{name: "name", path: [{key: "film_original_language_id_fkey"}]}])
LATEST_STOCK @order(fields: [{method: {className: "com.example.FilmSortKeys", method: "latestStockStore"}}])
}
ORIGINAL_LANGUAGE sorts films by the name of their original language, a film with none sorting last; LATEST_STOCK by a value the method computes from a film’s many inventory rows. On a connection neither counts toward identifying a row, so both page on the key and then film_id. See How-to: Sort by a value on another table for the to-one rule and the no-code alternative.
The enum’s parent type is determined by where the enum is referenced from (the @orderBy argument); column names resolve against that type’s @table.
Constraints
-
Exactly one of
index,fields, orprimaryKeymust be set per enum value. Setting two, or none, fails the build. -
indexrequires jOOQ index generation. With<includeIndexes>true</includeIndexes>disabled, the lookup throws at startup with a "no matching index" error. -
fields[].namemust be a real column on the bound table, or on the table itspathlands on (SORT_KEY_COLUMN_OFF_PATH). Resolution mirrors@field: case-sensitive against the jOOQ-generated identifiers. -
Each
FieldSortsets exactly one ofnameandmethod, andpathonly besidename. Any other combination fails the build. -
Every hop of a
pathmust be to-one, proven from the jOOQ catalog: the hop’s join columns cover a primary or unique key of the table it reaches. A to-many hop (SORT_KEY_PATH_TO_MANY) or a hop with acondition:(SORT_KEY_PATH_CONDITION) fails the build and points atmethod, and so does a key that does not join the table the path stands on (SORT_KEY_PATH_UNRESOLVED). -
A
methodnames a public static method taking the field’s jOOQ table and returning a parameterisedField<X>.methodis required inside the reference andargMappingis rejected. -
pathandmethodare not available on a multi-table interface or union, or over a table-valued function result. -
collateis passed verbatim to the database; misspelled collations surface only at query time. -
@orderapplies to enum values only. Use it on every value of an enum referenced by@orderBy; missing values fail the build. -
@indexis the deprecated single-purpose alias for@order(index:). New code should use@order.
See also
-
@orderByis the entry point that activates@order-bearing enums. -
@defaultOrderreuses the sameindex/fields/primaryKeyshape on the field rather than the enum. -
@indexis deprecated; migrate to@order(index:).