Skip to content

Commit

Permalink
fix(gatsby): correct hasNextPage pagination info when resultOffset is…
Browse files Browse the repository at this point in the history
… provided (#32319) (#32386)

(cherry picked from commit 9f8a580)

Co-authored-by: Angelo Ashmore <angeloashmore@users.noreply.github.com>
  • Loading branch information
GatsbyJS Bot and angeloashmore committed Jul 16, 2021
1 parent 4f9a724 commit ba0050a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
21 changes: 20 additions & 1 deletion packages/gatsby/src/schema/__tests__/pagination.js
Expand Up @@ -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)
Expand All @@ -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)
})
Expand Down
19 changes: 18 additions & 1 deletion packages/gatsby/src/schema/resolvers.ts
Expand Up @@ -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,
Expand Down

0 comments on commit ba0050a

Please sign in to comment.