Sha256: 1097776f0327744cbfb024924b34e8e84ca7d293e28ca2f1e55f5cf0bd974b2a

Contents?: true

Size: 592 Bytes

Versions: 9

Compression:

Stored size: 592 Bytes

Contents

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 { |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

9 entries across 9 versions & 2 rubygems

Version Path
graphql-batch-0.4.3 examples/record_loader.rb
graphql-batch-0.4.2 examples/record_loader.rb
graphql-batch-0.4.1 examples/record_loader.rb
graphql-batch-edge-0.4.0 examples/record_loader.rb
graphql-batch-0.4.0 examples/record_loader.rb
graphql-batch-0.3.10 examples/record_loader.rb
graphql-batch-0.3.9 examples/record_loader.rb
graphql-batch-0.3.8 examples/record_loader.rb
graphql-batch-0.3.7 examples/record_loader.rb