Skip to content

Commit

Permalink
fix(graphql,paging): Fix for #281 paging backwards windowing
Browse files Browse the repository at this point in the history
  • Loading branch information
doug-martin committed Jun 12, 2020
1 parent d2ad54d commit c319344
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ describe('ConnectionType', (): void => {
paging: createPage({ last: 2, before: 'YXJyYXljb25uZWN0aW9uOjM=' }),
});
expect(queryMany).toHaveBeenCalledTimes(1);
expect(queryMany).toHaveBeenCalledWith({ paging: { limit: 2, offset: 0 } });
expect(queryMany).toHaveBeenCalledWith({ paging: { limit: 3, offset: 0 } });
expect(response).toEqual({
edges: [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,15 @@ export class CursorPager<DTO> {
}

async runQuery(queryMany: QueryMany<DTO>, query: Query<DTO>, pagingMeta: PagingMeta): Promise<QueryResults<DTO>> {
// if paging forward add 1 to limit for check for an additional page.
const limit = pagingMeta.isForward ? pagingMeta.limit + 1 : pagingMeta.limit;
// Add 1 to the limit so we will fetch an additional node
let limit = pagingMeta.limit + 1;
// if paging backwards remove one from the offset to check for a previous page.
const offset = Math.max(0, pagingMeta.isBackward ? pagingMeta.offset - 1 : pagingMeta.offset);
let offset = pagingMeta.isBackward ? pagingMeta.offset - 1 : pagingMeta.offset;
if (offset < 0) {
// if the offset is < 0 it means we underflowed and that we cant have an extra page.
offset = 0;
limit = pagingMeta.limit;
}
const nodes = await queryMany({ ...query, paging: { limit, offset } });
// check if we have an additional node
// if paging forward that indicates we have a next page
Expand Down

0 comments on commit c319344

Please sign in to comment.