Skip to content

Commit

Permalink
Request totalCount through ToPageAsync if its requested. (#8011)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib authored Feb 12, 2025
1 parent 73be31d commit 13c074d
Show file tree
Hide file tree
Showing 13 changed files with 315 additions and 95 deletions.
170 changes: 85 additions & 85 deletions src/GreenDonut/GreenDonut.sln
Original file line number Diff line number Diff line change
@@ -1,85 +1,85 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{3697CA1D-4F24-491C-8C89-F17CBB63F8BC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GreenDonut", "src\GreenDonut\GreenDonut.csproj", "{D596ABFF-2B7E-42A1-ABF9-ECCE2C4E656D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GreenDonut.Abstractions", "src\GreenDonut.Abstractions\GreenDonut.Abstractions.csproj", "{7FD1006E-CB9A-4EC2-BCCC-7EF412B062EB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GreenDonut.Data", "src\GreenDonut.Data\GreenDonut.Data.csproj", "{2A37845A-EDC7-4A32-8103-2D86448F314D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GreenDonut.Data.Abstractions", "src\GreenDonut.Data.Abstractions\GreenDonut.Data.Abstractions.csproj", "{5F62F5E4-A450-4533-A0CF-64CE62F3DD06}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GreenDonut.Data.EntityFramework", "src\GreenDonut.Data.EntityFramework\GreenDonut.Data.EntityFramework.csproj", "{BA537BF3-3B52-42C9-9608-4EE7C1ACB0BA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GreenDonut.Data.Primitives", "src\GreenDonut.Data.Primitives\GreenDonut.Data.Primitives.csproj", "{211F2EF0-2E15-4C58-B195-3E7665558645}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{26D53CCB-1C96-4160-B9F6-F8A6A7207EDD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GreenDonut.Tests", "test\GreenDonut.Tests\GreenDonut.Tests.csproj", "{61879EC3-D0DC-4743-890A-6EE19D43E81B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GreenDonut.Data.EntityFramework.Tests", "test\GreenDonut.Data.EntityFramework.Tests\GreenDonut.Data.EntityFramework.Tests.csproj", "{72C43DC2-194C-4493-8673-938AF0BC3336}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GreenDonut.Data.Tests", "test\GreenDonut.Data.Tests\GreenDonut.Data.Tests.csproj", "{28AA2DCE-F97B-431F-90E7-B4D4F4636430}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{D596ABFF-2B7E-42A1-ABF9-ECCE2C4E656D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D596ABFF-2B7E-42A1-ABF9-ECCE2C4E656D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D596ABFF-2B7E-42A1-ABF9-ECCE2C4E656D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D596ABFF-2B7E-42A1-ABF9-ECCE2C4E656D}.Release|Any CPU.Build.0 = Release|Any CPU
{7FD1006E-CB9A-4EC2-BCCC-7EF412B062EB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7FD1006E-CB9A-4EC2-BCCC-7EF412B062EB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7FD1006E-CB9A-4EC2-BCCC-7EF412B062EB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7FD1006E-CB9A-4EC2-BCCC-7EF412B062EB}.Release|Any CPU.Build.0 = Release|Any CPU
{2A37845A-EDC7-4A32-8103-2D86448F314D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2A37845A-EDC7-4A32-8103-2D86448F314D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2A37845A-EDC7-4A32-8103-2D86448F314D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2A37845A-EDC7-4A32-8103-2D86448F314D}.Release|Any CPU.Build.0 = Release|Any CPU
{5F62F5E4-A450-4533-A0CF-64CE62F3DD06}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5F62F5E4-A450-4533-A0CF-64CE62F3DD06}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5F62F5E4-A450-4533-A0CF-64CE62F3DD06}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5F62F5E4-A450-4533-A0CF-64CE62F3DD06}.Release|Any CPU.Build.0 = Release|Any CPU
{BA537BF3-3B52-42C9-9608-4EE7C1ACB0BA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BA537BF3-3B52-42C9-9608-4EE7C1ACB0BA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BA537BF3-3B52-42C9-9608-4EE7C1ACB0BA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BA537BF3-3B52-42C9-9608-4EE7C1ACB0BA}.Release|Any CPU.Build.0 = Release|Any CPU
{211F2EF0-2E15-4C58-B195-3E7665558645}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{211F2EF0-2E15-4C58-B195-3E7665558645}.Debug|Any CPU.Build.0 = Debug|Any CPU
{211F2EF0-2E15-4C58-B195-3E7665558645}.Release|Any CPU.ActiveCfg = Release|Any CPU
{211F2EF0-2E15-4C58-B195-3E7665558645}.Release|Any CPU.Build.0 = Release|Any CPU
{61879EC3-D0DC-4743-890A-6EE19D43E81B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{61879EC3-D0DC-4743-890A-6EE19D43E81B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{61879EC3-D0DC-4743-890A-6EE19D43E81B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{61879EC3-D0DC-4743-890A-6EE19D43E81B}.Release|Any CPU.Build.0 = Release|Any CPU
{72C43DC2-194C-4493-8673-938AF0BC3336}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{72C43DC2-194C-4493-8673-938AF0BC3336}.Debug|Any CPU.Build.0 = Debug|Any CPU
{72C43DC2-194C-4493-8673-938AF0BC3336}.Release|Any CPU.ActiveCfg = Release|Any CPU
{72C43DC2-194C-4493-8673-938AF0BC3336}.Release|Any CPU.Build.0 = Release|Any CPU
{28AA2DCE-F97B-431F-90E7-B4D4F4636430}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{28AA2DCE-F97B-431F-90E7-B4D4F4636430}.Debug|Any CPU.Build.0 = Debug|Any CPU
{28AA2DCE-F97B-431F-90E7-B4D4F4636430}.Release|Any CPU.ActiveCfg = Release|Any CPU
{28AA2DCE-F97B-431F-90E7-B4D4F4636430}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{D596ABFF-2B7E-42A1-ABF9-ECCE2C4E656D} = {3697CA1D-4F24-491C-8C89-F17CBB63F8BC}
{7FD1006E-CB9A-4EC2-BCCC-7EF412B062EB} = {3697CA1D-4F24-491C-8C89-F17CBB63F8BC}
{2A37845A-EDC7-4A32-8103-2D86448F314D} = {3697CA1D-4F24-491C-8C89-F17CBB63F8BC}
{5F62F5E4-A450-4533-A0CF-64CE62F3DD06} = {3697CA1D-4F24-491C-8C89-F17CBB63F8BC}
{BA537BF3-3B52-42C9-9608-4EE7C1ACB0BA} = {3697CA1D-4F24-491C-8C89-F17CBB63F8BC}
{211F2EF0-2E15-4C58-B195-3E7665558645} = {3697CA1D-4F24-491C-8C89-F17CBB63F8BC}
{61879EC3-D0DC-4743-890A-6EE19D43E81B} = {26D53CCB-1C96-4160-B9F6-F8A6A7207EDD}
{72C43DC2-194C-4493-8673-938AF0BC3336} = {26D53CCB-1C96-4160-B9F6-F8A6A7207EDD}
{28AA2DCE-F97B-431F-90E7-B4D4F4636430} = {26D53CCB-1C96-4160-B9F6-F8A6A7207EDD}
EndGlobalSection
EndGlobal

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{3697CA1D-4F24-491C-8C89-F17CBB63F8BC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GreenDonut", "src\GreenDonut\GreenDonut.csproj", "{D596ABFF-2B7E-42A1-ABF9-ECCE2C4E656D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GreenDonut.Abstractions", "src\GreenDonut.Abstractions\GreenDonut.Abstractions.csproj", "{7FD1006E-CB9A-4EC2-BCCC-7EF412B062EB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GreenDonut.Data", "src\GreenDonut.Data\GreenDonut.Data.csproj", "{2A37845A-EDC7-4A32-8103-2D86448F314D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GreenDonut.Data.Abstractions", "src\GreenDonut.Data.Abstractions\GreenDonut.Data.Abstractions.csproj", "{5F62F5E4-A450-4533-A0CF-64CE62F3DD06}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GreenDonut.Data.EntityFramework", "src\GreenDonut.Data.EntityFramework\GreenDonut.Data.EntityFramework.csproj", "{BA537BF3-3B52-42C9-9608-4EE7C1ACB0BA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GreenDonut.Data.Primitives", "src\GreenDonut.Data.Primitives\GreenDonut.Data.Primitives.csproj", "{211F2EF0-2E15-4C58-B195-3E7665558645}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{26D53CCB-1C96-4160-B9F6-F8A6A7207EDD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GreenDonut.Tests", "test\GreenDonut.Tests\GreenDonut.Tests.csproj", "{61879EC3-D0DC-4743-890A-6EE19D43E81B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GreenDonut.Data.EntityFramework.Tests", "test\GreenDonut.Data.EntityFramework.Tests\GreenDonut.Data.EntityFramework.Tests.csproj", "{72C43DC2-194C-4493-8673-938AF0BC3336}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GreenDonut.Data.Tests", "test\GreenDonut.Data.Tests\GreenDonut.Data.Tests.csproj", "{28AA2DCE-F97B-431F-90E7-B4D4F4636430}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{D596ABFF-2B7E-42A1-ABF9-ECCE2C4E656D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D596ABFF-2B7E-42A1-ABF9-ECCE2C4E656D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D596ABFF-2B7E-42A1-ABF9-ECCE2C4E656D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D596ABFF-2B7E-42A1-ABF9-ECCE2C4E656D}.Release|Any CPU.Build.0 = Release|Any CPU
{7FD1006E-CB9A-4EC2-BCCC-7EF412B062EB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7FD1006E-CB9A-4EC2-BCCC-7EF412B062EB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7FD1006E-CB9A-4EC2-BCCC-7EF412B062EB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7FD1006E-CB9A-4EC2-BCCC-7EF412B062EB}.Release|Any CPU.Build.0 = Release|Any CPU
{2A37845A-EDC7-4A32-8103-2D86448F314D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2A37845A-EDC7-4A32-8103-2D86448F314D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2A37845A-EDC7-4A32-8103-2D86448F314D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2A37845A-EDC7-4A32-8103-2D86448F314D}.Release|Any CPU.Build.0 = Release|Any CPU
{5F62F5E4-A450-4533-A0CF-64CE62F3DD06}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5F62F5E4-A450-4533-A0CF-64CE62F3DD06}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5F62F5E4-A450-4533-A0CF-64CE62F3DD06}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5F62F5E4-A450-4533-A0CF-64CE62F3DD06}.Release|Any CPU.Build.0 = Release|Any CPU
{BA537BF3-3B52-42C9-9608-4EE7C1ACB0BA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BA537BF3-3B52-42C9-9608-4EE7C1ACB0BA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BA537BF3-3B52-42C9-9608-4EE7C1ACB0BA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BA537BF3-3B52-42C9-9608-4EE7C1ACB0BA}.Release|Any CPU.Build.0 = Release|Any CPU
{211F2EF0-2E15-4C58-B195-3E7665558645}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{211F2EF0-2E15-4C58-B195-3E7665558645}.Debug|Any CPU.Build.0 = Debug|Any CPU
{211F2EF0-2E15-4C58-B195-3E7665558645}.Release|Any CPU.ActiveCfg = Release|Any CPU
{211F2EF0-2E15-4C58-B195-3E7665558645}.Release|Any CPU.Build.0 = Release|Any CPU
{61879EC3-D0DC-4743-890A-6EE19D43E81B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{61879EC3-D0DC-4743-890A-6EE19D43E81B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{61879EC3-D0DC-4743-890A-6EE19D43E81B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{61879EC3-D0DC-4743-890A-6EE19D43E81B}.Release|Any CPU.Build.0 = Release|Any CPU
{72C43DC2-194C-4493-8673-938AF0BC3336}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{72C43DC2-194C-4493-8673-938AF0BC3336}.Debug|Any CPU.Build.0 = Debug|Any CPU
{72C43DC2-194C-4493-8673-938AF0BC3336}.Release|Any CPU.ActiveCfg = Release|Any CPU
{72C43DC2-194C-4493-8673-938AF0BC3336}.Release|Any CPU.Build.0 = Release|Any CPU
{28AA2DCE-F97B-431F-90E7-B4D4F4636430}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{28AA2DCE-F97B-431F-90E7-B4D4F4636430}.Debug|Any CPU.Build.0 = Debug|Any CPU
{28AA2DCE-F97B-431F-90E7-B4D4F4636430}.Release|Any CPU.ActiveCfg = Release|Any CPU
{28AA2DCE-F97B-431F-90E7-B4D4F4636430}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{D596ABFF-2B7E-42A1-ABF9-ECCE2C4E656D} = {3697CA1D-4F24-491C-8C89-F17CBB63F8BC}
{7FD1006E-CB9A-4EC2-BCCC-7EF412B062EB} = {3697CA1D-4F24-491C-8C89-F17CBB63F8BC}
{2A37845A-EDC7-4A32-8103-2D86448F314D} = {3697CA1D-4F24-491C-8C89-F17CBB63F8BC}
{5F62F5E4-A450-4533-A0CF-64CE62F3DD06} = {3697CA1D-4F24-491C-8C89-F17CBB63F8BC}
{BA537BF3-3B52-42C9-9608-4EE7C1ACB0BA} = {3697CA1D-4F24-491C-8C89-F17CBB63F8BC}
{211F2EF0-2E15-4C58-B195-3E7665558645} = {3697CA1D-4F24-491C-8C89-F17CBB63F8BC}
{61879EC3-D0DC-4743-890A-6EE19D43E81B} = {26D53CCB-1C96-4160-B9F6-F8A6A7207EDD}
{72C43DC2-194C-4493-8673-938AF0BC3336} = {26D53CCB-1C96-4160-B9F6-F8A6A7207EDD}
{28AA2DCE-F97B-431F-90E7-B4D4F4636430} = {26D53CCB-1C96-4160-B9F6-F8A6A7207EDD}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static async ValueTask<Page<T>> ToPageAsync<T>(
this IQueryable<T> source,
PagingArguments arguments,
CancellationToken cancellationToken = default)
=> await source.ToPageAsync(arguments, includeTotalCount: false, cancellationToken);
=> await source.ToPageAsync(arguments, includeTotalCount: arguments.IncludeTotalCount, cancellationToken);

/// <summary>
/// Executes a query with paging and returns the selected page.
Expand Down Expand Up @@ -212,7 +212,12 @@ public static ValueTask<Dictionary<TKey, Page<TValue>>> ToBatchPageAsync<TKey, T
PagingArguments arguments,
CancellationToken cancellationToken = default)
where TKey : notnull
=> ToBatchPageAsync<TKey, TValue, TValue>(source, keySelector, t => t, arguments, cancellationToken);
=> ToBatchPageAsync<TKey, TValue, TValue>(
source,
keySelector,
t => t,
arguments,
cancellationToken);

/// <summary>
/// Executes a batch query with paging and returns the selected pages for each parent.
Expand Down Expand Up @@ -295,7 +300,13 @@ public static ValueTask<Dictionary<TKey, Page<TValue>>> ToBatchPageAsync<TKey, T
PagingArguments arguments,
CancellationToken cancellationToken = default)
where TKey : notnull
=> ToBatchPageAsync(source, keySelector, valueSelector, arguments, includeTotalCount: false, cancellationToken);
=> ToBatchPageAsync(
source,
keySelector,
valueSelector,
arguments,
includeTotalCount: arguments.IncludeTotalCount,
cancellationToken);

/// <summary>
/// Executes a batch query with paging and returns the selected pages for each parent.
Expand Down
19 changes: 17 additions & 2 deletions src/GreenDonut/src/GreenDonut.Data.Primitives/PagingArguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,21 @@ public readonly record struct PagingArguments
/// <param name="before">
/// The cursor before which entities shall be taken.
/// </param>
/// <param name="includeTotalCount">
/// Defines if the total count of items in the dataset shall be included in the result.
/// </param>
public PagingArguments(
int? first = null,
string? after = null,
int? last = null,
string? before = null)
string? before = null,
bool includeTotalCount = false)
{
First = first;
After = after;
Last = last;
Before = before;
IncludeTotalCount = includeTotalCount;
}

/// <summary>
Expand All @@ -52,6 +57,11 @@ public PagingArguments(
/// </summary>
public string? Before { get; init; }

/// <summary>
/// Defines if the total count of items in the dataset shall be included in the result.
/// </summary>
public bool IncludeTotalCount { get; init; }

/// <summary>
/// Deconstructs the paging arguments into its components.
/// </summary>
Expand All @@ -67,15 +77,20 @@ public PagingArguments(
/// <param name="before">
/// The cursor before which entities shall be taken.
/// </param>
/// <param name="includeTotalCount">
/// Defines if the total count of items in the dataset shall be included in the result.
/// </param>
public void Deconstruct(
out int? first,
out string? after,
out int? last,
out string? before)
out string? before,
out bool includeTotalCount)
{
first = First;
after = After;
last = Last;
before = Before;
includeTotalCount = IncludeTotalCount;
}
}
1 change: 0 additions & 1 deletion src/GreenDonut/src/GreenDonut/GreenDonut.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Data.Primitives\GreenDonut.Abstractions.csproj" />
<ProjectReference Include="..\GreenDonut.Abstractions\GreenDonut.Abstractions.csproj" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ private static void ApplyConfiguration(

var index = definition.MiddlewareDefinitions.IndexOf(placeholder);
definition.MiddlewareDefinitions[index] = new(middleware, key: Paging);
definition.ContextData[WellKnownContextData.PagingOptions] = options;
}

private static IExtendedType GetSourceType(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using GreenDonut.Data;
using HotChocolate.Execution.Processing;
using HotChocolate.Internal;
using HotChocolate.Resolvers;
using HotChocolate.Types.Pagination;
Expand All @@ -21,8 +22,22 @@ public T Execute<T>(IResolverContext context)
=> (T)(object)MapArguments(context);

private static PagingArguments MapArguments(IResolverContext context)
=> MapArguments(context.GetLocalState<CursorPagingArguments>(WellKnownContextData.PagingArguments));
{
var pagingArguments = context.GetLocalState<CursorPagingArguments>(WellKnownContextData.PagingArguments);
var includeTotalCount = IncludeTotalCount(context.Selection);

private static PagingArguments MapArguments(CursorPagingArguments arguments)
=> new(arguments.First, arguments.After, arguments.Last, arguments.Before);
if (includeTotalCount)
{
includeTotalCount = context.IsSelected("totalCount");
}

return MapArguments(pagingArguments, includeTotalCount);
}

private static PagingArguments MapArguments(CursorPagingArguments arguments, bool includeTotalCount)
=> new(arguments.First, arguments.After, arguments.Last, arguments.Before, includeTotalCount);

private static bool IncludeTotalCount(ISelection selection)
=> selection.Field.ContextData.TryGetValue(WellKnownContextData.PagingOptions, out var options)
&& options is PagingOptions { IncludeTotalCount: true };
}
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,54 @@ public async Task Query_Products_First_2_And_Brand()
MatchSnapshot(result, interceptor);
}

[Fact]
public async Task Query_Products_Include_TotalCount()
{
// arrange
using var interceptor = new TestQueryInterceptor();

// act
var result = await ExecuteAsync(
"""
{
products(first: 2) {
nodes {
name
}
totalCount
}
}
""",
interceptor);

// assert
MatchSnapshot(result, interceptor);
}

[Fact]
public async Task Query_Products_Exclude_TotalCount()
{
// arrange
using var interceptor = new TestQueryInterceptor();

// act
var result = await ExecuteAsync(
"""
{
products(first: 2) {
nodes {
name
}
}
}
""",
interceptor);

// assert
MatchSnapshot(result, interceptor);
}

private static ServiceProvider CreateServer(string connectionString)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace HotChocolate.Data.Types.Products;
[QueryType]
public static partial class ProductQueries
{
[UsePaging]
[UsePaging(IncludeTotalCount = true)]
[UseFiltering]
[UseSorting]
public static async Task<Connection<Product>> GetProductsAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ type ProductsConnection {
edges: [ProductsEdge!]
"A flattened list of the nodes."
nodes: [Product!]
"Identifies the total count of items in the connection."
totalCount: Int! @cost(weight: "10")
}

"An edge in a connection."
Expand Down
Loading

0 comments on commit 13c074d

Please sign in to comment.