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

index

String

none

Name of a database index. The generator emits the index’s column list as the sort specification. Requires <includeIndexes>true</includeIndexes> in the jOOQ generator.

fields

[FieldSort!]

none

Explicit sort list. Each FieldSort sets exactly one of name (a column on the field’s return-type table) and method (a static method returning the sort key), with an optional collate (database-specific collation) and direction. Beside name, an optional path resolves the column on a to-one related table instead; see @order: Sorting by a related value.

primaryKey

Boolean

false

When true, sorts by the parent table’s primary key columns in declaration order. Common choice for stable keyset pagination.

direction

SortDirection

ASC

Sort direction applied to every column in the spec. ASC or DESC.

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, so primaryKey: true fails the build with ORDER_PRIMARY_KEY_MISSING and index: with ELEMENT_UNRESOLVED.

  • A name that is not a column of the routine’s result fails the build with COLUMN_UNRESOLVED, naming it.

  • A fields element with a method: or a path: names no column of the routine’s result and fails the build with ROUTINE_WRITE_PLURAL_ORDER_NOT_A_COLUMN.

  • A field of this shape with no @defaultOrder fails the build with ROUTINE_WRITE_PLURAL_UNORDERED, naming the list argument.

  • An @orderBy argument is not accepted on a @routine mutation field (READ_SURFACE_ON_WRITE): a client-chosen order would break the order the schema states.

Constraints

  • Exactly one of index, fields, or primaryKey must be set. Multiple or none fails the build.

  • fields names 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 @order value written the same way.

  • primaryKey: true needs 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 the fields: spelling is one edit away. A function’s result is the standing case: its type is bound to no table unless its own @table binds 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 / Edge types.

  • When combined with @orderBy on the same field, the client’s argument value (when present) takes precedence; @defaultOrder is the fallback.

  • direction is global across the spec. Per-column direction requires @orderBy (the enum input carries its own SortDirection).

  • index requires jOOQ index generation. fields[].name resolves against the field’s return-type table, not the parent, or against the table its path lands on.

  • A path follows to-one hops only and carries no condition:, each hop’s key joining the table the path stands on (SORT_KEY_PATH_TO_MANY, SORT_KEY_PATH_CONDITION, SORT_KEY_PATH_UNRESOLVED); a method takes the field’s table and returns a Field<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 @defaultOrder written, 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 @reference falls back on that table’s key) needs @defaultOrder, or an @orderBy argument that is non-null and single-valued; a list-valued one never stands in, since a client may send it empty. A nullable @orderBy argument is no order stated, since a client may leave it out, so it needs @defaultOrder beside 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 @defaultOrder naming the result’s columns with fields:, or to make a single-valued @orderBy argument non-null. A lookup answers in its keys' order and a @service in the order it hands back, so neither is refused. The other refusals above are ORDER_SURFACES, ORDER_FIELDS_EMPTY, ORDER_PRIMARY_KEY_MISSING, ELEMENT_UNRESOLVED for an index the table does not have, COLUMN_UNRESOLVED for a column it does not have, and ORDER_FIELD_SHAPE, and an @orderBy on a list read inside its parent’s query is deferred (ORDER_CHOICE_ON_INLINE_LIST); see the defects reference.

  • A column that admits NULL sorts its NULL rows last whatever the direction:, on lists and connections alike; see Where NULL sorts.

See also

  • @orderBy supplies the dynamic counterpart.

  • @order reuses the index / fields / primaryKey shape on enum values.

  • @asConnection for connection synthesis; @defaultOrder is required for stable pagination on synthesised connections.

  • How-to: Sort results covers tie-breakers, collation pitfalls, and pagination semantics.