Skip to content

Commit

Permalink
[temp][fix] Naive improvement for fetching many-to-one relations
Browse files Browse the repository at this point in the history
  • Loading branch information
wmaciejak committed Oct 20, 2017
1 parent 87992bc commit 0b2eff5
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ This is a sample app which shows how Rails 5, ROM 4.0, GraphQL and Clean Archite
- [x] modularization of fields in QueryType [app/graphql/query_type.rb](https://github.com/wmaciejak/rails_rom_graphql_clean_architecture_boilerplate/blob/master/app/graphql/query_type.rb#L6)
- [x] Implementation of Clean Architecture
- [x] Separation of seeds by the environment [db/seeds.rb](https://github.com/wmaciejak/rails_rom_graphql_clean_architecture_boilerplate/blob/master/db/seeds.rb)
- [x] Loading relations one-to-many without N+1, fixed by naive implementation - [BatchLoader issue#5](https://github.com/exAspArk/batch-loader/issues/5)

## What's to be done

Expand All @@ -32,7 +33,6 @@ This is a sample app which shows how Rails 5, ROM 4.0, GraphQL and Clean Archite

## Known issues

- [ ] [BatchLoader issue#5](https://github.com/exAspArk/batch-loader/issues/5) - Partly fixed by custom solution but it's still `in progress`
- [ ] camelCase in queries(thx [RadoMark](https://github.com/RadoMark/))

## Links
Expand Down
7 changes: 3 additions & 4 deletions app/graphql/types/post_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
field :title, !types.String
field :created_at, !types.String
field :updated_at, !types.String
field :comments, CommentType, resolve: ->(post, _, _) do
field :comments, types[CommentType], resolve: ->(post, _, _) do
BatchLoader.for(post.id).batch do |post_ids, loader|
CommentRepo.new(ROM.env).all_for_posts(post_ids).each do |comment|
loader.call(comment.post_id, comment)
end
grouped_hash = CommentRepo.new(ROM.env).all_for_posts(post_ids).group_by(&:post_id)
post_ids.each { |post_id| loader.call(post_id, grouped_hash[post_id]) }
end
end
end
16 changes: 7 additions & 9 deletions app/graphql/types/user_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@
name "User"
field :email, types.String
field :username, types.String
field :posts, PostType, resolve: ->(user, _, _) do
field :posts, types[PostType], resolve: ->(user, _, _) do
BatchLoader.for(user.id).batch do |user_ids, loader|
PostRepo.new(ROM.env).all_for_user(user_ids).each do |post|
loader.call(post.user_id, post)
end
grouped_hash = PostRepo.new(ROM.env).all_for_user(user_ids).group_by(&:user_id)
user_ids.each { |user_id| loader.call(user_id, grouped_hash[user_id]) }
end
end
field :comments, CommentType, resolve: ->(user, _, _) do
BatchLoader.for(user.id).batch do |user_ids, loader|
CommentRepo.new(ROM.env).all_for_author(user_ids).each do |comment|
loader.call(comment.author_id, comment)
end
field :comments, types[CommentType], resolve: ->(user, _, _) do
BatchLoader.for(user.id).batch do |author_ids, loader|
grouped_hash = CommentRepo.new(ROM.env).all_for_author(author_ids).group_by(&:author_id)
author_ids.each { |author_id| loader.call(author_id, grouped_hash[author_id]) }
end
end
end

0 comments on commit 0b2eff5

Please sign in to comment.