Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Active Record AssociationLoader and RecordLoader examples (#64)
- Loading branch information
1 parent
ae230d5
commit 058213b
Showing
3 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
class AssociationLoader < GraphQL::Batch::Loader | ||
def self.validate(model, association_name) | ||
new(model, association_name) | ||
nil | ||
end | ||
|
||
def initialize(model, association_name) | ||
@model = model | ||
@association_name = association_name | ||
validate | ||
end | ||
|
||
def load(record) | ||
raise TypeError, "#{@model} loader can't load association for #{record.class}" unless record.is_a?(@model) | ||
return Promise.resolve(read_association(record)) if association_loaded?(record) | ||
super | ||
end | ||
|
||
# We want to load the associations on all records, even if they have the same id | ||
def cache_key(record) | ||
record.object_id | ||
end | ||
|
||
def perform(records) | ||
preload_association(records) | ||
records.each { |record| fulfill(record, read_association(record)) } | ||
end | ||
|
||
private | ||
|
||
def validate | ||
unless @model.reflect_on_association(@association_name) | ||
raise ArgumentError, "No association #{@association_name} on #{@model}" | ||
end | ||
end | ||
|
||
def preload_association(records) | ||
::ActiveRecord::Associations::Preloader.new.preload(records, @association_name) | ||
end | ||
|
||
def read_association(record) | ||
record.public_send(@association_name) | ||
end | ||
|
||
def association_loaded?(record) | ||
record.association(@association_name).loaded? | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
class RecordLoader < GraphQL::Batch::Loader | ||
def initialize(model, column: model.primary_key, where: nil) | ||
@model = model | ||
@column = column.to_s | ||
@column_type = model.type_for_attribute(@column) | ||
@where = where | ||
end | ||
|
||
def load(key) | ||
super(@column_type.cast(key)) | ||
end | ||
|
||
def perform(keys) | ||
query(keys).each do |record| | ||
value = @column_type.cast(record.public_send(@column)) | ||
fulfill(value, record) | ||
end | ||
keys.each { |key| fulfill(key, nil) unless fulfilled?(key) } | ||
end | ||
|
||
private | ||
|
||
def query(keys) | ||
scope = @model | ||
scope = scope.where(@where) if @where | ||
scope.where(@column => keys) | ||
end | ||
end |