tinylibs


Issues Stars License Codecov FOSSA Status Join the chat at https://gitter.im/tinylibs-js-org/community Speed Blazing

Latest Version Downloads JsDelivr Bundlephobia Packagephobia


  

🧬 Prisma TypeGraphql Pagination


Prisma TypeGraphql Pagination builds prisma pagination types for type-graphql.


import { createPaginator } from 'prisma-typegraphql-pagination';

const UserPagination = createPaginator(User);

@Resolver(() => User)
export class UserResolver {
  // ...
  async friends(
    @Ctx() { prisma }: GqlContext,
    @Args(() => UserPagination) pagination: Prisma.UserFindManyArgs
  ): 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


Installing

npm   install prisma-typegraphql-pagination
yarn  add     prisma-typegraphql-pagination
import { createPaginator } from 'prisma-typegraphql-pagination';
import { createAxiosHooks } from 'https://cdn.skypack.dev/prisma-typegraphql-pagination@latest';

// 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.


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.

FOSSA Status