Class: GraphqlMigrateExecution::FallbackValue

Inherits:
Strategy
  • Object
show all
Defined in:
lib/graphql_migrate_execution/fallback_value.rb

Overview

Turns ‘fallback_value: …` into `resolve_static: true`

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



35
36
37
# File 'lib/graphql_migrate_execution/fallback_value.rb', line 35

def cleanup(field_definition)
  remove_field_keyword(field_definition, :fallback_value)
end

#migrate(field_definition) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/graphql_migrate_execution/fallback_value.rb', line 7

def migrate(field_definition)
  indent = field_definition.node.location.slice_lines[/^ +/]
  method_name = self.class.prefix_if_necessary(field_definition.name)
  is_interface = field_definition.type_definition.is_interface
  new_body = "\n".dup

  if is_interface
    method_prefix = "def #{method_name}"
    new_body << "#{indent}resolver_methods do\n"
    method_indent = indent + "  "
  else
    method_prefix = "def self.#{method_name}"
    method_indent = indent
  end

  new_body << "#{method_indent}#{method_prefix}(_context)\n"
  new_body << method_indent + "  #{field_definition.fallback_value}\n"
  new_body << method_indent + "end"

  if is_interface
    new_body << "\n#{indent}end"
  end

  @result_source.sub!(field_definition.source, field_definition.source + "\n" + new_body)
  keyword_v = method_name == field_definition.name.to_s ? true : method_name.to_sym.inspect
  inject_field_keyword(field_definition, :resolve_static, keyword_v)
end