Skip to content

Commit

Permalink
feat(gatsby): Add details to pagination info (#13625)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanprobst authored and sidharthachatterjee committed Apr 25, 2019
1 parent fcb3068 commit f115994
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 5 deletions.
103 changes: 103 additions & 0 deletions packages/gatsby/src/schema/__tests__/pagination.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
const { paginate } = require(`../resolvers`)

describe(`Paginate query results`, () => {
const results = [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }]

it(`returns results`, async () => {
const args = { limit: 1 }
const nodes = paginate(results, args).edges.map(({ node }) => node)
expect(nodes).toEqual([results[0]])
})

it(`returns next and previos nodes`, async () => {
const args = { limit: 3 }
const next = paginate(results, args).edges.map(({ next }) => next)
const prev = paginate(results, args).edges.map(({ previous }) => previous)
expect(next).toEqual([results[1], results[2], undefined])
expect(prev).toEqual([undefined, results[0], results[1]])
})

it(`returns correct pagination info with limit only`, async () => {
const args = { limit: 2 }
const { pageInfo, totalCount } = paginate(results, args)
expect(totalCount).toBe(4)
expect(pageInfo).toEqual({
currentPage: 1,
hasNextPage: true,
hasPreviousPage: false,
itemCount: 2,
pageCount: 2,
perPage: 2,
})
})

it(`returns correct pagination info with skip and limit`, async () => {
const args = { skip: 1, limit: 2 }
const { pageInfo, totalCount } = paginate(results, args)
expect(totalCount).toBe(4)
expect(pageInfo).toEqual({
currentPage: 2,
hasNextPage: true,
hasPreviousPage: true,
itemCount: 2,
pageCount: 3,
perPage: 2,
})
})

it(`returns correct pagination info with skip and limit`, async () => {
const args = { skip: 2, limit: 2 }
const { pageInfo, totalCount } = paginate(results, args)
expect(totalCount).toBe(4)
expect(pageInfo).toEqual({
currentPage: 2,
hasNextPage: false,
hasPreviousPage: true,
itemCount: 2,
pageCount: 2,
perPage: 2,
})
})

it(`returns correct pagination info with skip only`, async () => {
const args = { skip: 1 }
const { pageInfo, totalCount } = paginate(results, args)
expect(totalCount).toBe(4)
expect(pageInfo).toEqual({
currentPage: 2,
hasNextPage: false,
hasPreviousPage: true,
itemCount: 3,
pageCount: 2,
perPage: undefined,
})
})

it(`returns correct pagination info with skip > totalCount`, async () => {
const args = { skip: 10 }
const { pageInfo, totalCount } = paginate(results, args)
expect(totalCount).toBe(4)
expect(pageInfo).toEqual({
currentPage: 2,
hasNextPage: false,
hasPreviousPage: true,
itemCount: 0,
pageCount: 2,
perPage: undefined,
})
})

it(`returns correct pagination info with limit > totalCount`, async () => {
const args = { limit: 10 }
const { pageInfo, totalCount } = paginate(results, args)
expect(totalCount).toBe(4)
expect(pageInfo).toEqual({
currentPage: 1,
hasNextPage: false,
hasPreviousPage: false,
itemCount: 4,
pageCount: 1,
perPage: 10,
})
})
})
13 changes: 13 additions & 0 deletions packages/gatsby/src/schema/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ const paginate = (results = [], { skip = 0, limit }) => {
const count = results.length
const items = results.slice(skip, limit && skip + limit)

const pageCount = limit
? Math.ceil(skip / limit) + Math.ceil((count - skip) / limit)
: skip
? 2
: 1
const currentPage = limit ? Math.ceil(skip / limit) + 1 : skip ? 2 : 1
const hasPreviousPage = currentPage > 1
const hasNextPage = skip + limit < count

return {
Expand All @@ -88,7 +95,12 @@ const paginate = (results = [], { skip = 0, limit }) => {
}),
nodes: items,
pageInfo: {
currentPage,
hasPreviousPage,
hasNextPage,
itemCount: items.length,
pageCount,
perPage: limit,
},
}
}
Expand Down Expand Up @@ -188,4 +200,5 @@ module.exports = {
link,
distinct,
group,
paginate,
}
10 changes: 5 additions & 5 deletions packages/gatsby/src/schema/types/pagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ const { distinct, group } = require(`../resolvers`)
const getPageInfo = ({ schemaComposer }) =>
schemaComposer.getOrCreateOTC(`PageInfo`, tc => {
tc.addFields({
currentPage: `Int!`,
hasPreviousPage: `Boolean!`,
hasNextPage: `Boolean!`,
// currentPage: `Int!`,
// hasPreviousPage: `Boolean!`,
// itemCount: `Int!`,
// pageCount: `Int!`,
// perPage: `Int`,
itemCount: `Int!`,
pageCount: `Int!`,
perPage: `Int`,
})
})

Expand Down

0 comments on commit f115994

Please sign in to comment.