You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+23-12Lines changed: 23 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,24 +1,29 @@
1
1
# NestJS Dataloader
2
+
2
3
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.
3
4
4
5
## Installation
5
6
6
7
Install with yarn
7
-
```bash
8
+
9
+
```bash
8
10
yarn add nestjs-dataloader
9
11
```
10
12
11
13
Install with npm
12
-
```bash
14
+
15
+
```bash
13
16
npm install --save nestjs-dataloader
14
-
```
17
+
```
15
18
16
19
## Usage
20
+
17
21
### NestDataLoader Creation
18
-
We start by implementing the ```NestDataLoader``` interface. This tells ```DataLoader``` how to load our objects.
19
22
20
-
```typescript
21
-
import*asDataLoaderfrom'dataloader';
23
+
We start by implementing the `NestDataLoader` interface. This tells `DataLoader` how to load our objects.
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.
41
+
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.
37
42
38
43
### Providing the NestDataLoader
44
+
39
45
For each NestDataLoader we create, we need to provide it to our module.
@@ -59,9 +65,11 @@ export class ResolversModule { }
59
65
```
60
66
61
67
### Using the NestDataLoader
68
+
62
69
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.
63
-
```typescript
64
-
import*asDataLoaderfrom'dataloader';
70
+
71
+
```typescript
72
+
importDataLoaderfrom'dataloader';
65
73
import { Loader } from'nestjs-dataloader';
66
74
...
67
75
@@ -76,6 +84,9 @@ export class AccountResolver {
76
84
}
77
85
}
78
86
```
79
-
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.
87
+
88
+
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.
89
+
80
90
## Contributing
91
+
81
92
Pull requests are always welcome. For major changes, please open an issue first to discuss what you would like to change.
0 commit comments