Sorting
A sorting input type is generated for every object type defined in your type definitions. It allows for query results to be sorted by each individual field.
Using this example type definition:
type Movie {
title: String!
runtime: Int!
}
The following sorting input type and query are generated:
type Movie {
title: String!
runtime: Int!
}
enum SortDirection {
ASC
DESC
}
input MovieSort {
title: SortDirection
runtime: SortDirection
}
input MovieOptions {
"""Specify one or more MovieSort objects to sort Movies by. The sorts will be applied in the order in which they are arranged in the array."""
sort: [MovieSort!]
limit: Int
offset: Int
}
type Query {
movies(where: MovieWhere, options: MovieOptions): [Movie!]!
}
The following query fetches all movies sorted by runtime in ascending order:
query {
movies(options: {
sort: [
{
runtime: ASC
}
]
}) {
title
runtime
}
}
If there is a relationship between the Movie
and an Actor
type, you can also sort when fetching the actors
field:
query {
movies {
title
runtime
actors(options: {
sort: [
{
surname: ASC
}
]
}) {
surname
}
}
}
You cannot sort query results based on relationship properties. |