open-index/open-npm huggingface dataset:
npm package: @nestjs-devkit/dataloader
Description: Dependency Injection friendly adaptation of dataloaders for NestJS.
Author: Char2s
Latest version: 0.1.1

# Dataloader

Dependency Injection friendly adaptation of [graphql/dataloader](https://github.com/graphql/dataloader) for NestJS.

> This package internally uses `AsyncLocalStorage` to provide a clean cache map for each dataloader in each request, but it may have [a slight negative impact on the performance](https://github.com/nodejs/node/issues/34493#issuecomment-845094849).

## Installation

```
npm i @nestjs-devkit/dataloader
```

## Tutorial

### Defining DataLoaders

To define a dataloader, we simply extend the abstract `DataLoader` and implement the `resolve()` method.

```ts
import { DataLoader } from "@nestjs-devkit/dataloader";
// ... more imports

@Injectable()
export class UserIdLoader extends DataLoader<number, User> {
  constructor(@InjectRepository(User) private repo: EntityRepository<User>) {}

  protected async resolve(keys: number[]): Promise<User[]> {
    return this.repo.find({ id: { $in: keys } });
  }
}
```

### Preparing DataLoaders

Before using the dataloaders, we need to firstly provide these dataloaders.

```ts
@Module({
  providers: [UserIdLoader],
  exports: [UserIdLoader],
})
export class UsersModule {}
```

Besides, the `DataLoaderModule` is also required for them to work.

```ts
@Module({
  imports: [DataLoaderModule.forRoot()],
})
export class AppModule {}
```

### Using Dataloaders

Now we can use the dataloaders simply by injecting them into your resolvers.

```ts
@Resolver(() => Book)
export class BooksResolver {
  constructor(private userIdLoader: UserIdLoader) {}

  @ResolveField()
  async owner(@Parent() book: Book) {
    return this.userIdLoader.load(book.owner.id);
  }
}
```

Remember to import the module where the dataloader is defined.

```ts
@Module({
  imports: [forwardRef(() => UsersModule)],
})
export class BooksModule {}
```

## Advanced

### Cross-Platform

There is a `DataLoaderContext` used to provide clean cache maps for dataloaders in each request, which need to be applied by a middleware.

By default, a built-in middleware will be used to apply the context for Express. You can disable it and implement your own one for other platforms.

```ts
@Module({
  imports: [DataLoaderModule.forRoot({ middleware: false })],
})
export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer): void {
    consumer.apply(FastifyDataLoaderModule).forRoutes("(*)");
  }
}
```

## Contributing

Pull requests are always welcome, but please open an issue firstly before major changes. ;]
npm package: @nestjs-devkit/mikro-graphql-flusher
Description: GraphQL bulk-operation solution for MikroORM in Nest.
Latest version: 0.1.0

# Mikro GraphQL Flusher

GraphQL bulk-operation solution for MikroORM in Nest.

> This package internally uses `AsyncLocalStorage` to count mutations in requests, which may have [a slight negative impact on the performance](https://github.com/nodejs/node/issues/34493#issuecomment-845094849).

## Installation

```
npm i @nestjs-devkit/mikro-graphql-flusher
```

## Why?

The GraphQL runtime has built-in support for bulk-operations, which means you can send an array of GraphQL queries and mutations in a single HTTP request and receive an array of results.

While, in Nest, all bulk queries and mutations are handled in parallel. Therefore, when there are multiple mutations in a request, invoking `em.flush()` in resolvers will usually conflict and causes errors because `em.flush()` is not allowed to be invoked when another invoke is pending.

So how will we solve this problem? By importing the `MikroFlusherModule`, a global interceptor will be registered that will defer the return of each mutation after it has been handled. When all the mutations in the request have been deferred, the interceptor will invoke `em.flush()` to save the changes of all these mutations, and then return the results of all these mutations at once.

You can send bulk-operation requests to your GraphQL server now, and you don't need to invoke `em.flush()` manually any more! How wonderful!

## Tutorial

As said before, the only thing we need to do is to import the `MikroFlusherModule`:

```ts
@Module({
  imports: [
    MikroFlusherModule,
    // ...
  ],
})
export class AppModule {}
```

Done! (applies to Express only)

## Contributing

Pull requests are always welcome! ;]