Class: GraphQL::Preload::Loader
- Inherits:
-
Batch::Loader
- Object
- Batch::Loader
- GraphQL::Preload::Loader
- Defined in:
- lib/graphql/preload/loader.rb
Overview
Preloads ActiveRecord::Associations when called from the Preload::Instrument
Instance Attribute Summary collapse
-
#association ⇒ Object
readonly
Returns the value of attribute association.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
Instance Method Summary collapse
-
#initialize(model, association) ⇒ Loader
constructor
A new instance of Loader.
- #load(record) ⇒ Object
- #perform(records) ⇒ Object
Constructor Details
#initialize(model, association) ⇒ Loader
Returns a new instance of Loader.
7 8 9 10 11 12 |
# File 'lib/graphql/preload/loader.rb', line 7 def initialize(model, association) @association = association @model = model validate_association end |
Instance Attribute Details
#association ⇒ Object (readonly)
Returns the value of attribute association.
5 6 7 |
# File 'lib/graphql/preload/loader.rb', line 5 def association @association end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
5 6 7 |
# File 'lib/graphql/preload/loader.rb', line 5 def model @model end |
Instance Method Details
#load(record) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/graphql/preload/loader.rb', line 14 def load(record) unless record.is_a?(model) raise TypeError, "loader for #{model.name} can't load associations for #{record.class.name} objects" end if record.association(association).loaded? Promise.resolve(record) else super end end |
#perform(records) ⇒ Object
26 27 28 29 30 31 32 33 34 |
# File 'lib/graphql/preload/loader.rb', line 26 def perform(records) if ActiveRecord::VERSION::MAJOR > 3 ActiveRecord::Associations::Preloader.new.preload(records, association) else ActiveRecord::Associations::Preloader.new(records, association).run end records.each { |record| fulfill(record, record) } end |