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;}Simple Sorting
Section intitulée « Simple Sorting »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" } } ] } }}Multiple Field Sorting
Section intitulée « Multiple Field Sorting »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.
Sort Directions
Section intitulée « Sort Directions »ASC- Ascending order (A-Z, 0-9, oldest to newest)DESC- Descending order (Z-A, 9-0, newest to oldest)
Default Sorting
Section intitulée « Default Sorting »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.
Combining with Filtering and Paging
Section intitulée « Combining with Filtering and Paging »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 } }}