diff --git a/packages/query-graphql/__tests__/types/connection/connection.type.spec.ts b/packages/query-graphql/__tests__/types/connection/connection.type.spec.ts index 79803c1b5..dd111d8b0 100644 --- a/packages/query-graphql/__tests__/types/connection/connection.type.spec.ts +++ b/packages/query-graphql/__tests__/types/connection/connection.type.spec.ts @@ -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: [ { diff --git a/packages/query-graphql/src/types/connection/pager/limit-offset.pager.ts b/packages/query-graphql/src/types/connection/pager/limit-offset.pager.ts index 23eaf4116..96e94f2d9 100644 --- a/packages/query-graphql/src/types/connection/pager/limit-offset.pager.ts +++ b/packages/query-graphql/src/types/connection/pager/limit-offset.pager.ts @@ -35,10 +35,15 @@ export class CursorPager { } async runQuery(queryMany: QueryMany, query: Query, pagingMeta: PagingMeta): Promise> { - // 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