npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@hejtech/nestjs-graphql-relay-pagination

v2.0.2

Published

A flexible pagination module for NestJS applications (compatible with NestJS 11)

Readme

nestjs-graphql-relay-pagination

A flexible pagination module for NestJS GraphQL applications following the Relay cursor pagination specification.

✨ Compatible with both NestJS 10 and 11!

Installation

npm install @hejtech/nestjs-graphql-relay-pagination

Version Compatibility

| Package Version | NestJS Version | | --------------- | -------------------- | | 1.x.x | ^10.0.0 || ^11.0.0 | | 0.x.x | ^10.0.0 |

Features

  • 🚀 NestJS 10 & 11 Compatible - Works with both NestJS 10 and 11
  • 📄 Cursor-based pagination following the Relay specification
  • 🔧 Works seamlessly with NestJS and GraphQL
  • 🔐 Customizable cursor encoding/decoding
  • 🎯 Support for various filtering and sorting options
  • ⚡ Easy integration with existing NestJS applications
  • 🗄️ Currently optimized for TypeORM (with plans to support more ORMs)
  • 🧪 Comprehensive test coverage with both unit and integration tests

Usage

Import the module

import { Module } from '@nestjs/common';
import { PaginationModule } from '@hejtech/nestjs-graphql-relay-pagination';

@Module({
  imports: [
    PaginationModule,
    // other modules...
  ],
})
export class AppModule {}

Use in your resolvers

import { Resolver, Query, Args } from '@nestjs/graphql';
import {
  PaginationService,
  RelayPaginatedArgs,
} from '@hejtech/nestjs-graphql-relay-pagination';
import { YourEntity } from './your-entity.entity';

@Resolver(() => YourEntity)
export class YourResolver {
  constructor(private readonly paginationService: PaginationService) {}

  @Query(() => YourEntity)
  async findAll(@Args() args: RelayPaginatedArgs) {
    return this.paginationService.getManyWithCount({
      args,
      query: (queryBuilder) => {
        // Your query logic here
        return queryBuilder;
      },
    });
  }
}

Sorting and Filtering

This library intentionally delegates all sorting and filtering logic to your QueryBuilder. This approach provides maximum flexibility, allowing you to implement complex, multi-column sorting, utilize database-specific functions, and handle filtering dynamically.

You are responsible for adding any orderBy clauses to your QueryBuilder instance before passing it to the setup method. The pagination service will automatically introspect the QueryBuilder to determine the sort order for cursor creation.

If no orderBy clause is present on the QueryBuilder, the service will automatically sort by the entity's creation date (createdAt or the field decorated with @CreateDateColumn) in descending order as a sensible default.

Example

Here is an example of a resolver that handles dynamic sorting and filtering before pagination.

import { Resolver, Query, Args } from '@nestjs/graphql';
import {
  PaginationService,
  RelayPaginatedArgs,
} from '@hejtech/nestjs-graphql-relay-pagination';
import { YourEntity } from './your-entity.entity';
import { YourEntitySortField, SortOrder } from '../enums'; // Your application-specific enums
import { Repository } from 'typeorm';

@ArgsType()
class FindAllArgs extends RelayPaginatedArgs {
  @Field(() => String, { nullable: true })
  nameContains?: string;

  @Field(() => YourEntitySortField, { nullable: true })
  sortBy?: YourEntitySortField;

  @Field(() => SortOrder, { nullable: true })
  sortOrder?: SortOrder;
}

@Resolver(() => YourEntity)
export class YourResolver {
  constructor(
    private readonly paginationService: PaginationService<YourEntity>,
    private readonly entityRepository: Repository<YourEntity>, // Injected repository
  ) {}

  @Query(() => YourEntity)
  async findAll(@Args() args: FindAllArgs) {
    const { sortBy, sortOrder, nameContains, ...paginationArgs } = args;

    const queryBuilder = this.entityRepository.createQueryBuilder('entity');

    if (nameContains) {
      queryBuilder.andWhere('entity.name ILIKE :nameContains', {
        nameContains: `%${nameContains}%`,
      });
    }

    if (sortBy && sortOrder) {
      const sortColumn = this.getSortColumn(sortBy);
      queryBuilder.orderBy(sortColumn, sortOrder, 'NULLS LAST');
    }

    // The pagination service automatically uses the order set on the queryBuilder
    this.paginationService.setup(this.entityRepository, {
      ...paginationArgs,
      queryBuilder,
    });

    return this.paginationService.getManyWithCount();
  }

  private getSortColumn(sortBy: YourEntitySortField): string {
    const sortFieldMap: { [key in YourEntitySortField]: string } = {
      [YourEntitySortField.CREATED_AT]: 'entity.createdAt',
      [YourEntitySortField.NAME]: 'entity.name',
    };
    return sortFieldMap[sortBy];
  }
}

Development

Running the tests

This package includes both unit tests and integration tests. The integration tests require a running instance of CockroachDB, which is provided via Docker Compose.

To run the unit tests:

pnpm test

To run the integration tests:

  1. Start the required services:
pnpm run docker:up
  1. Run the integration tests:
pnpm run test:int
  1. When done, shut down the services:
pnpm run docker:down

API Documentation

PaginationService

The main service for handling pagination operations.

RelayPaginatedArgs

GraphQL arguments for Relay-style pagination, including first, last, before, and after.

PaginationFactory

Factory for creating pagination instances with custom configurations.

Current Limitations

This library is currently tightly coupled with TypeORM. We're working on making it more ORM-agnostic.

TODO

  • [ ] Create a more generic core pagination system
  • [ ] Decouple from TypeORM
  • [ ] Add more ORM adapters:
    • [ ] MikroORM
    • [ ] Sequelize
    • [ ] Prisma
  • [ ] Provide documentation for creating custom adapters
  • [ ] Add more comprehensive examples
  • [ ] Improve test coverage

License

MIT