简体   繁体   English

HotChocolate - 启用分页并在同一查询类型中获取所有结果

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

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.为了找到一个好方法,我们需要删除所有属性以启用 HotChocolate 以返回数据库上的所有用户,并为分页提供另一个查询。

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谢谢

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.在“OffsetPaging”功能的早期版本中,它很容易实现:如果您没有在查询中设置页面大小和偏移量,那么实际上会返回整个列表。 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.据我了解,没有直接的方法可以在“OffsetPaging”打开的情况下使返回列表不受限制。 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.这允许我们在请求中省略分页参数,同时确保将返回整个列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM