Class: Graphiti::Sideload

Inherits:
Object show all
Defined in:
lib/graphiti/sideload.rb

Direct Known Subclasses

BelongsTo, HasMany

Defined Under Namespace

Classes: BelongsTo, HasMany, HasOne, ManyToMany, PolymorphicBelongsTo

Constant Summary collapse

HOOK_ACTIONS =
[:save, :create, :update, :destroy, :disassociate]
TYPES =
[:has_many, :belongs_to, :has_one, :many_to_many]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, opts) ⇒ Sideload

Returns a new instance of Sideload.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/graphiti/sideload.rb', line 20

def initialize(name, opts)
  @name = name
  validate_options!(opts)
  translate_deprecated_options!(opts)
  @parent_resource_class = opts[:parent_resource]
  @resource_class_name = opts[:resource]
  @primary_key = opts[:primary_key]
  @foreign_key = opts[:foreign_key]
  @type = opts[:type]
  @base_scope = opts[:base_scope]
  @readable = opts[:readable]
  @writable = opts[:writable]
  @as = opts[:as]
  @link = opts[:link]
  unless @link.nil? || Resource::LINK_MODES.include?(@link)
    raise Errors::InvalidLinkRendering.new(@parent_resource_class, :"#{name} link", @link)
  end
  @single = opts[:single]
  @remote = opts[:remote]
  apply_belongs_to_many_filter if type == :many_to_many

  @description = opts[:description]

  # polymorphic has_many
  @polymorphic_as = opts[:polymorphic_as]
  # polymorphic_belongs_to-specific
  @group_name = opts[:group_name]
  @polymorphic_child = opts[:polymorphic_child]
  @parent = opts[:parent]
  @render_resource_ids = opts[:resource_ids]

  if polymorphic_child?
    parent.resource.polymorphic << resource_class
  end

  if remote?
    @resource_class = create_remote_resource
  end
end

Instance Attribute Details

#group_name ⇒ Object (readonly)

Returns the value of attribute group_name.



6
7
8
# File 'lib/graphiti/sideload.rb', line 6

def group_name
  @group_name
end

Returns the value of attribute link.



6
7
8
# File 'lib/graphiti/sideload.rb', line 6

def link
  @link
end

#name ⇒ Object (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/graphiti/sideload.rb', line 6

def name
  @name
end

#parent ⇒ Object (readonly)

Returns the value of attribute parent.



6
7
8
# File 'lib/graphiti/sideload.rb', line 6

def parent
  @parent
end

#parent_resource_class ⇒ Object (readonly)

Returns the value of attribute parent_resource_class.



6
7
8
# File 'lib/graphiti/sideload.rb', line 6

def parent_resource_class
  @parent_resource_class
end

#polymorphic_as ⇒ Object (readonly)

Returns the value of attribute polymorphic_as.



6
7
8
# File 'lib/graphiti/sideload.rb', line 6

def polymorphic_as
  @polymorphic_as
end

Class Method Details

.after_save(only: [], except: [], &blk) ⇒ Object



434
435
436
437
438
439
440
441
# File 'lib/graphiti/sideload.rb', line 434

def self.after_save(only: [], except: [], &blk)
  actions = HOOK_ACTIONS - except
  actions = only & actions
  actions = [:save] if only.empty? && except.empty?
  actions.each do |a|
    hooks[:"after_#{a}"] << blk
  end
end

.assign(&blk) ⇒ Object



86
87
88
# File 'lib/graphiti/sideload.rb', line 86

def self.assign(&blk)
  self.assign_proc = blk
end

.assign_each(&blk) ⇒ Object



90
91
92
# File 'lib/graphiti/sideload.rb', line 90

def self.assign_each(&blk)
  self.assign_each_proc = blk
end

.assigned?(query, record, association_name) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
84
# File 'lib/graphiti/sideload.rb', line 81

def self.assigned?(query, record, association_name)
  owners = query.association_owners[association_name]
  !owners.nil? && owners.key?(record.object_id)
end

.assigning_node(query) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/graphiti/sideload.rb', line 67

def self.assigning_node(query)
  return yield if query.nil?

  previous = Fiber[ASSIGNING_QUERY]
  Fiber[ASSIGNING_QUERY] = query
  yield
ensure
  Fiber[ASSIGNING_QUERY] = previous unless query.nil?
end

.current_assigning_query ⇒ Object



77
78
79
# File 'lib/graphiti/sideload.rb', line 77

def self.current_assigning_query
  Fiber[ASSIGNING_QUERY]
end

.hooks ⇒ Object



443
444
445
446
447
448
449
450
# File 'lib/graphiti/sideload.rb', line 443

def self.hooks
  @hooks ||= {}.tap do |h|
    HOOK_ACTIONS.each do |a|
      h[:"after_#{a}"] = []
      h[:"before_#{a}"] = []
    end
  end
end


102
103
104
# File 'lib/graphiti/sideload.rb', line 102

def self.link(&blk)
  self.link_proc = blk
end

.params(&blk) ⇒ Object



94
95
96
# File 'lib/graphiti/sideload.rb', line 94

def self.params(&blk)
  self.params_proc = blk
end

.pre_load(&blk) ⇒ Object



98
99
100
# File 'lib/graphiti/sideload.rb', line 98

def self.pre_load(&blk)
  self.pre_load_proc = blk
end

.scope(&blk) ⇒ Object



60
61
62
# File 'lib/graphiti/sideload.rb', line 60

def self.scope(&blk)
  self.scope_proc = blk
end

Instance Method Details

#assign(parents, children) ⇒ Object



374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# File 'lib/graphiti/sideload.rb', line 374

def assign(parents, children)
  track_associated = type == :has_one
  associated = [] if track_associated
  if performant_assign?
    map = child_map(children)
  end

  parents.each do |parent|
    relevant_children = if performant_assign?
      children_for(parent, map) || []
    else
      fire_assign_each(parent, children)
    end

    if relevant_children.is_a?(Array)
      associated |= relevant_children if track_associated
      associate_all(parent, relevant_children)
    else
      associated << relevant_children if track_associated && relevant_children
      associate(parent, relevant_children)
    end
  end
  children.replace(associated) if track_associated
end

#assign_each(parent, children) ⇒ Object



288
289
290
# File 'lib/graphiti/sideload.rb', line 288

def assign_each(parent, children)
  raise "Override #assign_each in subclass"
end

#associate(parent, child) ⇒ Object



467
468
469
470
471
# File 'lib/graphiti/sideload.rb', line 467

def associate(parent, child)
  return unless claim_association(parent)

  parent_resource.associate(parent, child, association_name, type)
end

#associate_all(parent, children) ⇒ Object



461
462
463
464
465
# File 'lib/graphiti/sideload.rb', line 461

def associate_all(parent, children)
  return unless claim_association(parent)

  parent_resource.associate_all(parent, children, association_name, type)
end

#association_name ⇒ Object



275
276
277
# File 'lib/graphiti/sideload.rb', line 275

def association_name
  @as || name
end

#base_scope ⇒ Object



305
306
307
308
309
310
311
# File 'lib/graphiti/sideload.rb', line 305

def base_scope
  if @base_scope
    @base_scope.respond_to?(:call) ? @base_scope.call : @base_scope
  else
    resource.base_scope
  end
end

#build_resource_proxy(parents, query, graph_parent) ⇒ Object



313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/graphiti/sideload.rb', line 313

def build_resource_proxy(parents, query, graph_parent)
  params = nil
  opts = nil
  proxy = nil

  with_error_handling Errors::SideloadParamsError do
    params = load_params(parents, query)
    params_proc&.call(params, parents, context)
    return [] if blank_query?(params)

    opts = load_options(parents, query)
    opts[:sideload] = self
    opts[:parent] = graph_parent
  end

  with_error_handling(Errors::SideloadQueryBuildingError) do
    scope = base_scope
    scope[:foreign_key] = foreign_key if remote?
    proxy = resource.class._all(params, opts, scope)
    pre_load_proc&.call(proxy, parents)
  end

  proxy
end

#clear_resources ⇒ Object



369
370
371
372
# File 'lib/graphiti/sideload.rb', line 369

def clear_resources
  @resource = nil
  @parent_resource = nil
end

#collect_foreign_keys(parents, query) ⇒ Object



225
226
# File 'lib/graphiti/sideload.rb', line 225

def collect_foreign_keys(parents, query)
end

#create_remote_resource ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/graphiti/sideload.rb', line 106

def create_remote_resource
  remote_url = @remote
  klass = Class.new(Graphiti::Resource) {
    self.adapter = Graphiti::Adapters::GraphitiAPI
    self.model = OpenStruct
    self.remote = remote_url
    self.validate_requests = false
    self.validate_links = false
  }
  name = "#{parent_resource_class.name}.#{@name}.remote"
  klass.class_eval("def self.name;'#{name}';end", __FILE__, __LINE__)
  klass
end

#customized_base_scope? ⇒ Boolean

Returns:

  • (Boolean)


161
162
163
# File 'lib/graphiti/sideload.rb', line 161

def customized_base_scope?
  !!@base_scope
end


212
213
214
215
# File 'lib/graphiti/sideload.rb', line 212

def default_link_mode
  default = @parent_resource_class.relationship_links
  (link_proc && default == false) ? true : default
end

#default_render_resource_ids? ⇒ Boolean

Returns:

  • (Boolean)


195
196
197
# File 'lib/graphiti/sideload.rb', line 195

def default_render_resource_ids?
  false
end

#description ⇒ Object



300
301
302
303
# File 'lib/graphiti/sideload.rb', line 300

def description
  return @description if @description.present?
  parent_resource_class.resolve_i18n_field_description(name, field_type: :relationships)
end

#disassociate(parent, child) ⇒ Object



473
474
475
# File 'lib/graphiti/sideload.rb', line 473

def disassociate(parent, child)
  parent_resource.disassociate(parent, child, association_name, type)
end

#errors ⇒ Object



120
121
122
# File 'lib/graphiti/sideload.rb', line 120

def errors
  @errors ||= []
end

#fire_hooks!(parent, objects, method) ⇒ Object



452
453
454
455
456
457
458
459
# File 'lib/graphiti/sideload.rb', line 452

def fire_hooks!(parent, objects, method)
  return unless self.class.hooks

  all = self.class.hooks[:"after_#{method}"] + self.class.hooks[:after_save]
  all.compact.each do |hook|
    resource.instance_exec(parent, objects, &hook)
  end
end

#foreign_key ⇒ Object



271
272
273
# File 'lib/graphiti/sideload.rb', line 271

def foreign_key
  @foreign_key ||= infer_foreign_key
end

#future_resolve(parents, query, graph_parent, &proxy_block) ⇒ Object

A scope_proc builds a Scope rather than a proxy, and a Scope's cache key omits what a proxy's carries.



422
423
424
425
426
427
428
429
430
431
432
# File 'lib/graphiti/sideload.rb', line 422

def future_resolve(parents, query, graph_parent, &proxy_block)
  assert_singular!(parents)

  if self.class.scope_proc
    build_sideload_scope(parents, query, graph_parent).future_resolve do |sideload_results|
      fire_assign(parents, sideload_results, query)
    end
  else
    future_load(parents, query, graph_parent, &proxy_block)
  end
end

#guarded? ⇒ Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/graphiti/sideload.rb', line 136

def guarded?
  dynamic_flag?(@readable) || dynamic_flag?(@writable)
end

#ids_for_parents(parents) ⇒ Object



477
478
479
480
481
482
# File 'lib/graphiti/sideload.rb', line 477

def ids_for_parents(parents)
  parent_ids = parents.map(&primary_key)
  parent_ids.compact!
  parent_ids.uniq!
  parent_ids
end

#infer_foreign_key ⇒ Object

Override in subclass



353
354
355
356
357
358
# File 'lib/graphiti/sideload.rb', line 353

def infer_foreign_key
  model = parent_resource_class.model
  namespace = namespace_for(model)
  model_name = model.name.gsub("#{namespace}::", "")
  :"#{model_name.underscore}_id"
end

#link? ⇒ Boolean

Returns:

  • (Boolean)


217
218
219
# File 'lib/graphiti/sideload.rb', line 217

def link?
  link_mode != false
end


239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/graphiti/sideload.rb', line 239

def link_extra_fields
  return unless context&.respond_to?(:params)

  extra_fields_name = [association_name, resource.type].find { |param|
    context.params.dig(:extra_fields, param)
  }

  if extra_fields_name
    extra_fields = context.params.dig(:extra_fields, extra_fields_name)
    {resource.type => extra_fields}
  end
end


235
236
237
# File 'lib/graphiti/sideload.rb', line 235

def link_filter(parents)
  base_filter(parents)
end

Returns:

  • (Boolean)


221
222
223
# File 'lib/graphiti/sideload.rb', line 221

def link_hides_primary_key?
  true
end

A custom link block means the author wants the link, so a false default does not silence it.



200
201
202
203
204
205
206
# File 'lib/graphiti/sideload.rb', line 200

def link_mode
  mode = requested_link_mode
  return false unless mode
  return false unless link_proc || link_hides_primary_key?

  mode
end

#load(parents, query, graph_parent, &proxy_block) ⇒ Object



338
339
340
341
342
343
344
# File 'lib/graphiti/sideload.rb', line 338

def load(parents, query, graph_parent, &proxy_block)
  if Scope.resolve_synchronously?
    sync_load(parents, query, graph_parent, &proxy_block)
  else
    future_load(parents, query, graph_parent, &proxy_block).value!
  end
end

#load_params(parents, query) ⇒ Object



296
297
298
# File 'lib/graphiti/sideload.rb', line 296

def load_params(parents, query)
  raise "Override #load_params in subclass"
end

#non_default_options ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/graphiti/sideload.rb', line 148

def non_default_options
  options = {}
  options[:as] = association_name if @as
  options[:primary_key] = primary_key unless primary_key == :id
  options[:single] = true if single?
  options[:remote] = @remote if remote?
  options[:link] = @link unless @link.nil?
  options[:readable] = @readable unless @readable.nil? || @readable == true
  options[:writable] = @writable unless @writable.nil? || @writable == true
  options[:resource_ids] = @render_resource_ids unless @render_resource_ids.nil?
  options
end

#parent_resource ⇒ Object



364
365
366
# File 'lib/graphiti/sideload.rb', line 364

def parent_resource
  @parent_resource ||= parent_resource_class.new
end

#performant_assign? ⇒ Boolean

Returns:

  • (Boolean)


484
485
486
# File 'lib/graphiti/sideload.rb', line 484

def performant_assign?
  !self.class.assign_each_proc
end

#polymorphic_child? ⇒ Boolean

Returns:

  • (Boolean)


263
264
265
# File 'lib/graphiti/sideload.rb', line 263

def polymorphic_child?
  !!@polymorphic_child
end

#polymorphic_has_many? ⇒ Boolean

Returns:

  • (Boolean)


173
174
175
# File 'lib/graphiti/sideload.rb', line 173

def polymorphic_has_many?
  !!@polymorphic_as
end

#polymorphic_has_one? ⇒ Boolean

Returns:

  • (Boolean)


169
170
171
# File 'lib/graphiti/sideload.rb', line 169

def polymorphic_has_one?
  !!@polymorphic_as
end

#polymorphic_parent? ⇒ Boolean

Returns:

  • (Boolean)


259
260
261
# File 'lib/graphiti/sideload.rb', line 259

def polymorphic_parent?
  resource.polymorphic?
end

#primary_key ⇒ Object



267
268
269
# File 'lib/graphiti/sideload.rb', line 267

def primary_key
  @primary_key ||= :id
end

#readable? ⇒ Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/graphiti/sideload.rb', line 128

def readable?
  evaluate_flag @readable
end

#readable_guard_name ⇒ Object



144
145
146
# File 'lib/graphiti/sideload.rb', line 144

def readable_guard_name
  @readable.to_sym if @readable.is_a?(Symbol) || @readable.is_a?(String)
end

#readable_guarded? ⇒ Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/graphiti/sideload.rb', line 140

def readable_guarded?
  dynamic_flag?(@readable)
end

#register_public_id_source ⇒ Object



228
229
# File 'lib/graphiti/sideload.rb', line 228

def register_public_id_source
end

#remote? ⇒ Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/graphiti/sideload.rb', line 124

def remote?
  !!@remote
end

#render_resource_ids? ⇒ Boolean

Returns:

  • (Boolean)


189
190
191
192
193
# File 'lib/graphiti/sideload.rb', line 189

def render_resource_ids?
  return !!@render_resource_ids unless @render_resource_ids.nil?

  default_render_resource_ids?
end

#rendered_id_for(foreign_key, query) ⇒ Object



231
232
233
# File 'lib/graphiti/sideload.rb', line 231

def rendered_id_for(foreign_key, query)
  foreign_key
end


208
209
210
# File 'lib/graphiti/sideload.rb', line 208

def requested_link_mode
  @link.nil? ? default_link_mode : @link
end

#resolve(parents, query, graph_parent, &proxy_block) ⇒ Object



399
400
401
402
403
404
405
# File 'lib/graphiti/sideload.rb', line 399

def resolve(parents, query, graph_parent, &proxy_block)
  if Scope.resolve_synchronously?
    sync_resolve(parents, query, graph_parent, &proxy_block)
  else
    future_resolve(parents, query, graph_parent, &proxy_block).value!
  end
end

#resource ⇒ Object



360
361
362
# File 'lib/graphiti/sideload.rb', line 360

def resource
  @resource ||= resource_class.new
end

#resource_class ⇒ Object



279
280
281
282
# File 'lib/graphiti/sideload.rb', line 279

def resource_class
  @resource_class ||= (@resource_class_name.is_a?(String) ? @resource_class_name.constantize : @resource_class_name) ||
    infer_resource_class
end

#resource_class_loaded? ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


489
490
491
492
493
494
# File 'lib/graphiti/sideload.rb', line 489

def resource_class_loaded?
  resource_class
  true
rescue Graphiti::Errors::ResourceNotFound
  false
end

#resource_ids_blocker ⇒ Object



185
186
187
# File 'lib/graphiti/sideload.rb', line 185

def resource_ids_blocker
  :no_foreign_key_on_parent
end

#resource_ids_from_foreign_key? ⇒ Boolean

Every check behind the blocker is static sideload configuration, and this is asked once per rendered record per relationship.

Returns:

  • (Boolean)


179
180
181
182
183
# File 'lib/graphiti/sideload.rb', line 179

def resource_ids_from_foreign_key?
  return @resource_ids_from_foreign_key unless @resource_ids_from_foreign_key.nil?

  @resource_ids_from_foreign_key = resource_ids_blocker.nil?
end

#scope(parents) ⇒ Object



284
285
286
# File 'lib/graphiti/sideload.rb', line 284

def scope(parents)
  raise "No #scope defined for sideload with name '#{name}'. Make sure to define this in your adapter, or pass a block that defines the scope."
end

#shared_remote? ⇒ Boolean

The parent resource is a remote, AND the sideload is a remote to the same endpoint

Returns:

  • (Boolean)


254
255
256
257
# File 'lib/graphiti/sideload.rb', line 254

def shared_remote?
  resource.remote? &&
    resource.remote_base_url = parent_resource_class.remote_base_url
end

#single? ⇒ Boolean

Returns:

  • (Boolean)


165
166
167
# File 'lib/graphiti/sideload.rb', line 165

def single?
  !!@single
end

#sync_load(parents, query, graph_parent, &proxy_block) ⇒ Object



346
347
348
349
350
# File 'lib/graphiti/sideload.rb', line 346

def sync_load(parents, query, graph_parent, &proxy_block)
  proxy = build_resource_proxy(parents, query, graph_parent)
  proxy_block&.call(proxy)
  proxy.to_a
end

#sync_resolve(parents, query, graph_parent, &proxy_block) ⇒ Object

Called by a scope that already decided to stay inline, so it must not consult the pool again. A scope_proc builds a Scope rather than a proxy, and that nested scope decides for itself.



409
410
411
412
413
414
415
416
417
418
419
# File 'lib/graphiti/sideload.rb', line 409

def sync_resolve(parents, query, graph_parent, &proxy_block)
  assert_singular!(parents)

  if self.class.scope_proc
    build_sideload_scope(parents, query, graph_parent).resolve do |sideload_results|
      fire_assign(parents, sideload_results, query)
    end
  else
    sync_load(parents, query, graph_parent, &proxy_block)
  end
end

#type ⇒ Object



292
293
294
# File 'lib/graphiti/sideload.rb', line 292

def type
  @type || raise("Override #type in subclass. Should be one of #{TYPES.inspect}")
end

#writable? ⇒ Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/graphiti/sideload.rb', line 132

def writable?
  evaluate_flag @writable
end