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.
-
#scope ⇒ Object
Returns the value of attribute scope.
Instance Method Summary collapse
- #cache_key(record) ⇒ Object
-
#initialize(model, association, _scope_sql) ⇒ Loader
constructor
A new instance of Loader.
- #load(record) ⇒ Object
- #perform(records) ⇒ Object
Constructor Details
#initialize(model, association, _scope_sql) ⇒ Loader
Returns a new instance of Loader.
12 13 14 15 16 17 |
# File 'lib/graphql/preload/loader.rb', line 12 def initialize(model, association, _scope_sql) @association = association @model = model validate_association end |
Instance Attribute Details
#association ⇒ Object (readonly)
Returns the value of attribute association.
6 7 8 |
# File 'lib/graphql/preload/loader.rb', line 6 def association @association end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
6 7 8 |
# File 'lib/graphql/preload/loader.rb', line 6 def model @model end |
#scope ⇒ Object
Returns the value of attribute scope.
5 6 7 |
# File 'lib/graphql/preload/loader.rb', line 5 def scope @scope end |
Instance Method Details
#cache_key(record) ⇒ Object
8 9 10 |
# File 'lib/graphql/preload/loader.rb', line 8 def cache_key(record) record.object_id end |
#load(record) ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/graphql/preload/loader.rb', line 19 def load(record) unless record.is_a?(model) raise TypeError, "Loader for #{model} can't load associations for #{record.class} objects" end return Promise.resolve(record) if association_loaded?(record) super end |
#perform(records) ⇒ Object
28 29 30 31 |
# File 'lib/graphql/preload/loader.rb', line 28 def perform(records) preload_association(records) records.each { |record| fulfill(record, record) } end |