Class: GraphqlMigrateExecution::DataloaderBatch

Inherits:
DataloaderAll show all
Defined in:
lib/graphql_migrate_execution/dataloader_batch.rb

Overview

These fields return an array of values using Dataloader, based on a method or attribute of ‘object`. They can be migrated to `dataloader_all` calls, using `object.flat_map`.

TODO: This is not quite right yet. It returns a single array instead of an array of arrays.

Instead, this should create an Array of arrays using ‘dataloader.request_all`.

Constant Summary

Constants inherited from Strategy

Strategy::METHODS_TO_RENAME

Instance Method Summary collapse

Methods inherited from Strategy

#initialize, prefix_if_necessary, #run, strategy_name

Constructor Details

This class inherits a constructor from GraphqlMigrateExecution::Strategy

Instance Method Details

#cleanup(field_definition) ⇒ Object



66
67
68
# File 'lib/graphql_migrate_execution/dataloader_batch.rb', line 66

def cleanup(field_definition)
  remove_resolver_method(field_definition)
end

#migrate(field_definition) ⇒ Object



13
14
15
16
17
18
19
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
59
60
61
62
63
64
# File 'lib/graphql_migrate_execution/dataloader_batch.rb', line 13

def migrate(field_definition)
  inject_resolve_keyword(field_definition, :resolve_batch)
  # inject_batch_dataloader_method(field_definition, [:request_all, :load_all], :dataload_all, "flat_map")

  def_node = field_definition.resolver_method.node
  call_node = def_node.body.body.first
  case call_node.name
  when :request_all, :load_all
    load_arg_node = call_node.arguments.arguments.first
    with_node = call_node.receiver
    source_class_node, *source_args_nodes = with_node.arguments
  when :dataload_all
    source_class_node, *source_args_nodes, load_arg_node = call_node.arguments.arguments
  else
    raise ArgumentError, "Unexpected DataloadAll method name: #{def_node.name.inspect}"
  end

  old_load_arg_s = load_arg_node.slice
  new_load_arg_s = case old_load_arg_s
  when "object"
    "object"
  when /object((\.|\[)[:a-zA-Z0-9_\.\"\'\[\]]+)/
    call_chain = $1
    "object#{call_chain}"
  else
    raise ArgumentError, "Failed to transform Dataloader argument: #{old_load_arg_s.inspect}"
  end
  new_source_args = [
    source_class_node.slice,
    *source_args_nodes.map(&:slice)
  ].join(", ")

  old_method_source = def_node.slice_lines
  new_method_source = old_method_source.sub(/def ([a-z_A-Z0-9]+)(\(|$| )/) do
    is_adding_args = $2.size == 0
    "def self.#{$1}#{is_adding_args ? "(" : $2}objects, context#{is_adding_args ? ")" : ", "}"
  end

  old_source_lines = call_node.slice_lines
  leading_whitespace = old_source_lines[/^\s+/]

  new_method_body = <<~RUBY
    #{leading_whitespace}requests = objects.map { |object| context.dataloader.with(#{new_source_args}).request_all(#{new_load_arg_s}) }
    #{leading_whitespace}requests.map! { |reqs| reqs.map!(&:load) } # replace dataloader requests with loaded data
    #{leading_whitespace}requests
  RUBY

  new_method_source.sub!(old_source_lines, new_method_body)

  combined_new_source = new_method_source + "\n" + old_method_source
  @result_source.sub!(old_method_source, combined_new_source)
end