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 🙏

© 2024 – Pkg Stats / Ryan Hefner

nexus-pagination-plugin

v0.1.6

Published

A plugin for adding offset-based pagination to your nexus types

Downloads

166

Readme

nexus-pagination-plugin

This pagination plugin provides a new method on the object definition builder, enabling paginated associations between types, following the offset-based pagination standard defined below.

Offset-Based Pagination Standard

A paginated query field should receive two arguments:

  • page
    • The current page being request. Defaults to 1
  • pageSize
    • The size of pages being used. Defaults to 25

A paginated query field should return a type that contains the following:

  • results
    • A collection of the data requested
  • pageInfo
    • A type that contains information about the pagination
    • page
    • nextPage
    • totalPages

Installation

yarn add nexus-pagination-plugin

or

npm install nexus-pagination-plugin

Setup

import { makeSchema} from 'nexus';
import { paginationPlugin } from 'nexus-pagination-plugin';

export const schema = makeSchema({
  // ... types, etc,
  plugins: [
    // ... other plugins
    paginationPlugin()
  ],
});

Example Usage

This plugin surfaces a t.paginatedQueryResult function that can be used to add pagination to a given type.

// User Type
export const User = objectType({
  name: 'User',
  description: 'A User',
  definition(t) {
    t.nonNull.id('id');
    t.nonNull.string('firstName');
    t.nonNull.string('lastName');
  },
});

// add paginated users query
export const UsersQuery = queryField((t) => {
  t.paginatedQueryResult('users', {
    type: 'User',
    resolve: async (_root, args, ctx) => {
      const { take, skip } = ctx.paginationParams;

      const users = await ctx.prisma.user.findMany({
        skip,
        take,
      });
      const usersCount = await ctx.prisma.user.count();
      const pageInfo = ctx.calculatePageInfo(usersCount);

      return {
        results: users,
        pageInfo
      }
    },
  })
})

resolve: (root, args, ctx) => ...

t.paginatedQueryResult wraps the resolve function passed in the query config and adds the following fields on the context object.

ctx.paginationParams: {take: number, skip: number}

  • take specifies the page size
  • skip specifies how many to skip based on the page size and page arguments

ctx.calculatePageInfo: (count: number) => PageInfo

  • count specifies the total count of records in the DB
  • Returns type PageInfo

Generated Types

PageInfo

objectType({
  name: 'PageInfo',
  definition(t) {
    t.int('page');
    t.int('nextPage');
    t.int('totalPages');
  },
})

Paginated${targeTypename}s

objectType({
  name: generatedTypeName,
  definition(t2) {
    t2.nonNull.list.field('results', {
      type: fieldConfig.type,
      description: `Collection of ${fieldName}`,
    });

    t2.nonNull.field('pageInfo', {
      type: 'PageInfo',
      description: 'Pagination information',
    });
  },
})

For a paginated field defined on Query like this:

queryField((t) => {
  t.paginatedQueryField('foos', {
    type: 'Foo',
    // ... any additional query config
  });
});

The following types would be generated:

type PageInfo {
  nextPage: Int
  page: Int
  totalPages: Int
}

type PaginatedFoos {
  pageInfo: PageInfo!
  results: [Foo]!
}

type Query {
  """ ... other Query fields """

  foos(
    page: Int = 1,
    pageSize: Int = 25
  ): PaginatedFoos
}

Note that the collection type will be added to whatever parent type is specified. This means that if you were to define a paginated field on another object rather than on Query like this:

objectType({
  type: 'Bar',
  definition(t) {
    // ... other definitions
    t.paginatedQueryField('foos', {
      type: 'Foo',
      // ... any additional query config
    });
  }
})

The foos query would be added to the Bar type rather than the Query type:

type Bar {
  """ ... other Bar fields """

  foos(
    page: Int = 1
    pageSize: Int = 25
  ): PaginatedFoos
}

Options

defaultPageSize: number

Used to specify a different default for the generated pageSize argument. Defaults to 25

Global usage:

paginationPlugin({
  defaultPageSize: 10
})

Field usage:

export const UsersQuery = queryField((t) => {
  t.paginatedQueryResult('users', {
    // ... any additional query fields
    defaultPageSize: 10
  })  
})

getGeneratedTypename: (targetTypename: string) => string

Used to specify a different generated typename for the paginated types. Defaults to Paginated${targetTypename}s

Usage:

paginationPlugin({
  getGeneratedTypename: (targetTypename) => `${targetTypename}sPaginated`
})

generatedTypename: string

Used to specify a different generated typename for a specific field. Defaults to Paginated${targetTypename}s or whatever is defined by getGeneratedTypename

Usage:

export const FinishesQuery = queryField((t) => {
  t.paginatedQueryResult('finishes', {
    type: 'Finish',
    // ... any additional query fields
    generatedTypename: 'PaginatedFinishes'
  })
})