Skip to content

Commit 9589a48

Browse files
committed
fix(query-graphql): Improve handling of offset and limit in pager strategies
- Added logic to remove `offset` when it equals zero to prevent unnecessary query parameters. - Ensured consistent behavior for `limit` and `offset` when `enableFetchAllWithNegative` is true.
1 parent 5cc38f0 commit 9589a48

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,26 @@ export class LimitOffsetPagerStrategy<DTO> implements PagerStrategy<DTO> {
3030
if (includeExtraNode && (!this.enableFetchAllWithNegative || opts.limit !== -1)) {
3131
// Add 1 to the limit so we will fetch an additional node
3232
paging.limit += 1
33-
// if paging backwards remove one from the offset to check for a previous page.
33+
// if paging backwards, remove one from the offset to check for a previous page.
3434
if (isBackward) {
3535
paging.offset -= 1
3636
}
3737
if (paging.offset < 0) {
38-
// if the offset is < 0 it means we underflowed and that we cant have an extra page.
38+
// if the offset is < 0, it means we underflow and that we can't have an extra page.
3939
paging.offset = 0
4040
paging.limit = opts.limit
4141
}
4242
}
43-
if (this.enableFetchAllWithNegative && paging.limit === -1) delete paging.limit
43+
44+
if (this.enableFetchAllWithNegative && paging.limit === -1) {
45+
delete paging.limit
46+
47+
// Delete the offset if it is 0.
48+
if (paging.offset === 0) {
49+
delete paging.offset
50+
}
51+
}
52+
4453
return { ...query, paging }
4554
}
4655

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,16 @@ export class OffsetPager<DTO> implements Pager<DTO, OffsetPagerResult<DTO>> {
6969
private createQuery<Q extends OffsetQueryArgsType<DTO>>(query: Q, pagingMeta: OffsetPagingMeta<DTO>): Q {
7070
const { limit, offset } = pagingMeta.opts
7171
const paging = { limit: limit + 1, offset }
72-
if (this.enableFetchAllWithNegative && limit === -1) delete paging.limit
72+
73+
if (this.enableFetchAllWithNegative && limit === -1) {
74+
delete paging.limit
75+
76+
// Delete the offset if it is 0.
77+
if (offset === 0) {
78+
delete paging.offset
79+
}
80+
}
81+
7382
return { ...query, paging }
7483
}
7584

0 commit comments

Comments
 (0)