Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/overnested/nestjs-gql-connections
Easy and spec-compliant pagination/connections for your NestJS GraphQL objects.
https://github.com/overnested/nestjs-gql-connections
graphql nestjs pagination relay
Last synced: 2 months ago
JSON representation
Easy and spec-compliant pagination/connections for your NestJS GraphQL objects.
- Host: GitHub
- URL: https://github.com/overnested/nestjs-gql-connections
- Owner: overnested
- License: mit
- Created: 2021-11-17T18:45:28.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-12-15T02:35:12.000Z (about 1 year ago)
- Last Synced: 2024-11-09T10:55:31.722Z (3 months ago)
- Topics: graphql, nestjs, pagination, relay
- Language: TypeScript
- Homepage:
- Size: 169 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# GraphQL Connections (Pagination)
This package is a spec-compliant set of models and utilities based on [Relay's Graphql Cursor Connections Specification](https://relay.dev/graphql/connections.htm) and is created using [Generics](https://docs.nestjs.com/graphql/resolvers#generics)
## Introduction
To use pagination in GraphQL, it's proposed by the spec that we use the "Connections" pattern and expose these connections in a standardized way.
- In queries, you use `PaginationArgs` to slice and paginate the result.
- In response, you return `Connection` objects to provide cursors and a way to tell the client when more results are available.## Installation
On Yarn:
```shell
yarn add nestjs-gql-connections
```On NPM:
```shell
npm install nestjs-gql-connections
```## Usage
You can create a new paginated model like so:
```ts
import { Paginated } from 'nestjs-gql-connections';
import { Product } from '../product.model';export class ProductConnection extends Paginated(Product) {}
```Note that for your connection to be compliant to relay's specifications, its name should end with `Connection`. ([reference](https://relay.dev/graphql/connections.htm#sec-Reserved-Types))
To create a query for a paginated resource, do like so:
```ts
@Query(() => ProductConnection)
resourcePublications(
@Args() { after, before, first, last, skip }: PaginationArgs,
) {
// return data based on pagination
}
```## Apollo Cache Compatability
Be default, Apollo server won't cache if a field on resolver is an object (non-scalar), thinking non-scalar fields need fetching and are probably somehow independent ([more info](https://www.apollographql.com/docs/apollo-server/performance/caching/#default-maxage)). While in the case of pagination's `node` object or `edges`, they are already fetched and available. So we've added cache control inheritance to all the respective fields and you don't really have to worry about that. This package goes nicely with the other [overnested](https://github.com/overnested) package for GraphQL caching, [overnested/graphql-cache-control](https://github.com/overnested/nestjs-gql-cache-control)
## With Prisma
If you use Prisma, there is another spec-compliant package that facilitates returning data as connections: [@devoxa/prisma-relay-cursor-connection](https://github.com/devoxa/prisma-relay-cursor-connection)