diff --git a/src/HotChocolate/Pagination/src/Pagination.EntityFramework/Extensions/PagingQueryableExtensions.cs b/src/HotChocolate/Pagination/src/Pagination.EntityFramework/Extensions/PagingQueryableExtensions.cs index 9c692438b9b..9fadcc6bfe6 100644 --- a/src/HotChocolate/Pagination/src/Pagination.EntityFramework/Extensions/PagingQueryableExtensions.cs +++ b/src/HotChocolate/Pagination/src/Pagination.EntityFramework/Extensions/PagingQueryableExtensions.cs @@ -99,13 +99,13 @@ public static async ValueTask> ToPageAsync( if (arguments.After is not null) { var cursor = CursorParser.Parse(arguments.After, keys); - source = source.Where(BuildWhereExpression(keys, cursor, forward)); + source = source.Where(BuildWhereExpression(keys, cursor, true)); } if (arguments.Before is not null) { var cursor = CursorParser.Parse(arguments.Before, keys); - source = source.Where(BuildWhereExpression(keys, cursor, forward)); + source = source.Where(BuildWhereExpression(keys, cursor, false)); } if (arguments.First is not null) diff --git a/src/HotChocolate/Pagination/test/Pagination.EntityFramework.Tests/PagingHelperTests.cs b/src/HotChocolate/Pagination/test/Pagination.EntityFramework.Tests/PagingHelperTests.cs index ac98c235439..9afbbcddf55 100644 --- a/src/HotChocolate/Pagination/test/Pagination.EntityFramework.Tests/PagingHelperTests.cs +++ b/src/HotChocolate/Pagination/test/Pagination.EntityFramework.Tests/PagingHelperTests.cs @@ -75,6 +75,27 @@ public async Task Fetch_First_2_Items_Third_Page() page.MatchMarkdownSnapshot(); } + [Fact] + public async Task Fetch_First_2_Items_Between() + { + // Arrange + var connectionString = CreateConnectionString(); + await SeedAsync(connectionString); + + // .. get first page + var arguments = new PagingArguments(4); + await using var context = new CatalogContext(connectionString); + var page = await context.Products.OrderBy(t => t.Name).ThenBy(t => t.Id) + .ToPageAsync(arguments); + + // Act + arguments = new PagingArguments(2, after: page.CreateCursor(page.First!), before: page.CreateCursor(page.Last!)); + page = await context.Products.OrderBy(t => t.Name).ThenBy(t => t.Id).ToPageAsync(arguments); + + // Assert + page.MatchMarkdownSnapshot(); + } + [Fact] public async Task Fetch_Last_2_Items() { @@ -117,6 +138,29 @@ public async Task Fetch_Last_2_Items_Before_Last_Page() page.MatchMarkdownSnapshot(); } + [Fact] + public async Task Fetch_Last_2_Items_Between() + { + // Arrange + var connectionString = CreateConnectionString(); + await SeedAsync(connectionString); + + // .. get last page + var arguments = new PagingArguments(last: 4); + await using var context = new CatalogContext(connectionString); + var page = await context.Products + .OrderBy(t => t.Name) + .ThenBy(t => t.Id) + .ToPageAsync(arguments); + + // Act + arguments = new PagingArguments(after: page.CreateCursor(page.First!), last: 2, before: page.CreateCursor(page.Last!)); + page = await context.Products.OrderBy(t => t.Name).ThenBy(t => t.Id).ToPageAsync(arguments); + + // Assert + page.MatchMarkdownSnapshot(); + } + [Fact] public async Task Batch_Fetch_First_2_Items() { diff --git a/src/HotChocolate/Pagination/test/Pagination.EntityFramework.Tests/__snapshots__/PagingHelperTests.Fetch_First_2_Items_Between.md b/src/HotChocolate/Pagination/test/Pagination.EntityFramework.Tests/__snapshots__/PagingHelperTests.Fetch_First_2_Items_Between.md new file mode 100644 index 00000000000..d2fa29aa0d6 --- /dev/null +++ b/src/HotChocolate/Pagination/test/Pagination.EntityFramework.Tests/__snapshots__/PagingHelperTests.Fetch_First_2_Items_Between.md @@ -0,0 +1,36 @@ +# Fetch_First_2_Items_Between + +```json +[ + { + "Id": 2, + "Name": "Product 0-1", + "Description": null, + "Price": 0.0, + "ImageFileName": null, + "TypeId": 1, + "Type": null, + "BrandId": 1, + "Brand": null, + "AvailableStock": 0, + "RestockThreshold": 0, + "MaxStockThreshold": 0, + "OnReorder": false + }, + { + "Id": 11, + "Name": "Product 0-10", + "Description": null, + "Price": 0.0, + "ImageFileName": null, + "TypeId": 1, + "Type": null, + "BrandId": 1, + "Brand": null, + "AvailableStock": 0, + "RestockThreshold": 0, + "MaxStockThreshold": 0, + "OnReorder": false + } +] +``` diff --git a/src/HotChocolate/Pagination/test/Pagination.EntityFramework.Tests/__snapshots__/PagingHelperTests.Fetch_Last_2_Items_Between.md b/src/HotChocolate/Pagination/test/Pagination.EntityFramework.Tests/__snapshots__/PagingHelperTests.Fetch_Last_2_Items_Between.md new file mode 100644 index 00000000000..5134dd84b75 --- /dev/null +++ b/src/HotChocolate/Pagination/test/Pagination.EntityFramework.Tests/__snapshots__/PagingHelperTests.Fetch_Last_2_Items_Between.md @@ -0,0 +1,36 @@ +# Fetch_Last_2_Items_Between + +```json +[ + { + "Id": 9998, + "Name": "Product 99-97", + "Description": null, + "Price": 0.0, + "ImageFileName": null, + "TypeId": 1, + "Type": null, + "BrandId": 100, + "Brand": null, + "AvailableStock": 0, + "RestockThreshold": 0, + "MaxStockThreshold": 0, + "OnReorder": false + }, + { + "Id": 9999, + "Name": "Product 99-98", + "Description": null, + "Price": 0.0, + "ImageFileName": null, + "TypeId": 1, + "Type": null, + "BrandId": 100, + "Brand": null, + "AvailableStock": 0, + "RestockThreshold": 0, + "MaxStockThreshold": 0, + "OnReorder": false + } +] +```