{"action":"closed","issue":{"url":"https://api.github.com/repos/graphql-java/java-dataloader/issues/18","repository_url":"https://api.github.com/repos/graphql-java/java-dataloader","labels_url":"https://api.github.com/repos/graphql-java/java-dataloader/issues/18/labels{/name}","comments_url":"https://api.github.com/repos/graphql-java/java-dataloader/issues/18/comments","events_url":"https://api.github.com/repos/graphql-java/java-dataloader/issues/18/events","html_url":"https://github.com/graphql-java/java-dataloader/issues/18","id":302874285,"number":18,"title":"Provide a way to have batch loaders that return Map","user":{"login":"ceronman","id":309508,"avatar_url":"https://avatars1.githubusercontent.com/u/309508?v=4","gravatar_id":"","url":"https://api.github.com/users/ceronman","html_url":"https://github.com/ceronman","followers_url":"https://api.github.com/users/ceronman/followers","following_url":"https://api.github.com/users/ceronman/following{/other_user}","gists_url":"https://api.github.com/users/ceronman/gists{/gist_id}","starred_url":"https://api.github.com/users/ceronman/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ceronman/subscriptions","organizations_url":"https://api.github.com/users/ceronman/orgs","repos_url":"https://api.github.com/users/ceronman/repos","events_url":"https://api.github.com/users/ceronman/events{/privacy}","received_events_url":"https://api.github.com/users/ceronman/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2018-03-06T21:31:44Z","updated_at":"2018-03-15T06:40:32Z","closed_at":"2018-03-15T06:40:32Z","author_association":"NONE","body":"Hello,\r\n\r\nI have been using java-dataloader for some time now and I found it very useful. Here is one small feedback:\r\n\r\nI find the current `BatchLoader` functional interface is often inconvenient:\r\n\r\n```java\r\nCompletionStage<List<V>> load(List<K> keys)\r\n```\r\nThe 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:\r\n\r\n```sql\r\nSELECT * FROM User WHERE id IN (keys)\r\n```\r\nThis 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:\r\n\r\n```java\r\nList<User> users = database.query(\"SELECT * FROM User WHERE id IN keys\");\r\nMap<Integer, User> userByKey = users.stream().collect(toMap(User::getId, Function.identity()));\r\nList<User> result = keys.stream().map(userByKey::get).collect(toList());\r\nreturn result;\r\n```\r\n\r\nI think all this bolier plate code can be reduced if an alternative `BatchLoader` is provided that returns a `Map` instead:\r\n\r\n```java\r\nCompletionStage<Map<K, V>> load(List<K> keys)\r\n```\r\nWhat do you think?\r\n"}}
{"actor":{"display_login":"bbakerman"}}