Class: GraphQL::Extras::PreloadSource

Inherits:
Dataloader::Source
  • Object
show all
Defined in:
lib/graphql/extras/preload.rb

Instance Method Summary collapse

Constructor Details

#initialize(preload) ⇒ PreloadSource

Returns a new instance of PreloadSource.



4
5
6
# File 'lib/graphql/extras/preload.rb', line 4

def initialize(preload)
  @preload = preload
end

Instance Method Details

#fetch(records) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/graphql/extras/preload.rb', line 8

def fetch(records)
  if ActiveRecord::VERSION::MAJOR >= 7
    preloader = ActiveRecord::Associations::Preloader.new(
      records: records,
      associations: @preload
    )
    preloader.call
  else
    preloader = ActiveRecord::Associations::Preloader.new
    preloader.preload(records, @preload)
  end

  records
end