Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/simenandre/nestjs-dataloader
Dataloader plugin for NestJS
https://github.com/simenandre/nestjs-dataloader
Last synced: 3 months ago
JSON representation
Dataloader plugin for NestJS
- Host: GitHub
- URL: https://github.com/simenandre/nestjs-dataloader
- Owner: simenandre
- License: mit
- Archived: true
- Fork: true (krislefeber/nestjs-dataloader)
- Created: 2021-10-02T19:40:46.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2023-05-14T12:55:23.000Z (over 1 year ago)
- Last Synced: 2024-05-28T17:14:49.125Z (7 months ago)
- Language: TypeScript
- Homepage: https://krislefeber.github.io/nestjs-dataloader/
- Size: 473 KB
- Stars: 2
- Watchers: 0
- Forks: 2
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# NestJS Dataloader
![Node.js CI](https://github.com/krislefeber/nestjs-dataloader/workflows/Node.js%20CI/badge.svg)
[![Coverage Status](https://coveralls.io/repos/github/krislefeber/nestjs-dataloader/badge.svg?branch=master)](https://coveralls.io/github/krislefeber/nestjs-dataloader?branch=master)NestJS dataloader simplifies adding [graphql/dataloader](https://github.com/graphql/dataloader) to your NestJS project. DataLoader aims to solve the common N+1 loading problem.
## Installation
Install with yarn
```bash
yarn add nestjs-dataloader
```Install with npm
```bash
npm install --save nestjs-dataloader
```## Usage
### NestDataLoader Creation
We start by implementing the `NestDataLoader` interface. This tells `DataLoader` how to load our objects.
```typescript
import * as DataLoader from 'dataloader';
import { Injectable } from '@nestjs/common';
import { NestDataLoader } from 'nestjs-dataloader';
...@Injectable()
export class AccountLoader implements NestDataLoader {
constructor(private readonly accountService: AccountService) { }generateDataLoader(): DataLoader {
return new DataLoader(keys => this.accountService.findByIds(keys));
}
}
```The first generic of the interface is the type of ID the datastore uses. The second generic is the type of object that will be returned. In the above instance, we want `DataLoader` to return instances of the `Account` class.
### Providing the NestDataLoader
For each NestDataLoader we create, we need to provide it to our module.
```typescript
import { Module } from '@nestjs/common';
import { APP_INTERCEPTOR } from '@nestjs/core';
import {DataLoaderInterceptor} from 'nestjs-dataloader'
...@Module({
providers: [
AccountResolver,
AccountLoader,
{
provide: APP_INTERCEPTOR,
useClass: DataLoaderInterceptor,
},
],})
export class ResolversModule { }
```### Using the NestDataLoader
Now that we have a dataloader and our module is aware of it, we need to pass it as a parameter to an endpoint in our graphQL resolver.
```typescript
import * as DataLoader from 'dataloader';
import { Loader } from 'nestjs-dataloader';
...@Resolver(Account)
export class AccountResolver {@Query(() => [Account])
public getAccounts(
@Args({ name: 'ids', type: () => [String] }) ids: string[],
@Loader(AccountLoader.name) accountLoader: DataLoader): Promise {
return accountLoader.loadMany(ids);
}
}
```The important thing to note is that the parameter of the `@Loader` decorator is the name of the `NestDataLoader` class we want to be injected to the method. The DataLoader library will handle bulk retrieval and caching of our requests. Note that the caching is stored on a per-request basis.
## Contributing
Pull requests are always welcome. For major changes, please open an issue first to discuss what you would like to change.