Sha256: 6af160af3b33cfb6c23d3f07069a61fe0af6c2e2f56b8088a841172dd46d33eb

Contents?: true

Size: 604 Bytes

Versions: 6

Compression:

Stored size: 604 Bytes

Contents

class RecordLoader < GraphQL::Batch::Loader
  def initialize(model, column: model.primary_key, where: nil)
    super()
    @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 { |record| fulfill(record.public_send(@column), record) }
    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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
graphql-batch-0.6.0 examples/record_loader.rb
graphql-batch-0.5.4 examples/record_loader.rb
graphql-batch-0.5.3 examples/record_loader.rb
graphql-batch-0.5.2 examples/record_loader.rb
graphql-batch-0.5.1 examples/record_loader.rb
graphql-batch-0.5.0 examples/record_loader.rb