Sorting
A sorting input type is generated for every object type defined in your type definitions, allowing for Query results to be sorted by each individual field.
Using the following example type definition:
type Movie {
title: String!
runtime: Int!
}
The following sorting input type and query will be 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 would then allow you to fetch all movies sorted by runtime in ascending order:
query {
movies(options: {
sort: [
{
runtime: ASC
}
]
}) {
title
runtime
}
}
Additionally, say there was a relationship between the Movie
and an Actor
type, sorting can also be applied when fetching the actors
field:
query {
movies {
title
runtime
actors(options: {
sort: [
{
surname: ASC
}
]
}) {
surname
}
}
}
Was this page helpful?