Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
nexus-pagination-plugin
Advanced tools
A plugin for adding offset-based pagination to your nexus types
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.
A paginated query field should receive two arguments:
page
pageSize
A paginated query field should return a type that contains the following:
results
pageInfo
page
nextPage
totalPages
yarn add nexus-pagination-plugin
or
npm install nexus-pagination-plugin
import { makeSchema} from 'nexus';
import { paginationPlugin } from 'nexus-pagination-plugin';
export const schema = makeSchema({
// ... types, etc,
plugins: [
// ... other plugins
paginationPlugin()
],
});
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 sizeskip
specifies how many to skip based on the page size and page argumentsctx.calculatePageInfo: (count: number) => PageInfo
count
specifies the total count of records in the DBPageInfo
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
}
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'
})
})
FAQs
A plugin for adding offset-based pagination to your nexus types
We found that nexus-pagination-plugin demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 6 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.