Class: GraphitiGql::Loaders::Many

Inherits:
GraphQL::Batch::Loader
  • Object
show all
Defined in:
lib/graphiti_gql/loaders/many.rb

Direct Known Subclasses

HasMany, HasOne, ManyToMany, PolymorphicHasMany

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sideload, params) ⇒ Many

Returns a new instance of Many.



16
17
18
19
# File 'lib/graphiti_gql/loaders/many.rb', line 16

def initialize(sideload, params)
  @sideload = sideload
  @params = params.merge(typecast_filters: false)
end

Class Method Details

.factory(sideload, params) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/graphiti_gql/loaders/many.rb', line 4

def self.factory(sideload, params)
  if sideload.polymorphic_as
    PolymorphicHasMany.for(sideload, params)
  elsif sideload.type == :many_to_many
    ManyToMany.for(sideload, params)
  elsif sideload.type == :has_one
    HasOne.for(sideload, params)
  else
    HasMany.for(sideload, params)
  end
end

Instance Method Details

#assign(ids, proxy) ⇒ Object



45
46
47
# File 'lib/graphiti_gql/loaders/many.rb', line 45

def assign(ids, proxy)
  raise "implement in subclass"
end

#perform(parent_records) ⇒ Object

Raises:

  • (::Graphiti::Errors::UnsupportedPagination)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/graphiti_gql/loaders/many.rb', line 21

def perform(parent_records)
  Graphiti.broadcast("association", { sideload: @sideload })
  raise ::Graphiti::Errors::UnsupportedPagination if paginating? && parent_records.length > 1
  raise Errors::UnsupportedStats if requesting_stats? && parent_records.length > 1 && !can_group?

  ids = parent_records.map do |pr|
    pk = pr.send(@sideload.primary_key)
    if @sideload.polymorphic_as
      hash = {}
      hash[@sideload.foreign_key] = pk
      hash[:"#{@sideload.polymorphic_as}_type"] = pr.class.name
      hash
    else
      pk
    end
  end
  ids.compact!

  build_params(ids, parent_records)
  resource = Schema.registry.get(@sideload.resource.class)[:resource]
  proxy = resource.all(@params)
  assign(parent_records, proxy)
end