From ba0050a637824477f7666ebf0564dafd94976589 Mon Sep 17 00:00:00 2001 From: GatsbyJS Bot Date: Fri, 16 Jul 2021 04:06:56 -0400 Subject: [PATCH] fix(gatsby): correct hasNextPage pagination info when resultOffset is provided (#32319) (#32386) (cherry picked from commit 9f8a580287ec3a4bf9696237e7e8ac49e2fc001c) Co-authored-by: Angelo Ashmore --- .../gatsby/src/schema/__tests__/pagination.js | 21 ++++++++++++++++++- packages/gatsby/src/schema/resolvers.ts | 19 ++++++++++++++++- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/packages/gatsby/src/schema/__tests__/pagination.js b/packages/gatsby/src/schema/__tests__/pagination.js index 8db457266f9ba..4518c0b520457 100644 --- a/packages/gatsby/src/schema/__tests__/pagination.js +++ b/packages/gatsby/src/schema/__tests__/pagination.js @@ -154,7 +154,7 @@ describe(`Paginate query results`, () => { }) it(`returns correct pagination info with skip, limit and resultOffset`, async () => { - const args = { skip: 2, limit: 2, resultOffset: 1 } + const args = { skip: 1, limit: 2, resultOffset: 1 } const { pageInfo, totalCount } = paginate(results, args) expect(typeof totalCount).toBe(`function`) expect(await totalCount()).toBe(4) @@ -168,6 +168,25 @@ describe(`Paginate query results`, () => { perPage: 2, totalCount: expect.toBeFunction(), }) + expect(await pageInfo.pageCount()).toEqual(3) + expect(await pageInfo.totalCount()).toEqual(4) + }) + + it(`returns correct pagination info with skip, limit and resultOffset on the last page`, async () => { + const args = { skip: 2, limit: 2, resultOffset: 1 } + const { pageInfo, totalCount } = paginate(results, args) + expect(typeof totalCount).toBe(`function`) + expect(await totalCount()).toBe(4) + + expect(pageInfo).toEqual({ + currentPage: 2, + hasNextPage: false, + hasPreviousPage: true, + itemCount: 2, + pageCount: expect.toBeFunction(), + perPage: 2, + totalCount: expect.toBeFunction(), + }) expect(await pageInfo.pageCount()).toEqual(2) expect(await pageInfo.totalCount()).toEqual(4) }) diff --git a/packages/gatsby/src/schema/resolvers.ts b/packages/gatsby/src/schema/resolvers.ts index bd452c35ab894..b89e47d06f531 100644 --- a/packages/gatsby/src/schema/resolvers.ts +++ b/packages/gatsby/src/schema/resolvers.ts @@ -288,7 +288,24 @@ export function paginate( } const currentPage = limit ? Math.ceil(skip / limit) + 1 : skip ? 2 : 1 const hasPreviousPage = currentPage > 1 - const hasNextPage = limit ? allItems.length - start > limit : false + + let hasNextPage = false + // If limit is not defined, there will never be a next page. + if (limit) { + if (resultOffset > 0) { + // If resultOffset is greater than 0, we need to test if `allItems` contains + // items that should be skipped. + // + // This is represented if the `start` index offset is 0 or less. A start + // greater than 0 means `allItems` contains extra items that would come + // before the skipped items. + hasNextPage = start < 1 + } else { + // If the resultOffset is 0, we can test if `allItems` contains more items + // than the limit after removing the skipped items. + hasNextPage = allItems.length - start > limit + } + } return { totalCount,