Class: Graphiform::PreloaderSource

Inherits:
GraphQL::Dataloader::ActiveRecordAssociationSource
  • Object
show all
Defined in:
lib/graphiform/preloader_source.rb

Overview

Loads ActiveRecord associations through the dataloader, with optional Scopiform-style ‘where`/`sort`/`includes`/`scope` composition applied to the relation handed to ActiveRecord::Associations::Preloader.

Inherits batching semantics (including ‘scope.to_sql`-based batch keys), `assoc.loaded?` short-circuiting, polymorphic handling, and cross-source cache reuse from GraphQL::Dataloader::ActiveRecordAssociationSource.

Usage:

dataloader.with(
  Graphiform::PreloaderSource,
  :posts,
  klass: User.reflect_on_association(:posts).klass,
  scope: association_reflection.scope,
  where: args[:where],
  sort:  args[:sort],
).load(user_record)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(association_name, klass: nil, scope: nil, where: nil, sort: nil, includes: nil) ⇒ PreloaderSource

Returns a new instance of PreloaderSource.

Parameters:

  • association_name (Symbol)

    the association on the parent record(s)

  • klass (Class, nil) (defaults to: nil)

    target model class used to build the composed scope. Required when any of ‘scope`/`where`/`sort`/`includes` is given; may be nil for bare preloads.

  • scope (Proc, nil) (defaults to: nil)
  • where (Hash, nil) (defaults to: nil)
  • sort (Hash, nil) (defaults to: nil)
  • includes (Symbol, Array, Hash, nil) (defaults to: nil)


36
37
38
39
# File 'lib/graphiform/preloader_source.rb', line 36

def initialize(association_name, klass: nil, scope: nil, where: nil, sort: nil, includes: nil)
  effective_scope = build_scope(klass, scope: scope, where: where, sort: sort, includes: includes)
  super(association_name, effective_scope)
end

Class Method Details

.batch_key_for(association_name, klass: nil, scope: nil, where: nil, sort: nil, includes: nil) ⇒ Object

Mirror initialize’s scope composition so the batch key (which upstream derives from the scope via ‘scope.to_sql`) is consistent across loads that should batch together.



44
45
46
47
# File 'lib/graphiform/preloader_source.rb', line 44

def self.batch_key_for(association_name, klass: nil, scope: nil, where: nil, sort: nil, includes: nil)
  effective_scope = build_scope(klass, scope: scope, where: where, sort: sort, includes: includes)
  super(association_name, effective_scope)
end

.build_scope(klass, scope: nil, where: nil, sort: nil, includes: nil) ⇒ Object



49
50
51
52
53
54
# File 'lib/graphiform/preloader_source.rb', line 49

def self.build_scope(klass, scope: nil, where: nil, sort: nil, includes: nil)
  return nil unless klass
  return nil unless ScopeComposer.customized?(scope: scope, where: where, sort: sort, includes: includes)

  ScopeComposer.compose(klass.all, scope: scope, where: where, sort: sort, includes: includes)
end

Instance Method Details

#build_scope(klass, **kwargs) ⇒ Object

Instance-level helper kept for symmetry / overrideability.



57
58
59
# File 'lib/graphiform/preloader_source.rb', line 57

def build_scope(klass, **kwargs)
  self.class.build_scope(klass, **kwargs)
end