Class: GraphQL::Upgrader::UnderscorizeMutationHashTransform

Inherits:
Transform
  • Object
show all
Defined in:
lib/graphql/upgrader/member.rb

Overview

Find hash literals which are returned from mutation resolves, and convert their keys to underscores. This catches a lot of cases but misses hashes which are initialized anywhere except in the return expression.

Defined Under Namespace

Classes: ReturnedHashLiteralProcessor

Instance Method Summary collapse

Methods inherited from Transform

#apply_processor, #normalize_type_expression, #reindent_lines, #trim_lines, #underscorize

Instance Method Details

#apply(input_text) ⇒ Object



385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# File 'lib/graphql/upgrader/member.rb', line 385

def apply(input_text)
  if input_text =~ /def resolve\(\*\*/
    processor = apply_processor(input_text, ReturnedHashLiteralProcessor.new)
    # Use reverse_each to avoid messing up positions
    processor.keys_to_upgrade.reverse_each do |key_data|
      underscored_key = underscorize(key_data[:key].to_s)
      if key_data[:operator] == ":"
        input_text[key_data[:start]...key_data[:end]] = underscored_key
      else
        input_text[key_data[:start]...key_data[:end]] = ":#{underscored_key}"
      end
    end
  end
  input_text
end