Last active
September 28, 2022 04:03
-
-
Save Diluka/8b87158fdf55808e44b1ed3c513f2eea to your computer and use it in GitHub Desktop.
add offset support in cursor pagination for nestjs-query
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
import { applyDecorators } from '@nestjs/common'; | |
import { Field, Int } from '@nestjs/graphql'; | |
import { CursorPagingType } from '@ptc-org/nestjs-query-graphql'; | |
import { LimitOffsetPagerStrategy } from '@ptc-org/nestjs-query-graphql/src/types/connection/cursor/pager/strategies'; | |
import { getOrCreateCursorPagingType } from '@ptc-org/nestjs-query-graphql/src/types/query/paging'; | |
import { CannotUseWith } from '@ptc-org/nestjs-query-graphql/src/types/validators'; | |
import { IsOptional, Min, Validate } from 'class-validator'; | |
const CPT = getOrCreateCursorPagingType(); | |
applyDecorators( | |
Field(() => Int, { nullable: true, description: 'Paginate offset (added by hotfix)' }), | |
IsOptional(), | |
Validate(CannotUseWith, ['after', 'before']), | |
Min(0), | |
)(CPT.prototype, 'offset'); | |
// @ts-ignore | |
const getOffset = LimitOffsetPagerStrategy.prototype.getOffset; | |
// @ts-ignore | |
LimitOffsetPagerStrategy.prototype.getOffset = function (cursor: CursorPagingType) { | |
return cursor.offset ?? getOffset.call(this, cursor); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment