Skip to content

Commit 9de6600

Browse files
committed
fix: hasNextPage value when first/last are both empty but exist more edges
1 parent e0c9c2b commit 9de6600

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/__tests__/connectionResolver-test.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,8 +369,16 @@ describe('connectionResolver', () => {
369369
});
370370

371371
describe('HasNextPage', () => {
372-
it('If first was not set, return false.', () => {
373-
expect(preparePageInfo(fiveEdges, {}, 4, 0).hasNextPage).toBe(false);
372+
it('If first was not set (and last is empty), return true.', () => {
373+
// By current Relay Cursor Connections Specification
374+
// if `first` and `last` are empty `hasNextPage` should be false.
375+
// This rule is deviation from specification for better dev experience:
376+
// when first and last args are empty
377+
// we should check if exist more edges and provide correct `hasNextPage` value.
378+
expect(preparePageInfo(fiveEdges, {}, 4, 0).hasNextPage).toBe(true);
379+
});
380+
it('If first was not set (but last is present), return false.', () => {
381+
expect(preparePageInfo(fiveEdges, { last: 200 }, 4, 0).hasNextPage).toBe(false);
374382
});
375383
it('If edges contains more than first elements, return true.', () => {
376384
expect(preparePageInfo(fiveEdges, { first: 4 }, 4, 0).hasNextPage).toBe(true);

src/connectionResolver.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ export function preparePageInfo(
303303
pageInfo.endCursor = edges[edges.length - 1].cursor;
304304
}
305305
pageInfo.hasPreviousPage = !!args.last && skip > 0;
306-
pageInfo.hasNextPage = !!args.first && hasExtraRecords;
306+
pageInfo.hasNextPage = (!!args.first || !args.last) && hasExtraRecords;
307307
}
308308

309309
return pageInfo;

0 commit comments

Comments
 (0)