Note: Be sure to install the HotChocolate.Types.SortingNuGet package.
Next we will add sorting to the users query, so we can sort on fields provided in the entity.
This is simply done by adding .UseSorting() on the field we want to sort, like seen below. You can chain sorting and filtering.
./GraphQL/QueryType.cs
using HotChocolate.Types;
namespace SlackClone.GraphQL
{
public class QueryType : ObjectType<Query>
{
protected override void Configure(IObjectTypeDescriptor<Query> descriptor)
{
descriptor.Field(t => t.GetUsers())
.UseFiltering()
.UseSorting();
descriptor.Field(t => t.GetUserStatuses());
}
}
}
Now we can run the server again and test what we have added. Reading the schema documentation you can see the sorting capabilities added to users query.
Lets test the sorting with the following query in the playground.