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
path: [ReferenceElement!]
method: ExternalCodeReference
collate: String
direction: SortDirection
}
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 sort 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)
}
stores and filmsConnection have no @orderBy argument: every page is sorted by primary key. A field that also takes an @orderBy argument falls back to its @defaultOrder when the client omits that argument. The keyset-pagination cursor binds to whichever order is active.
Field-based with collation:
type Query {
customers: [Customer!]!
@asConnection
@defaultOrder(fields: [{name : "last_name", collate : "case_insensitive"}, {name : "first_name", collate : "case_insensitive"}])
}
By a column on a related table, here the name of each film’s original language:
type Query {
filmsByOriginalLanguage: [Film!]!
@asConnection
@defaultOrder(fields: [{name : "name", path : [{key : "film_original_language_id_fkey"}]}])
}
Index-based with descending direction:
type Query {
filmsByTitleDescending: [Film!]! @asConnection @defaultOrder(index: "idx_title", direction: DESC)
}
On a plural mutation backed by a routine
A mutation field taking a list argument and returning a list answers one item per element of that argument, in the order of the input. A field of that shape behind a @routine that returns a table reads the routine’s result, and the rows of a table have no order of their own. On such a field, @defaultOrder states the order the routine’s result keeps: fields: names columns of the routine’s result, not of the table the field’s type is bound to, and each name resolves against those columns.
type Mutation {
rentFilmsInOrder(inventoryIds: [Int!]!, customerId: Int!): [Rental!]!
@routine(
name: "rent_films_in_order"
argMapping: "pInventoryIds: inventoryIds, pCustomerId: customerId"
)
@reference(path: [{table: "rental"}])
@defaultOrder(fields: [{name: "input_index"}])
}
Here rent_films_in_order returns TABLE(rental_id INTEGER, input_index INTEGER), input_index being each rental’s place in inventoryIds. The written rows are read back from rental in the order of input_index, so the answer follows the input. Which column keeps the input’s order is the routine’s to say; the column of the bound table, such as rental_id, cannot say which element a row answers.
Only arguments that are lists count toward the shape, so scalar arguments beside the list, customerId here, leave it a plural mutation.
-
Only
fields:applies. A routine’s result has no primary key and no index, soprimaryKey: truefails the build withORDER_PRIMARY_KEY_MISSINGandindex:withELEMENT_UNRESOLVED. -
A
namethat is not a column of the routine’s result fails the build withCOLUMN_UNRESOLVED, naming it. -
A
fieldselement with amethod:or apath:names no column of the routine’s result and fails the build withROUTINE_WRITE_PLURAL_ORDER_NOT_A_COLUMN. -
A field of this shape with no
@defaultOrderfails the build withROUTINE_WRITE_PLURAL_UNORDERED, naming the list argument. -
An
@orderByargument is not accepted on a@routinemutation field (READ_SURFACE_ON_WRITE): a client-chosen order would break the order the schema states.
Constraints
-
Exactly one of
index,fields, orprimaryKeymust be set. Multiple or none fails the build. -
fieldsnames at least one field. An empty list,fields: [], names nothing to sort by and fails the build at the application (ORDER_FIELDS_EMPTY), wherever the field is read; so does an@ordervalue written the same way. -
primaryKey: trueneeds the target table to have one. Over a table with no primary key it fails the build, naming the table and listing the columns available, so thefields:spelling is one edit away. A function’s result is the standing case: its type is bound to no table unless its own@tablebinds it, so there is no key, and a routine-backed field whose path ends on the routine orders by naming the function’s own result columns (see@routine). -
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, or against the table itspathlands on. -
A
pathfollows to-one hops only and carries nocondition:, each hop’s key joining the table the path stands on (SORT_KEY_PATH_TO_MANY,SORT_KEY_PATH_CONDITION,SORT_KEY_PATH_UNRESOLVED); amethodtakes the field’s table and returns aField<X>. Neither counts toward a connection’s row identity, and neither is available on a multi-table interface or union or over a table-valued function result. See How-to: Sort by a value on another table. -
On a root field returning a list or connection of a multi-table interface or union, the columns resolve against each participant’s own table, and every participant has to carry them at the same SQL type and bound Java type, position by position (
ORDER_POSITIONS_DISAGREE). A child field, or a single-valued root field, over such a type rejects the directive. See How-to: Sort across polymorphism. -
Stable keyset pagination depends on the order identifying a row. On a connection, graphitron appends the table’s primary key to a field-based or index-based order that does not already contain a key, in the direction of the last listed column; see Stable cursors and tie-breakers.
-
With no
@defaultOrderwritten, a list or connection falls back on the primary key of the table its target type is bound to. One with nothing to fall back on (a table with no primary key, or a function’s result whose type is bound to no table; a field that hops on to a table after the routine with@referencefalls back on that table’s key) needs@defaultOrder, or an@orderByargument that is non-null and single-valued; a list-valued one never stands in, since a client may send it empty. A nullable@orderByargument is no order stated, since a client may leave it out, so it needs@defaultOrderbeside it. One with neither fails the build (LIST_ORDER_UNDETERMINED) rather than return rows in whatever order the database hands them back, whether it is read inside its parent’s query, by a query of its own at the root or split from its parent, or over several types' tables; the next step offered is to add@defaultOrdernaming the result’s columns withfields:, or to make a single-valued@orderByargument non-null. A lookup answers in its keys' order and a@servicein the order it hands back, so neither is refused. The other refusals above areORDER_SURFACES,ORDER_FIELDS_EMPTY,ORDER_PRIMARY_KEY_MISSING,ELEMENT_UNRESOLVEDfor an index the table does not have,COLUMN_UNRESOLVEDfor a column it does not have, andORDER_FIELD_SHAPE, and an@orderByon a list read inside its parent’s query is deferred (ORDER_CHOICE_ON_INLINE_LIST); see the defects reference. -
A column that admits
NULLsorts itsNULLrows last whatever thedirection:, on lists and connections alike; see WhereNULLsorts.
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.