Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/krislefeber/nestjs-dataloader
Dataloader plugin for NestJS
https://github.com/krislefeber/nestjs-dataloader
dataloader nestjs
Last synced: 2 days ago
JSON representation
Dataloader plugin for NestJS
- Host: GitHub
- URL: https://github.com/krislefeber/nestjs-dataloader
- Owner: krislefeber
- License: mit
- Created: 2019-10-11T02:46:33.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2024-01-17T05:15:02.000Z (10 months ago)
- Last Synced: 2024-11-09T00:38:57.101Z (5 days ago)
- Topics: dataloader, nestjs
- Language: TypeScript
- Homepage: https://krislefeber.github.io/nestjs-dataloader/
- Size: 353 KB
- Stars: 148
- Watchers: 7
- Forks: 45
- Open Issues: 23
-
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) accountLoader: DataLoader): Promise {
return accountLoader.loadMany(ids);
}
}
```The important thing to note is that the parameter of the `@Loader` decorator is the entity/class of the `NestDataLoader` 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.