Sorters allow returning elements that are ordered based on specific criteria. This document describes how to add a sorter to the query resolver.
Add sorter to query resolver
Step 1: Create new sort field
To create a new sort field, add
sorters.py file in the target
Inside this directory, create a GraphQL enum with members representing the options that can be used
for sorting and a
description property explaining how to use the field.
Let's see an example:
GraphQL field name defines the schema field, which can be used to sort elements, and the value defines the list of model fields that will be used for sorting. The list can contain multiple elements, each consecutive field used when the values of the previous one are tied.
In the above example, if elements sorted by
NAME have the same value in the
they will be sorted by the value of the
Step 2: Create Sort Input
In the same file create a
SortInputObjectType. In its
Meta class, define
corresponds to just created sorted enum and
type_name which is used to create sorter description.
Step 3: Add sorter to query
sort_by field in your target query resolver in the
schema.py file. Make sure that resolver use
BaseDjangoConnectionField or another type which inherits from this. Here is an example:
Add custom sorter
When defining sorter apart from a model field you can define a custom sort field, and use it like other fields.
Step 1: Define a static method in your sorter field
The method name must follow the format
to the custom field you want to sort your queryset with. The method must take a
queryset as an argument and return a queryset with the annotated new field.
Please notice that the new field name from the method name must be the same as the annotated field.
Step 2: Use the field in a sort field definition
The next step is just using it in sort field definition as a normal model field. Notice that value in the list must be the same as annotated field name.