Skip to content

Commit

Permalink
Merge 3056ca4 into 4b7596a
Browse files Browse the repository at this point in the history
  • Loading branch information
storuky committed Mar 31, 2020
2 parents 4b7596a + 3056ca4 commit 927edf7
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -319,15 +319,15 @@ That's it.
For batches where there is no item in response to a call, we normally return `nil`. However, you can use `:default_value` to return something else instead:

```ruby
BatchLoader.for(post.user_id).batch(default_value: NullUser.new) do |user_ids, loader|
BatchLoader::GraphQL.for(post.user_id).batch(default_value: NullUser.new) do |user_ids, loader|
User.where(id: user_ids).each { |user| loader.call(user.id, user) }
end
```

For batches where the value is some kind of collection, such as an Array or Hash, `loader` also supports being called with a block, which yields the _current_ value, and returns the _next_ value. This is extremely useful for 1:Many (`has_many`) relationships:

```ruby
BatchLoader.for(user.id).batch(default_value: []) do |user_ids, loader|
BatchLoader::GraphQL.for(user.id).batch(default_value: []) do |user_ids, loader|
Comment.where(user_id: user_ids).each do |comment|
loader.call(comment.user_id) { |memo| memo << comment }
end
Expand Down

0 comments on commit 927edf7

Please sign in to comment.