Class: SolidusGraphqlApi::BatchLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/solidus_graphql_api/batch_loader.rb,
lib/solidus_graphql_api/batch_loader/has_one.rb,
lib/solidus_graphql_api/batch_loader/has_many.rb,
lib/solidus_graphql_api/batch_loader/belongs_to.rb,
lib/solidus_graphql_api/batch_loader/has_many_through.rb

Overview

Provides an abstraction layer on top of BatchLoader::GraphQL that removes all the boilerplate normally needed to batch-load ActiveRecord associations.

Examples:

Batch-loading a user’s posts

BatchLoader.for(user, :posts)

Direct Known Subclasses

BelongsTo, HasMany, HasManyThrough, HasOne

Defined Under Namespace

Classes: BelongsTo, HasMany, HasManyThrough, HasOne

Constant Summary collapse

LOADER_CLASSES =
{
  has_one: BatchLoader::HasOne,
  has_many: BatchLoader::HasMany,
  has_many_through: BatchLoader::HasManyThrough,
  belongs_to: BatchLoader::BelongsTo,
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, reflection, options = {}) ⇒ BatchLoader

Generates a new instance of this batch loader for an ActiveRecord instance-association pair.

Parameters:

  • object (ActiveRecord::Base)

    the record whose association you want to batch-load

  • reflection (ActiveRecord::Reflection)

    the association to batch-load

  • options (Hash) (defaults to: {})

    an options hash

  • scope (Hash)

    a customizable set of options

  • klass (Hash)

    a customizable set of options



68
69
70
71
72
# File 'lib/solidus_graphql_api/batch_loader.rb', line 68

def initialize(object, reflection, options = {})
  @object = object
  @reflection = reflection
  @options = options
end

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



58
59
60
# File 'lib/solidus_graphql_api/batch_loader.rb', line 58

def object
  @object
end

#optionsObject (readonly)

Returns the value of attribute options.



58
59
60
# File 'lib/solidus_graphql_api/batch_loader.rb', line 58

def options
  @options
end

#reflectionObject (readonly)

Returns the value of attribute reflection.



58
59
60
# File 'lib/solidus_graphql_api/batch_loader.rb', line 58

def reflection
  @reflection
end

Class Method Details

.for(object, association, options = {}) ⇒ BatchLoader::GraphQL

Generates the batch loader for an ActiveRecord instance-association pair.

Parameters:

  • object (ActiveRecord::Base)

    the record whose association you want to batch-load

  • association (Symbol)

    the association to batch-load

  • options (Hash) (defaults to: {})

    an options hash

  • scope (Hash)

    a customizable set of options

Returns:

  • (BatchLoader::GraphQL)

See Also:

  • #new


29
30
31
32
# File 'lib/solidus_graphql_api/batch_loader.rb', line 29

def for(object, association, options = {})
  reflection = object.class.reflect_on_association(association)
  loader_class_for(reflection).new(object, reflection, options).load
end

Instance Method Details

#loadBatchLoader::GraphQL

Returns the batch loading logic.

Returns:

  • (BatchLoader::GraphQL)

Raises:

  • (NotImplementedError)


77
78
79
# File 'lib/solidus_graphql_api/batch_loader.rb', line 77

def load
  raise NotImplementedError
end