Skip to content

Commit 77a8ed3

Browse files
authored
Merge pull request #85 from graphql-java/introduce-a-factory-method
Introduces a Factory for creating DataLoaders
2 parents b570b56 + d114a40 commit 77a8ed3

14 files changed

+568
-129
lines changed

README.md

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ and Nicholas Schrock (@schrockn) from [Facebook](https://www.facebook.com/), the
5252
- Deals with partial errors when a batch future fails
5353
- Can disable batching and/or caching in configuration
5454
- Can supply your own [`CacheMap<K, V>`](https://github.com/graphql-java/java-dataloader/blob/master/src/main/java/io/engagingspaces/vertx/dataloader/CacheMap.java) implementations
55-
- Has very high test coverage (see [Acknowledgements](#acknowlegdements))
55+
- Has very high test coverage
5656

5757
## Examples
5858

@@ -70,7 +70,7 @@ a list of keys
7070
}
7171
};
7272

73-
DataLoader<Long, User> userLoader = DataLoader.newDataLoader(userBatchLoader);
73+
DataLoader<Long, User> userLoader = DataLoaderFactory.newDataLoader(userBatchLoader);
7474

7575
```
7676

@@ -188,7 +188,7 @@ for the context object.
188188
}
189189
};
190190

191-
DataLoader<String, String> loader = DataLoader.newDataLoader(batchLoader, options);
191+
DataLoader<String, String> loader = DataLoaderFactory.newDataLoader(batchLoader, options);
192192
```
193193

194194
The batch loading code will now receive this environment object and it can be used to get context perhaps allowing it
@@ -219,7 +219,7 @@ You can gain access to them as a map by key or as the original list of context o
219219
}
220220
};
221221

222-
DataLoader<String, String> loader = DataLoader.newDataLoader(batchLoader, options);
222+
DataLoader<String, String> loader = DataLoaderFactory.newDataLoader(batchLoader, options);
223223
loader.load("keyA", "contextForA");
224224
loader.load("keyB", "contextForB");
225225
```
@@ -255,7 +255,7 @@ For example, let's assume you want to load users from a database, you could prob
255255
}
256256
};
257257

258-
DataLoader<Long, User> userLoader = DataLoader.newMappedDataLoader(mapBatchLoader);
258+
DataLoader<Long, User> userLoader = DataLoaderFactory.newMappedDataLoader(mapBatchLoader);
259259

260260
// ...
261261
```
@@ -295,7 +295,7 @@ DataLoader supports this type and you can use this form to create a batch loader
295295
and some of which may have failed. From that data loader can infer the right behavior in terms of the `load(x)` promise.
296296

297297
```java
298-
DataLoader<String, User> dataLoader = DataLoader.newDataLoaderWithTry(new BatchLoader<String, Try<User>>() {
298+
DataLoader<String, User> dataLoader = DataLoaderFactory.newDataLoaderWithTry(new BatchLoader<String, Try<User>>() {
299299
@Override
300300
public CompletionStage<List<Try<User>>> load(List<String> keys) {
301301
return CompletableFuture.supplyAsync(() -> {
@@ -320,7 +320,7 @@ react to that, in a type safe manner.
320320
In certain uncommon cases, a DataLoader which does not cache may be desirable.
321321

322322
```java
323-
DataLoader.newDataLoader(userBatchLoader, DataLoaderOptions.newOptions().setCachingEnabled(false));
323+
DataLoaderFactory.newDataLoader(userBatchLoader, DataLoaderOptions.newOptions().setCachingEnabled(false));
324324
```
325325

326326
Calling the above will ensure that every call to `.load()` will produce a new promise, and requested keys will not be saved in memory.
@@ -387,7 +387,7 @@ You can configure the statistics collector used when you build the data loader
387387

388388
```java
389389
DataLoaderOptions options = DataLoaderOptions.newOptions().setStatisticsCollector(() -> new ThreadLocalStatisticsCollector());
390-
DataLoader<String,User> userDataLoader = DataLoader.newDataLoader(userBatchLoader,options);
390+
DataLoader<String,User> userDataLoader = DataLoaderFactory.newDataLoader(userBatchLoader,options);
391391

392392
```
393393

@@ -399,22 +399,24 @@ and `NoOpStatisticsCollector`.
399399
If you are serving web requests then the data can be specific to the user requesting it. If you have user specific data
400400
then you will not want to cache data meant for user A to then later give it user B in a subsequent request.
401401

402-
The scope of your `DataLoader` instances is important. You might want to create them per web request to ensure data is only cached within that
402+
The scope of your `DataLoader` instances is important. You will want to create them per web request to ensure data is only cached within that
403403
web request and no more.
404404

405-
If your data can be shared across web requests then you might want to scope your data loaders so they survive longer than the web request say.
405+
If your data can be shared across web requests then use a custom cache to keep values in a common place.
406+
407+
Data loaders are stateful components that contain promises (with context) that are likely share the same affinity as the request.
406408

407409
## Custom caches
408410

409-
The default cache behind `DataLoader` is an in memory `HashMap`. There is no expiry on this and it lives for as long as the data loader
411+
The default cache behind `DataLoader` is an in memory `HashMap`. There is no expiry on this, and it lives for as long as the data loader
410412
lives.
411413

412-
However you can create your own custom cache and supply it to the data loader on construction via the `org.dataloader.CacheMap` interface.
414+
However, you can create your own custom cache and supply it to the data loader on construction via the `org.dataloader.CacheMap` interface.
413415

414416
```java
415417
MyCustomCache customCache = new MyCustomCache();
416418
DataLoaderOptions options = DataLoaderOptions.newOptions().setCacheMap(customCache);
417-
DataLoader.newDataLoader(userBatchLoader, options);
419+
DataLoaderFactory.newDataLoader(userBatchLoader, options);
418420
```
419421

420422
You could choose to use one of the fancy cache implementations from Guava or Kaffeine and wrap it in a `CacheMap` wrapper ready

src/main/java/org/dataloader/DataLoader.java

Lines changed: 64 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ public class DataLoader<K, V> {
7575
* @param <V> the value type
7676
*
7777
* @return a new DataLoader
78+
*
79+
* @deprecated use {@link DataLoaderFactory} instead
7880
*/
81+
@Deprecated
7982
public static <K, V> DataLoader<K, V> newDataLoader(BatchLoader<K, V> batchLoadFunction) {
8083
return newDataLoader(batchLoadFunction, null);
8184
}
@@ -89,9 +92,12 @@ public static <K, V> DataLoader<K, V> newDataLoader(BatchLoader<K, V> batchLoadF
8992
* @param <V> the value type
9093
*
9194
* @return a new DataLoader
95+
*
96+
* @deprecated use {@link DataLoaderFactory} instead
9297
*/
98+
@Deprecated
9399
public static <K, V> DataLoader<K, V> newDataLoader(BatchLoader<K, V> batchLoadFunction, DataLoaderOptions options) {
94-
return new DataLoader<>(batchLoadFunction, options);
100+
return DataLoaderFactory.mkDataLoader(batchLoadFunction, options);
95101
}
96102

97103
/**
@@ -110,7 +116,10 @@ public static <K, V> DataLoader<K, V> newDataLoader(BatchLoader<K, V> batchLoadF
110116
* @param <V> the value type
111117
*
112118
* @return a new DataLoader
119+
*
120+
* @deprecated use {@link DataLoaderFactory} instead
113121
*/
122+
@Deprecated
114123
public static <K, V> DataLoader<K, V> newDataLoaderWithTry(BatchLoader<K, Try<V>> batchLoadFunction) {
115124
return newDataLoaderWithTry(batchLoadFunction, null);
116125
}
@@ -127,11 +136,12 @@ public static <K, V> DataLoader<K, V> newDataLoaderWithTry(BatchLoader<K, Try<V>
127136
*
128137
* @return a new DataLoader
129138
*
130-
* @see #newDataLoaderWithTry(BatchLoader)
139+
* @see DataLoaderFactory#newDataLoaderWithTry(BatchLoader)
140+
* @deprecated use {@link DataLoaderFactory} instead
131141
*/
132-
@SuppressWarnings("unchecked")
142+
@Deprecated
133143
public static <K, V> DataLoader<K, V> newDataLoaderWithTry(BatchLoader<K, Try<V>> batchLoadFunction, DataLoaderOptions options) {
134-
return new DataLoader<>((BatchLoader<K, V>) batchLoadFunction, options);
144+
return DataLoaderFactory.mkDataLoader(batchLoadFunction, options);
135145
}
136146

137147
/**
@@ -143,7 +153,10 @@ public static <K, V> DataLoader<K, V> newDataLoaderWithTry(BatchLoader<K, Try<V>
143153
* @param <V> the value type
144154
*
145155
* @return a new DataLoader
156+
*
157+
* @deprecated use {@link DataLoaderFactory} instead
146158
*/
159+
@Deprecated
147160
public static <K, V> DataLoader<K, V> newDataLoader(BatchLoaderWithContext<K, V> batchLoadFunction) {
148161
return newDataLoader(batchLoadFunction, null);
149162
}
@@ -157,9 +170,12 @@ public static <K, V> DataLoader<K, V> newDataLoader(BatchLoaderWithContext<K, V>
157170
* @param <V> the value type
158171
*
159172
* @return a new DataLoader
173+
*
174+
* @deprecated use {@link DataLoaderFactory} instead
160175
*/
176+
@Deprecated
161177
public static <K, V> DataLoader<K, V> newDataLoader(BatchLoaderWithContext<K, V> batchLoadFunction, DataLoaderOptions options) {
162-
return new DataLoader<>(batchLoadFunction, options);
178+
return DataLoaderFactory.mkDataLoader(batchLoadFunction, options);
163179
}
164180

165181
/**
@@ -178,7 +194,10 @@ public static <K, V> DataLoader<K, V> newDataLoader(BatchLoaderWithContext<K, V>
178194
* @param <V> the value type
179195
*
180196
* @return a new DataLoader
197+
*
198+
* @deprecated use {@link DataLoaderFactory} instead
181199
*/
200+
@Deprecated
182201
public static <K, V> DataLoader<K, V> newDataLoaderWithTry(BatchLoaderWithContext<K, Try<V>> batchLoadFunction) {
183202
return newDataLoaderWithTry(batchLoadFunction, null);
184203
}
@@ -195,10 +214,12 @@ public static <K, V> DataLoader<K, V> newDataLoaderWithTry(BatchLoaderWithContex
195214
*
196215
* @return a new DataLoader
197216
*
198-
* @see #newDataLoaderWithTry(BatchLoader)
217+
* @see DataLoaderFactory#newDataLoaderWithTry(BatchLoader)
218+
* @deprecated use {@link DataLoaderFactory} instead
199219
*/
220+
@Deprecated
200221
public static <K, V> DataLoader<K, V> newDataLoaderWithTry(BatchLoaderWithContext<K, Try<V>> batchLoadFunction, DataLoaderOptions options) {
201-
return new DataLoader<>(batchLoadFunction, options);
222+
return DataLoaderFactory.mkDataLoader(batchLoadFunction, options);
202223
}
203224

204225
/**
@@ -210,7 +231,10 @@ public static <K, V> DataLoader<K, V> newDataLoaderWithTry(BatchLoaderWithContex
210231
* @param <V> the value type
211232
*
212233
* @return a new DataLoader
234+
*
235+
* @deprecated use {@link DataLoaderFactory} instead
213236
*/
237+
@Deprecated
214238
public static <K, V> DataLoader<K, V> newMappedDataLoader(MappedBatchLoader<K, V> batchLoadFunction) {
215239
return newMappedDataLoader(batchLoadFunction, null);
216240
}
@@ -224,9 +248,12 @@ public static <K, V> DataLoader<K, V> newMappedDataLoader(MappedBatchLoader<K, V
224248
* @param <V> the value type
225249
*
226250
* @return a new DataLoader
251+
*
252+
* @deprecated use {@link DataLoaderFactory} instead
227253
*/
254+
@Deprecated
228255
public static <K, V> DataLoader<K, V> newMappedDataLoader(MappedBatchLoader<K, V> batchLoadFunction, DataLoaderOptions options) {
229-
return new DataLoader<>(batchLoadFunction, options);
256+
return DataLoaderFactory.mkDataLoader(batchLoadFunction, options);
230257
}
231258

232259
/**
@@ -246,7 +273,10 @@ public static <K, V> DataLoader<K, V> newMappedDataLoader(MappedBatchLoader<K, V
246273
* @param <V> the value type
247274
*
248275
* @return a new DataLoader
276+
*
277+
* @deprecated use {@link DataLoaderFactory} instead
249278
*/
279+
@Deprecated
250280
public static <K, V> DataLoader<K, V> newMappedDataLoaderWithTry(MappedBatchLoader<K, Try<V>> batchLoadFunction) {
251281
return newMappedDataLoaderWithTry(batchLoadFunction, null);
252282
}
@@ -263,10 +293,12 @@ public static <K, V> DataLoader<K, V> newMappedDataLoaderWithTry(MappedBatchLoad
263293
*
264294
* @return a new DataLoader
265295
*
266-
* @see #newDataLoaderWithTry(BatchLoader)
296+
* @see DataLoaderFactory#newDataLoaderWithTry(BatchLoader)
297+
* @deprecated use {@link DataLoaderFactory} instead
267298
*/
299+
@Deprecated
268300
public static <K, V> DataLoader<K, V> newMappedDataLoaderWithTry(MappedBatchLoader<K, Try<V>> batchLoadFunction, DataLoaderOptions options) {
269-
return new DataLoader<>(batchLoadFunction, options);
301+
return DataLoaderFactory.mkDataLoader(batchLoadFunction, options);
270302
}
271303

272304
/**
@@ -278,7 +310,10 @@ public static <K, V> DataLoader<K, V> newMappedDataLoaderWithTry(MappedBatchLoad
278310
* @param <V> the value type
279311
*
280312
* @return a new DataLoader
313+
*
314+
* @deprecated use {@link DataLoaderFactory} instead
281315
*/
316+
@Deprecated
282317
public static <K, V> DataLoader<K, V> newMappedDataLoader(MappedBatchLoaderWithContext<K, V> batchLoadFunction) {
283318
return newMappedDataLoader(batchLoadFunction, null);
284319
}
@@ -292,9 +327,12 @@ public static <K, V> DataLoader<K, V> newMappedDataLoader(MappedBatchLoaderWithC
292327
* @param <V> the value type
293328
*
294329
* @return a new DataLoader
330+
*
331+
* @deprecated use {@link DataLoaderFactory} instead
295332
*/
333+
@Deprecated
296334
public static <K, V> DataLoader<K, V> newMappedDataLoader(MappedBatchLoaderWithContext<K, V> batchLoadFunction, DataLoaderOptions options) {
297-
return new DataLoader<>(batchLoadFunction, options);
335+
return DataLoaderFactory.mkDataLoader(batchLoadFunction, options);
298336
}
299337

300338
/**
@@ -313,7 +351,10 @@ public static <K, V> DataLoader<K, V> newMappedDataLoader(MappedBatchLoaderWithC
313351
* @param <V> the value type
314352
*
315353
* @return a new DataLoader
354+
*
355+
* @deprecated use {@link DataLoaderFactory} instead
316356
*/
357+
@Deprecated
317358
public static <K, V> DataLoader<K, V> newMappedDataLoaderWithTry(MappedBatchLoaderWithContext<K, Try<V>> batchLoadFunction) {
318359
return newMappedDataLoaderWithTry(batchLoadFunction, null);
319360
}
@@ -330,32 +371,40 @@ public static <K, V> DataLoader<K, V> newMappedDataLoaderWithTry(MappedBatchLoad
330371
*
331372
* @return a new DataLoader
332373
*
333-
* @see #newDataLoaderWithTry(BatchLoader)
374+
* @see DataLoaderFactory#newDataLoaderWithTry(BatchLoader)
375+
* @deprecated use {@link DataLoaderFactory} instead
334376
*/
377+
@Deprecated
335378
public static <K, V> DataLoader<K, V> newMappedDataLoaderWithTry(MappedBatchLoaderWithContext<K, Try<V>> batchLoadFunction, DataLoaderOptions options) {
336-
return new DataLoader<>(batchLoadFunction, options);
379+
return DataLoaderFactory.mkDataLoader(batchLoadFunction, options);
337380
}
338381

339382
/**
340383
* Creates a new data loader with the provided batch load function, and default options.
341384
*
342385
* @param batchLoadFunction the batch load function to use
386+
*
387+
* @deprecated use {@link DataLoaderFactory} instead
343388
*/
389+
@Deprecated
344390
public DataLoader(BatchLoader<K, V> batchLoadFunction) {
345-
this(batchLoadFunction, null);
391+
this((Object) batchLoadFunction, null);
346392
}
347393

348394
/**
349395
* Creates a new data loader with the provided batch load function and options.
350396
*
351397
* @param batchLoadFunction the batch load function to use
352398
* @param options the batch load options
399+
*
400+
* @deprecated use {@link DataLoaderFactory} instead
353401
*/
402+
@Deprecated
354403
public DataLoader(BatchLoader<K, V> batchLoadFunction, DataLoaderOptions options) {
355404
this((Object) batchLoadFunction, options);
356405
}
357406

358-
private DataLoader(Object batchLoadFunction, DataLoaderOptions options) {
407+
DataLoader(Object batchLoadFunction, DataLoaderOptions options) {
359408
DataLoaderOptions loaderOptions = options == null ? new DataLoaderOptions() : options;
360409
this.futureCache = determineCacheMap(loaderOptions);
361410
// order of keys matter in data loader

0 commit comments

Comments
 (0)