Marks an argument whose value selects a sort order at runtime. The argument’s input type must contain exactly one SortDirection enum field and exactly one enum field whose values carry @order directives. The generator picks each up by GraphQL type, not by field name.
SDL signature
directive @orderBy on ARGUMENT_DEFINITION
@orderBy takes no parameters.
Canonical example
The example schema’s filmsOrderedConnection exposes a dynamic order argument:
type Query {
filmsOrderedConnection(
rating: MpaaRating @field(name: "RATING"),
order: [FilmOrderBy] @orderBy,
first: Int,
last: Int,
after: String,
before: String
): FilmsConnection! @defaultOrder(primaryKey: true)
}
input FilmOrderBy {
field: FilmSort!
direction: SortDirection
}
enum MpaaRating {
G
PG
PG_13
R
NC_17
}
enum FilmSort {
FILM_ID @order(primaryKey: true)
TITLE @order(fields: [{name : "title"}])
}
A client requesting order: [{ field: TITLE, direction: ASC }] gets the films sorted by film.title ascending. The [FilmOrderBy] list shape lets the client compose multi-key orderings. On a connection, graphitron appends the primary key after the composed sequence when it does not already contain a key, so [{ field: TITLE }] pages on title, film_id; sending FILM_ID after TITLE yourself gives the same order.
Constraints
-
Applies only to arguments. Putting
@orderByon a field, input field, or enum value is rejected. -
A list read inside its parent’s query, one with neither
@splitQuerynor a query of its own, does not apply a client’s choice yet, so@orderByon it fails the build (ORDER_CHOICE_ON_INLINE_LIST) rather than accept a choice and drop it. Read the list with@splitQuery, or order it with@defaultOrder. -
The argument’s input type must contain exactly one field typed as an enum whose values carry
@order, the sort enum, and exactly one other enum field, the direction, conventionally the project’sSortDirection. Field names are free; the generator tells the two apart by type. Any other shape fails the build (ORDER_INPUT_SHAPE). -
Every value of the bound enum must declare exactly one of
index,fields, orprimaryKeyvia@order. Missing or ambiguous specifications fail the build. -
@orderByand@defaultOrdercoexist on the same field. When the client supplies a value the dynamic order wins; when omitted,@defaultOrderapplies. -
Connection fields (
@asConnectionor hand-written*Connectiontypes) integrate@orderBywith keyset pagination automatically; the cursor encodes the active sort columns. -
On a root field returning a list or connection of a multi-table interface or union, each enum value’s columns resolve against every participant’s own table, under the same agreement rule as
@defaultOrder. A child field, or a single-valued root field, over such a type rejects the argument. See How-to: Sort across polymorphism.
See also
-
@orderspecifies how each enum value maps to database sort columns. -
@defaultOrdersupplies the fallback sort when the client omits the@orderByargument. -
How-to: Sort results covers multi-key orderings and keyset pagination interactions.