prisma-cursor-pagination
v0.2.2
Published
Relay cursor pagination helpers for prisma.
Downloads
240
Maintainers
Readme
prisma-cursor-pagination
Relay cursor pagination helpers for prisma.
Installation
$ npm i prisma-cursor-pagination --save
# --- or ---
$ yarn add prisma-cursor-pagination
📘 Usage
import { parsePaginationArgs } from "prisma-cursor-pagination";
const resolvers = {
Query: {
projects: async (_, args) => {
// parse pagination arguments (first: Int! & after: ID / last: Int! & before: ID)
const { findManyArgs, toConnection } = parsePaginationArgs(args);
const projects = await prisma.project.findMany(findManyArgs);
// transform prisma result into a relay connection
return toConnection(projects);
},
users: async (_, args) => {
const { findManyArgs, toConnection } = parsePaginationArgs(args);
const [totalCount, users] = await Promise.all([
prisma.user.count(),
prisma.user.findMany(findManyArgs),
]);
// add non-standard data in connection (here totalCount)
return { totalCount, ...toConnection(users) };
},
},
};
⚙️ API
parsePaginationArgs(args, options)
Take the query arguments and return prisma findMany
arguments and a function to transform prisma result into a relay connection.
type parsePaginationArgs = (
args: Partial<{
first: number | null;
after: string | null;
last: number | null;
before: string | null;
}>,
options: {
connectionName?: string; // optional, used only to improve error message
},
) => {
findManyArgs: {
cursor?: {
id: string;
};
skip?: number;
take: number;
};
toConnection: <T extends { id: string }>(
items: T[],
) => {
edges: Edge<T>[];
pageInfo: PageInfo;
};
};