Class: HQ::GraphQL::RecordLoader

Inherits:
GraphQL::Batch::Loader
  • Object
show all
Defined in:
lib/hq/graphql/record_loader.rb

Instance Method Summary collapse

Constructor Details

#initialize(model, column: model.primary_key, where: nil) ⇒ RecordLoader



6
7
8
9
10
11
12
# File 'lib/hq/graphql/record_loader.rb', line 6

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

Instance Method Details

#load(key) ⇒ Object



14
15
16
# File 'lib/hq/graphql/record_loader.rb', line 14

def load(key)
  super(@column_type.cast(key))
end

#perform(keys) ⇒ Object



18
19
20
21
# File 'lib/hq/graphql/record_loader.rb', line 18

def perform(keys)
  query(keys).each { |record| fulfill(record.public_send(@column), record) }
  keys.each { |key| fulfill(key, nil) unless fulfilled?(key) }
end