-
-
Notifications
You must be signed in to change notification settings - Fork 725
/
OffsetPagingHandler.cs
93 lines (83 loc) · 3.33 KB
/
OffsetPagingHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using System.Threading.Tasks;
using HotChocolate.Resolvers;
using HotChocolate.Utilities;
namespace HotChocolate.Types.Pagination
{
/// <summary>
/// Represents an offset paging handler, which can be implemented to
/// create optimized pagination for data sources.
///
/// The paging handler will be used by the paging middleware to slice the data.
/// </summary>
public abstract class OffsetPagingHandler : IPagingHandler
{
protected OffsetPagingHandler(PagingOptions options)
{
DefaultPageSize = options.DefaultPageSize ?? PagingDefaults.DefaultPageSize;
MaxPageSize = options.MaxPageSize ?? PagingDefaults.MaxPageSize;
IncludeTotalCount = options.IncludeTotalCount ?? PagingDefaults.IncludeTotalCount;
if (MaxPageSize < DefaultPageSize)
{
DefaultPageSize = MaxPageSize;
}
}
/// <summary>
/// The default page size configured for this handler.
/// </summary>
protected int DefaultPageSize { get; }
/// <summary>
/// The maximum allowed page size configured for this handler.
/// </summary>
/// <value></value>
protected int MaxPageSize { get; }
/// <summary>
/// Result should include total count.
/// </summary>
protected bool IncludeTotalCount { get; }
/// <summary>
/// Ensures that the arguments passed in by the user are valid and
/// do not try to consume more items per page as specified by
/// <see cref="MaxPageSize"/>.
/// </summary>
/// <param name="context">
/// The resolver context of the execution field.
/// </param>
public void ValidateContext(IResolverContext context)
{
int? take = context.ArgumentValue<int?>(OffsetPagingArgumentNames.Take);
if (take > MaxPageSize)
{
throw ThrowHelper.OffsetPagingHandler_MaxPageSize();
}
}
async ValueTask<IPage> IPagingHandler.SliceAsync(
IResolverContext context,
object source)
{
int? skip = context.ArgumentValue<int?>(OffsetPagingArgumentNames.Skip);
int? take = context.ArgumentValue<int?>(OffsetPagingArgumentNames.Take);
var arguments = new OffsetPagingArguments(skip, take ?? DefaultPageSize);
return await SliceAsync(context, source, arguments).ConfigureAwait(false);
}
/// <summary>
/// The algorithm defining how to slice data of the specified <paramref name="source"/>.
/// </summary>
/// <param name="context">
/// The resolver context of the execution field.
/// </param>
/// <param name="source">
/// The object representing the data source, collection, or query builder.
/// </param>
/// <param name="arguments">
/// The paging arguments provided by the user.
/// </param>
/// <returns>
/// The <see cref="CollectionSegment"/> representing
/// the slice of items belonging to the requested page.
/// </returns>
protected abstract ValueTask<CollectionSegment> SliceAsync(
IResolverContext context,
object source,
OffsetPagingArguments arguments);
}
}