Skip to content

Commit

Permalink
fix(graphql): pagination offset should never be below zero
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiscoding-SE committed Oct 29, 2019
1 parent 66cb43b commit 41e321f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,28 @@ describe('GraphqlService', () => {
expect(removeSpaces(query)).toBe(removeSpaces(expectation));
});

it('should make sure the offset pagination is never below zero, even when new page is 0', () => {
const expectation = `query{ users(first:20, offset:0){ totalCount, nodes{ id, field1, field2 }}}`;
const columns = [{ id: 'field1', field: 'field1', width: 100 }, { id: 'field2', field: 'field2', width: 100 }];

service.init({ datasetName: 'users', columnDefinitions: columns }, paginationOptions, gridStub);
service.updatePagination(0, 20);
const query = service.buildQuery();

expect(removeSpaces(query)).toBe(removeSpaces(expectation));
});

it('should make sure the offset pagination is never below zero, even when new is 1 the offset should remain 0', () => {
const expectation = `query{ users(first:20, offset:0){ totalCount, nodes{ id, field1, field2 }}}`;
const columns = [{ id: 'field1', field: 'field1', width: 100 }, { id: 'field2', field: 'field2', width: 100 }];

service.init({ datasetName: 'users', columnDefinitions: columns }, paginationOptions, gridStub);
service.updatePagination(1, 20);
const query = service.buildQuery();

expect(removeSpaces(query)).toBe(removeSpaces(expectation));
});

it('should be able to provide "sortingOptions" and see the query string include the sorting', () => {
const expectation = `query{ users(first:20, offset:40,orderBy:[{field:field1, direction:DESC}]){ totalCount, nodes{ id, field1, field2 }}}`;
const columns = [{ id: 'field1', field: 'field1', width: 100 }, { id: 'field2', field: 'field2', width: 100 }];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ export class GraphqlService implements BackendService {
} else {
paginationOptions = {
first: pageSize,
offset: (newPage - 1) * pageSize
offset: (newPage > 1) ? ((newPage - 1) * pageSize) : 0 // recalculate offset but make sure the result is always over 0
};
}

Expand Down

0 comments on commit 41e321f

Please sign in to comment.