Class: GraphQL::Sources::ActiveRecordBase

Inherits:
Dataloader::Source
  • Object
show all
Defined in:
lib/graphql/sources/active_record_base.rb

Overview

An abstract class for interacting with active record.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, key: :id) ⇒ ActiveRecordBase

Returns a new instance of ActiveRecordBase.

Parameters:

  • model (Class)

    a child class of ActiveRecord::Base (e.g. Comment)

  • key (Symbol) (defaults to: :id)

    an attribute (typically a foreign key) to use for loading (e.g. :user_id)



18
19
20
21
22
# File 'lib/graphql/sources/active_record_base.rb', line 18

def initialize(model, key: :id)
  super()
  @model = model
  @key = key
end

Class Method Details

.batch_key_for(model, key: :id) ⇒ Array

Returns a key.

Parameters:

  • model (Class)

    a child class of ActiveRecord::Base (e.g. Comment)

  • key (Symbol) (defaults to: :id)

    an attribute (typically a foreign key) to use for loading (e.g. :user_id)

Returns:

  • (Array)

    a key



10
11
12
13
14
# File 'lib/graphql/sources/active_record_base.rb', line 10

def self.batch_key_for(model, key: :id)
  return [model.to_sql, key] if model.is_a?(ActiveRecord::Relation)

  [model, key]
end