Skip to content
This repository was archived by the owner on Jun 1, 2025. It is now read-only.

Commit cf7c334

Browse files
authored
Merge pull request #329 from ghiscoding/bugfix/graphql-negative-pagination
fix(graphql): pagination offset should never be below zero
2 parents 66cb43b + 3ecd9b9 commit cf7c334

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

src/app/modules/angular-slickgrid/components/slick-pagination.component.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { AfterViewInit, Component, EventEmitter, Injectable, Input, OnDestroy, Optional, Output } from '@angular/core';
1+
import { AfterViewInit, Component, EventEmitter, Input, OnDestroy, Optional, Output } from '@angular/core';
22
import { TranslateService } from '@ngx-translate/core';
33
import { Subscription } from 'rxjs';
44

@@ -47,6 +47,7 @@ export class SlickPaginationComponent implements AfterViewInit, OnDestroy {
4747
/** Constructor */
4848
constructor(private paginationService: PaginationService, @Optional() private translate: TranslateService) {
4949
// translate all the text using ngx-translate or custom locales
50+
this.translateAllUiTexts(this.locales);
5051
if (translate && translate.onLangChange) {
5152
this.subscriptions.push(this.translate.onLangChange.subscribe(() => this.translateAllUiTexts(this.locales)));
5253
}

src/app/modules/angular-slickgrid/services/__tests__/graphql.service.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,28 @@ describe('GraphqlService', () => {
221221
expect(removeSpaces(query)).toBe(removeSpaces(expectation));
222222
});
223223

224+
it('should make sure the offset pagination is never below zero, even when new page is 0', () => {
225+
const expectation = `query{ users(first:20, offset:0){ totalCount, nodes{ id, field1, field2 }}}`;
226+
const columns = [{ id: 'field1', field: 'field1', width: 100 }, { id: 'field2', field: 'field2', width: 100 }];
227+
228+
service.init({ datasetName: 'users', columnDefinitions: columns }, paginationOptions, gridStub);
229+
service.updatePagination(0, 20);
230+
const query = service.buildQuery();
231+
232+
expect(removeSpaces(query)).toBe(removeSpaces(expectation));
233+
});
234+
235+
it('should make sure the offset pagination is never below zero, even when new is 1 the offset should remain 0', () => {
236+
const expectation = `query{ users(first:20, offset:0){ totalCount, nodes{ id, field1, field2 }}}`;
237+
const columns = [{ id: 'field1', field: 'field1', width: 100 }, { id: 'field2', field: 'field2', width: 100 }];
238+
239+
service.init({ datasetName: 'users', columnDefinitions: columns }, paginationOptions, gridStub);
240+
service.updatePagination(1, 20);
241+
const query = service.buildQuery();
242+
243+
expect(removeSpaces(query)).toBe(removeSpaces(expectation));
244+
});
245+
224246
it('should be able to provide "sortingOptions" and see the query string include the sorting', () => {
225247
const expectation = `query{ users(first:20, offset:40,orderBy:[{field:field1, direction:DESC}]){ totalCount, nodes{ id, field1, field2 }}}`;
226248
const columns = [{ id: 'field1', field: 'field1', width: 100 }, { id: 'field2', field: 'field2', width: 100 }];

src/app/modules/angular-slickgrid/services/graphql.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ export class GraphqlService implements BackendService {
462462
} else {
463463
paginationOptions = {
464464
first: pageSize,
465-
offset: (newPage - 1) * pageSize
465+
offset: (newPage > 1) ? ((newPage - 1) * pageSize) : 0 // recalculate offset but make sure the result is always over 0
466466
};
467467
}
468468

src/app/modules/angular-slickgrid/services/pagination.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ export class PaginationService {
121121
}
122122

123123
goToLastPage(event?: any): Promise<any> {
124-
this._pageNumber = this._pageCount;
124+
this._pageNumber = this._pageCount || 1;
125125
return this.processOnPageChanged(this._pageNumber, event);
126126
}
127127

@@ -253,7 +253,7 @@ export class PaginationService {
253253
this._dataTo = 0;
254254
this._pageNumber = 0;
255255
} else {
256-
this._dataFrom = (this._pageNumber * this._itemsPerPage) - this._itemsPerPage + 1;
256+
this._dataFrom = this._pageNumber > 1 ? ((this._pageNumber * this._itemsPerPage) - this._itemsPerPage + 1) : 1;
257257
this._dataTo = (this._totalItems < this._itemsPerPage) ? this._totalItems : (this._pageNumber * this._itemsPerPage);
258258
if (this._dataTo > this._totalItems) {
259259
this._dataTo = this._totalItems;

0 commit comments

Comments
 (0)