Skip to content

Commit c319344

Browse files
committed
fix(graphql,paging): Fix for #281 paging backwards windowing
1 parent d2ad54d commit c319344

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

packages/query-graphql/__tests__/types/connection/connection.type.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ describe('ConnectionType', (): void => {
169169
paging: createPage({ last: 2, before: 'YXJyYXljb25uZWN0aW9uOjM=' }),
170170
});
171171
expect(queryMany).toHaveBeenCalledTimes(1);
172-
expect(queryMany).toHaveBeenCalledWith({ paging: { limit: 2, offset: 0 } });
172+
expect(queryMany).toHaveBeenCalledWith({ paging: { limit: 3, offset: 0 } });
173173
expect(response).toEqual({
174174
edges: [
175175
{

packages/query-graphql/src/types/connection/pager/limit-offset.pager.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,15 @@ export class CursorPager<DTO> {
3535
}
3636

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

0 commit comments

Comments
 (0)