Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gatsby): correct hasNextPage pagination info when resultOffset is provided (#32319) #32386

Merged
merged 1 commit into from
Jul 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion packages/gatsby/src/schema/__tests__/pagination.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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