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
@@ -25,8 +25,6 @@ and Nicholas Schrock (@schrockn) from [Facebook](https://www.facebook.com/), the
25
25
26
26
-[Features](#features)
27
27
-[Examples](#examples)
28
-
-[Differences to reference implementation](#differences-to-reference-implementation)
29
-
-[Manual dispatching](#manual-dispatching)
30
28
-[Let's get started!](#lets-get-started)
31
29
-[Installing](#installing)
32
30
-[Building](#building)
@@ -290,9 +288,136 @@ this was not in place, then all the promises to data will never be dispatched ot
290
288
291
289
See below for more details on `dataLoader.dispatch()`
292
290
293
-
## Differences to reference implementation
291
+
### Error object is not a thing in a type safe Java world
292
+
293
+
In the reference JS implementation if the batch loader returns an `Error` object back from the `load()` promise is rejected
294
+
with that error. This allows fine grain (per object in the list) sets of error. If I ask for keys A,B,C and B errors out the promise
295
+
for B can contain a specific error.
296
+
297
+
This is not quite as loose in a Java implementation as Java is a type safe language.
298
+
299
+
A batch loader function is defined as `BatchLoader<K, V>` meaning for a key of type `K` it returns a value of type `V`.
300
+
301
+
It cant just return some `Exception` as an object of type `V`. Type safety matters.
302
+
303
+
However you can use the `Try` data type which can encapsulate a computation that succeeded or returned an exception.
294
304
295
-
### Manual dispatching
305
+
```java
306
+
Try<String> tryS =Try.tryCall(() -> {
307
+
if (rollDice()) {
308
+
return"OK";
309
+
} else {
310
+
thrownewRuntimeException("Bang");
311
+
}
312
+
});
313
+
314
+
if (tryS.isSuccess()) {
315
+
System.out.println("It work "+ tryS.get());
316
+
} else {
317
+
System.out.println("It failed with exception : "+ tryS.getThrowable());
318
+
319
+
}
320
+
```
321
+
322
+
DataLoader supports this type and you can use this form to create a batch loader that returns a list of `Try` objects, some of which may have succeeded
323
+
and some of which may have failed. From that data loader can infer the right behavior in terms of the `load(x)` promise.
You could choose to use one of the fancy cache implementations from Guava or Kaffeine and wrap it in a `CacheMap` wrapper ready
418
+
for data loader. They can do fancy things like time eviction and efficient LRU caching.
419
+
420
+
## Manual dispatching
296
421
297
422
The original [Facebook DataLoader](https://github.com/facebook/dataloader) was written in Javascript for NodeJS. NodeJS is single-threaded in nature, but simulates
298
423
asynchronous logic by invoking functions on separate threads in an event loop, as explained
@@ -320,21 +445,6 @@ and there are also gains to this different mode of operation:
320
445
However, with batch execution control comes responsibility! If you forget to make the call to `dispatch()` then the futures
321
446
in the load request queue will never be batched, and thus _will never complete_! So be careful when crafting your loader designs.
322
447
323
-
### Error object is not a thing in a type safe Java world
324
-
325
-
In the reference JS implementation if the batch loader returns an `Error` object back then the `loadKey()` promise is rejected
326
-
with that error. This allows fine grain (per object in the list) sets of error. If I ask for keys A,B,C and B errors out the promise
327
-
for B can contain a specific error.
328
-
329
-
This is not quite as neat in a Java implementation
330
-
331
-
A batch loader function is defined as `BatchLoader<K, V>` meaning for a key of type `K` it returns a value of type `V`.
332
-
333
-
It cant just return some `Exception` as an object of type `V` since Java is type safe.
334
-
335
-
You in order for a batch loader function to return an `Exception` it must be declared as `BatchLoader<K, Object>` which
336
-
allows both values and exceptions to be returned . Some type safety is lost in this case if you want
337
-
to use the mix of exceptions and values pattern.
338
448
339
449
## Let's get started!
340
450
@@ -350,7 +460,7 @@ repositories {
350
460
}
351
461
352
462
dependencies {
353
-
compile 'org.dataloader:java-dataloader:1.0.0'
463
+
compile 'com.graphql-java:java-dataloader:1.0.2'
354
464
}
355
465
```
356
466
@@ -385,13 +495,13 @@ deal with minor changes.
385
495
386
496
This library was originally written for use within a [VertX world](http://vertx.io/) and it used the vertx-core `Future` classes to implement
387
497
itself. All the heavy lifting has been done by this project : [vertx-dataloader](https://github.com/engagingspaces/vertx-dataloader)
388
-
including the extensive testing.
498
+
including the extensive testing (which itself came from Facebook).
389
499
390
500
This particular port was done to reduce the dependency on Vertx and to write a pure Java 8 implementation with no dependencies and also
391
501
to use the more normative Java CompletableFuture.
392
502
393
-
[vertx-core](http://vertx.io/docs/vertx-core/java/) is not a lightweight library by any means
394
-
so having a pure Java 8 implementation is very desirable.
503
+
[vertx-core](http://vertx.io/docs/vertx-core/java/) is not a lightweight library by any means so having a pure Java 8 implementation is
504
+
very desirable.
395
505
396
506
397
507
This library is entirely inspired by the great works of [Lee Byron](https://github.com/leebyron) and
* Creates new DataLoader with the specified batch loader function and default options
98
+
* (batching, caching and unlimited batch size) where the batch loader function returns a list of
99
+
* {@link org.dataloader.Try} objects.
100
+
*
101
+
* This allows you to capture both the value that might be returned and also whether exception that might have occurred getting that individual value. If its important you to
102
+
* know gther exact status of each item in a batch call and whether it threw exceptions when fetched then
103
+
* you can use this form to create the data loader.
104
+
*
105
+
* @param batchLoadFunction the batch load function to use that uses {@link org.dataloader.Try} objects
0 commit comments