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
+51-1Lines changed: 51 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -258,13 +258,63 @@ module.exports = {
258
258
};
259
259
```
260
260
261
+
### Dataloader
262
+
moleculer-apollo-server supports [DataLoader](https://github.com/graphql/dataloader) via configuration in the resolver definition.
263
+
The called action must be compatible with DataLoader semantics -- that is, it must accept params with an array property and return an array of the same size,
264
+
with the results in the same order as they were provided.
265
+
266
+
To activate DataLoader for a resolver, simply add `dataLoader: true` to the resolver's property object in the `resolvers` property of the service's `graphql` property:
267
+
268
+
```js
269
+
settings: {
270
+
graphql: {
271
+
resolvers: {
272
+
Post: {
273
+
author: {
274
+
action:"users.resolve",
275
+
dataLoader:true,
276
+
rootParams: {
277
+
author:"id",
278
+
},
279
+
},
280
+
voters: {
281
+
action:"users.resolve",
282
+
dataLoader:true,
283
+
rootParams: {
284
+
voters:"id",
285
+
},
286
+
},
287
+
...
288
+
```
289
+
Since DataLoader only expects a single value to be loaded at a time, only one `rootParams` key/value pairing will be utilized, but `params` and GraphQL child arguments work properly.
290
+
291
+
You can also specify [options](https://github.com/graphql/dataloader#api) for construction of the DataLoader in the called action definition's `graphql` property. This is useful for setting things like `maxBatchSize'.
It is unlikely that setting any of the options which accept a function will work properly unless you are running moleculer in a single-node environment. This is because the functions will not serialize and be run by the moleculer-web Api Gateway.
310
+
261
311
## Examples
262
312
263
313
- [Simple](examples/simple/index.js)
264
314
- `npm run dev`
265
315
- [Full](examples/full/index.js)
266
316
- `npm run dev full`
267
-
-[Full With Dataloader](examples/full-dataloader/index.js)
317
+
- [Full With Dataloader](examples/full/index.js)
268
318
- set `DATALOADER` environment variable to `"true"`
0 commit comments