HotChocolate - Enable Pagination mixed with Get All Results at the same Query Type

Viewed 120

We have a very simple scenario where I need to return data from the database.

Some actions require pagination on the data, but others do not. For this second case, I just need to return all the data (with no pagination).

Does anyone know a way for creating a single query type to solve both situations instead of two query types?

Trying to find a good way, we needed to remove all the attributes to enable HotChocolate for returning all Users on the database, and another query for the pagination.

First Query Type

[UseDbContext(typeof(MyContext)), UseOffsetPaging, UseFiltering, UseSorting]
public IQueryable<User> Users(ClaimsPrincipal claims, [Service] MyContext context)
{
    return context.Users;
}

Second Query Type (that we want to remove, and mix with a single query, mixed with the pagination features)

[UseDbContext(typeof(MyContext))]
public IQueryable<User> GetAllUsers(ClaimsPrincipal claims, [Service] MyContext context)
{
    return context.Users;
}

Is there a way to expose a single query type for both scenarios? Pagination and Get All?

Thank you!

1 Answers

In the early versions of the "OffsetPaging" functionality it was easily achievable: if you don't set up page size and offset in your query then literally the whole list is returned. In the newer versions, it is also possible to omit setting up the paging parameters, but some default parameters will be used anyway. As far as I understand, there's no direct way to make the return list unlimited having "OffsetPaging" turned on. As for me, I find that change strange and not always convenient, but now it is the way it is.

How do we solve that issue on our project? We just set up the default paging options to very high values:

      .AddQueryType<QueryType>()
            .SetPagingOptions(new PagingOptions { MaxPageSize = int.MaxValue - 1, DefaultPageSize = int.MaxValue - 1, IncludeTotalCount = true })

That allows us to omit the paging parameters in requests and, at the same time, be sure that the whole list will be returned.

Related