In the previous post about solving N+1 query in graphQL using graphql-batch, we discussed batching queries using the graphql-batch gem. This works great for belongs_to relationships, but it is not useful for preloading has_many associations. For example, if you wanted the comments for each article, it would again produce N+1 queries. Luckily I found a gem graphql-preload that solves this problem. The graphql-preload gem is created based on a gist by @theorygeek. This gem depends on the graphql-batch gem. so we need to keep graphql-batch settings in the schema for graphql-preload work. detailed explanation about graphql batch is given here.
Installation
Add this line to your application’s Gemfile:
Gemfile
1
gem 'graphql-preload'
Add enable_preloading to the schema file to be able to use this functionality.