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 FilmSort {
FILM_ID @order(primaryKey: true)
TITLE @order(fields: [{name: "title"}])
}
enum SortDirection { ASC, DESC }
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; FILM_ID after TITLE ties the keyset cursor onto the primary key for stable pagination.
Constraints
-
Applies only to arguments. Putting
@orderByon a field, input field, or enum value is rejected. -
The argument’s input type must contain exactly one field whose type is the project’s
SortDirectionenum (declared by name, looked up by type) and exactly one field whose type is an enum carrying@orderon every value. Field names are free; the generator binds by type. -
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.
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.