Skip to content

Best Practice Using Batch loader on Graphql 1.8.11 #30

@im-not-a-robot

Description

@im-not-a-robot

My code for batch loader :

BatchLoader.for(object.id).batch(default_value: []) do |province_ids, loader|
    City.where(province_id: province_ids).each do |city|
         loader.call(city.province_id) { |data| data << city }
     end
end

Result :

Province Load (0.7ms)  SELECT "provinces".* FROM "provinces"
↳ app/controllers/graphql_controller.rb:11
City Load (0.5ms)  SELECT "cities".* FROM "cities" WHERE "cities"."province_id" = $1  [["province_id", 1]]
↳ app/graphql/types/province_type.rb:21
 City Load (1.1ms)  SELECT "cities".* FROM "cities" WHERE "cities"."province_id" = $1  [["province_id", 2]]
↳ app/graphql/types/province_type.rb:21
City Load (0.5ms)  SELECT "cities".* FROM "cities" WHERE "cities"."province_id" = $1  [["province_id", 3]]

Here's my gemfile :

source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.4.5'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.2.1'
# Use sqlite3 as the database for Active Record
# gem 'sqlite3'
# Use postgresql as the database for Active Record
gem 'pg'
# Use Puma as the app server
gem 'puma', '~> 3.11'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
# gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use ActiveStorage variant
# gem 'mini_magick', '~> 4.8'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.1.0', require: false

# Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible
# gem 'rack-cors'

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end

group :development do
  gem 'listen', '>= 3.0.5', '< 3.2'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end


# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
gem 'graphql'

gem 'batch-loader'

Im already use your quick fix on this comment
#22 (comment)
It can solved this problem.

The other solution :
rmosolgo/graphql-ruby#1778 (comment)
But it must make scope attribute to false

Is there best practice to solve this problem ?
Thank you

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions