question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Provide a way to have batch loaders that return Map

See original GitHub issue

Hello,

I have been using java-dataloader for some time now and I found it very useful. Here is one small feedback:

I find the current BatchLoader functional interface is often inconvenient:

CompletionStage<List<V>> load(List<K> keys)

The input is a list of keys and the output is a list that has to match 1 to 1 with the list of keys. In real world data fetching scenarios, it’s rare to do a query that returns 1 to 1 matches. For example, let’s assume that I want to load Users from a database, I could probably use a query that looks like this:

SELECT * FROM User WHERE id IN (keys)

This kind of queries are very common when using batch loaders, however, this won’t return a 1 to 1 match. If one of the users does not exist, the result will not contain it. This means that in order to use this query in a BatchLoader, I have to first create a map, and then return a List of that crossed with the original keys. For example:

List<User> users = database.query("SELECT * FROM User WHERE id IN keys");
Map<Integer, User> userByKey = users.stream().collect(toMap(User::getId, Function.identity()));
List<User> result = keys.stream().map(userByKey::get).collect(toList());
return result;

I think all this bolier plate code can be reduced if an alternative BatchLoader is provided that returns a Map instead:

CompletionStage<Map<K, V>> load(List<K> keys)

What do you think?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
bbakermancommented, Aug 18, 2018

in addition to the exact length of list of values , it should also be in exact encounter order? Is that true?

yes that is true. The reason is 2 fold, 1) it needs a way to go from completed value back to completed key and 2) it was how Facebook write it originally and I kept those semantics.

Remember is needs a way to go from a value CompletableFuture finishing back to a key. Off hand I think we can do that with a Map but honestly it was more driven by being similar to the original JavaScript implementation.

0reactions
bbakermancommented, Aug 28, 2018

This has been added in 2.1.1

Read more comments on GitHub >

github_iconTop Results From Across the Web

MappedBatchLoader (java-dataloader 2.2.1 API) - javadoc.io
This form is useful when you don't have a 1:1 mapping of keys to values ... Called to batch load the provided keys...
Read more >
How do I pass custom options into the batch function ... - GitHub
I have a circumstance where we want to return a modified result based on a graphQL variable. It appears that the loaders only...
Read more >
Batching - GraphQL Java
The data loader library supports two types of context being passed to the batch loader. The first is an overall context object per...
Read more >
How to use DataLoaders in GraphQL | by Mike Cronin - Medium
The batch function resolves to its values which are then stored with the corresponding keys.
Read more >
Data Loaders - Bartosz Sypytkowski
Async batching · (Optional) If we use caching, we can check if we didn't have value for provided key already. · Add requested...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found