Skip to content

Instantly share code, notes, and snippets.

@Diluka
Last active September 28, 2022 04:03
Show Gist options
  • Save Diluka/8b87158fdf55808e44b1ed3c513f2eea to your computer and use it in GitHub Desktop.
Save Diluka/8b87158fdf55808e44b1ed3c513f2eea to your computer and use it in GitHub Desktop.
add offset support in cursor pagination for nestjs-query
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