Aller au contenu

Sorting

Sorting allows you to order query results by one or more fields.

The following examples are based on the following ProductDTO:

import { EnhancedField, IDField } from '@lib/type-graphql-query';
import { ObjectType, ID, GraphQLISODateTime, Field } from '@lib/type-graphql';
@ObjectType('Product')
export class ProductDTO {
@IDField(() => ID)
id!: string;
@EnhancedField()
name!: string;
@EnhancedField()
price!: number;
@Field(() => GraphQLISODateTime)
created!: Date;
@Field(() => GraphQLISODateTime)
updated!: Date;
}

To sort results by a specific field, use the sorting parameter:

GraphQL:

{
products(sorting: [{ field: "name", direction: DESC }]) {
edges {
node {
id
name
price
created
updated
}
}
}
}

Response:

{
"data": {
"products": {
"edges": [
{
"node": {
"id": "3",
"name": "Free Range Eggs",
"price": 20.99,
"created": "2021-02-14T12:56:01.106Z",
"updated": "2021-02-14T12:56:01.106Z"
}
},
{
"node": {
"id": "2",
"name": "Organic Eggs",
"price": 18.99,
"created": "2021-02-14T12:56:01.106Z",
"updated": "2021-02-14T12:56:01.106Z"
}
},
{
"node": {
"id": "1",
"name": "Premium Eggs",
"price": 15.99,
"created": "2021-02-14T12:56:01.106Z",
"updated": "2021-02-14T12:56:01.106Z"
}
}
]
}
}
}

You can also sort by multiple fields by specifying multiple objects in the sorting array. The order matters - the first field takes precedence:

GraphQL:

{
products(sorting: [{ field: "price", direction: ASC }, { field: "name", direction: DESC }]) {
edges {
node {
id
name
price
created
updated
}
}
}
}

Response:

{
"data": {
"products": {
"edges": [
{
"node": {
"id": "1",
"name": "Premium Eggs",
"price": 15.99,
"created": "2021-02-14T12:56:01.106Z",
"updated": "2021-02-14T12:56:01.106Z"
}
},
{
"node": {
"id": "2",
"name": "Organic Eggs",
"price": 18.99,
"created": "2021-02-14T12:56:01.106Z",
"updated": "2021-02-14T12:56:01.106Z"
}
},
{
"node": {
"id": "3",
"name": "Free Range Eggs",
"price": 20.99,
"created": "2021-02-14T12:56:01.106Z",
"updated": "2021-02-14T12:56:01.106Z"
}
}
]
}
}
}

In this example, products are first sorted by price in ascending order, and then by name in descending order for products with the same price.

  • ASC - Ascending order (A-Z, 0-9, oldest to newest)
  • DESC - Descending order (Z-A, 9-0, newest to oldest)

You can specify a default sort order using the @QueryOptions decorator on your DTO:

import { EnhancedField, IDField, QueryOptions } from '@lib/type-graphql-query';
import { ObjectType, ID, GraphQLISODateTime, Field } from '@lib/type-graphql';
import { SortDirection } from '@lib/type-graphql-query';
@ObjectType('Product')
@QueryOptions({
defaultSort: [{ field: 'name', direction: SortDirection.ASC }],
})
export class ProductDTO {
@IDField(() => ID)
id!: string;
@EnhancedField()
name!: string;
// ... other fields
}

When a default sort is specified, it will be used if no sorting is provided in the query.

Sorting works seamlessly with filtering and paging:

GraphQL:

{
products(
filter: { price: { gt: 10 } }
sorting: [{ field: "price", direction: ASC }]
paging: { first: 10 }
) {
edges {
node {
id
name
price
}
}
pageInfo {
hasNextPage
hasPreviousPage
}
}
}