🧬 Prisma TypeGraphql Pagination
Prisma TypeGraphql Pagination
builds prisma pagination types for type-graphql.
import { ... } from 'type-graphql'
import { createPaginator } from 'prisma-typegraphql-pagination';
const FriendsPagination = createPaginator(User);
@Resolver(() => User)
export class UserResolver {
// ...
async friends(
@Ctx() { prisma }: GqlContext,
@Args(() => FriendsPagination) pagination: Paginator<User>
): Promise<User[]> {
return prisma.user.findMany({ where: { id }, ...pagination });
// Or for a field resolver
return prisma.user.findUnique({ where: { id } }).friends(pagination);
}
}
Table of Contents
Guide
This is a pretty straightforward package, it just builds, at runtime, all graphql types required to represent the same Prisma.ENTITYFindManyArgs
, like Prisma.UserFindManyArgs
, which is used prisma.user.findMany(HERE)
.
Simple usage
You can create many pagination types for many entities, and this is how we do it:
import { createPaginator, type Paginator } from 'prisma-typegraphql-pagination';
// This UserPagination class can be used for any pagination for a User[] result.
// It is the type that type-graphql will understand
const UserPagination = createPaginator(User);
function userPagination(@Args(() => UserPagination) pag: Paginator<User>) {
// ...
}
Configuration
This package is entirely built in mind of using TSDocs, and as the possible configuration is very simple, you can see it in the following file. src/options.ts
.
Class Validator
This package provides an optional class validator integration, it is activated if the package class-validator
is installed.
It automatically generates @Validation
decorators for the pagination types, so you can expect a more robust and secure pagination.
Got any exceptions? You can always report them to our Issues page.
Getting a generated class at runtime.
If, for some reason, you need one of the generated classes, like the UserFieldsEnum
class generated by your createPagination(User)
call, you can get it in the same way as any other class from type-graphql
.
After getting the array of all classes, you can do a for-of loop and get the one you need by checking its name.
const metadata = getMetadataStorage(); // from type-graphql
metadata.objectTypes; // or
metadata.inputTypes; // or
metadata.enums;
// Example:
for (const type of metadata.objectTypes) {
if (type.name === 'UserEntityPaginator') {
// ...
}
}
License
Licensed under the MIT. See LICENSE
for more informations.
Contact
See my contact information on my github profile or open a new issue.