-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { Op } from '@unocha/hpc-api-core/src/db/util/conditions'; | ||
import { createBrandedValue } from '@unocha/hpc-api-core/src/util/types'; | ||
import { ObjectType, Field } from 'type-graphql'; | ||
|
||
export interface ItemPaged { | ||
cursor: number; | ||
} | ||
|
||
@ObjectType() | ||
export class PageInfo<TSortFields extends string> { | ||
@Field({ nullable: false }) | ||
hasNextPage: boolean; | ||
|
||
@Field({ nullable: false }) | ||
hasPreviousPage: boolean; | ||
|
||
@Field({ nullable: false }) | ||
startCursor: number; | ||
|
||
@Field({ nullable: false }) | ||
endCursor: number; | ||
|
||
@Field({ nullable: false }) | ||
pageSize: number; | ||
|
||
@Field(() => String, { nullable: false }) | ||
sortField: TSortFields; | ||
|
||
@Field({ nullable: false }) | ||
sortOrder: string; | ||
|
||
@Field({ nullable: false }) | ||
total: number; | ||
} | ||
|
||
export function prepareConditionFromCursor( | ||
sortCondition: { column: string; order: 'asc' | 'desc' }, | ||
afterCursor?: number, | ||
beforeCursor?: number | ||
): any { | ||
if (afterCursor && beforeCursor) { | ||
throw new Error('Cannot use before and after cursor at the same time'); | ||
} | ||
|
||
if (afterCursor || beforeCursor) { | ||
const isAscending = sortCondition.order === 'asc'; | ||
const cursorValue = afterCursor || beforeCursor; | ||
|
||
let op; | ||
if (isAscending) { | ||
op = afterCursor ? Op.GT : Op.LT; | ||
} else { | ||
op = beforeCursor ? Op.GT : Op.LT; | ||
} | ||
|
||
return { | ||
id: { | ||
[op]: createBrandedValue(cursorValue), | ||
}, | ||
}; | ||
} | ||
|
||
return {}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { Op } from '@unocha/hpc-api-core/src/db/util/conditions'; | ||
import { prepareConditionFromCursor } from '../../src/utils/graphql/pagination'; | ||
|
||
describe('Based on cursor and order for pagination', () => { | ||
describe('Order is asc', () => { | ||
const sortCondition = { column: 'id', order: 'asc' as const }; | ||
|
||
it("Should return 'GT' when afterCursor is defined", () => { | ||
const afterCursor = 1; | ||
const beforeCursor = undefined; | ||
const result = prepareConditionFromCursor( | ||
sortCondition, | ||
afterCursor, | ||
beforeCursor | ||
); | ||
expect(result.id).toEqual({ [Op.GT]: afterCursor }); | ||
}); | ||
|
||
it("Should return 'LT' when beforeCursor is defined", () => { | ||
const afterCursor = undefined; | ||
const beforeCursor = 1; | ||
const result = prepareConditionFromCursor( | ||
sortCondition, | ||
afterCursor, | ||
beforeCursor | ||
); | ||
expect(result.id).toEqual({ [Op.LT]: beforeCursor }); | ||
}); | ||
|
||
it('Should throw an error when both afterCursor and beforeCursor are defined', () => { | ||
const afterCursor = 1; | ||
const beforeCursor = 2; | ||
expect(() => | ||
prepareConditionFromCursor(sortCondition, afterCursor, beforeCursor) | ||
).toThrowError('Cannot use before and after cursor at the same time'); | ||
}); | ||
}); | ||
|
||
describe("Order is 'desc'", () => { | ||
const sortCondition = { column: 'id', order: 'desc' as const }; | ||
|
||
it("Should return 'LT' when afterCursor is defined", () => { | ||
const afterCursor = 1; | ||
const beforeCursor = undefined; | ||
const result = prepareConditionFromCursor( | ||
sortCondition, | ||
afterCursor, | ||
beforeCursor | ||
); | ||
expect(result.id).toEqual({ [Op.LT]: afterCursor }); | ||
}); | ||
|
||
it("Should return 'GT' when beforeCursor is defined", () => { | ||
const afterCursor = undefined; | ||
const beforeCursor = 1; | ||
const result = prepareConditionFromCursor( | ||
sortCondition, | ||
afterCursor, | ||
beforeCursor | ||
); | ||
expect(result.id).toEqual({ [Op.GT]: beforeCursor }); | ||
}); | ||
|
||
it('Should throw an error when both afterCursor and beforeCursor are defined', () => { | ||
const afterCursor = 1; | ||
const beforeCursor = 2; | ||
expect(() => | ||
prepareConditionFromCursor(sortCondition, afterCursor, beforeCursor) | ||
).toThrowError('Cannot use before and after cursor at the same time'); | ||
}); | ||
}); | ||
}); |