Class: GraphQL::Upgrader::UpdateMethodSignatureTransform

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

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



638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
# File 'lib/graphql/upgrader/member.rb', line 638

def apply(input_text)
  input_text.scan(/(?:input_field|field|return_field|connection|argument) .*$/).each do |field|
    matches = /(?<field_type>input_field|return_field|field|connection|argument) :(?<name>[a-zA-Z_0-9_]*)?(:?, +(?<return_type>([A-Za-z\[\]\.\!_0-9\(\)]|::|-> ?\{ ?| ?\})+))?(?<remainder>( |,|$).*)/.match(field)
    if matches
      name = matches[:name]
      return_type = matches[:return_type]
      remainder = matches[:remainder]
      field_type = matches[:field_type]
      with_block = remainder.gsub!(/\ do$/, '')

      remainder.gsub! /,$/, ''
      remainder.gsub! /^,/, ''
      remainder.chomp!

      if return_type
        non_nullable = return_type.sub! /(^|[^\[])!/, '\1'
        non_nullable ||= return_type.sub! /([^\[])\.to_non_null_type([^\]]|$)/, '\1'
        nullable = !non_nullable
        return_type = normalize_type_expression(return_type)
      else
        non_nullable = nil
        nullable = nil
      end

      input_text.sub!(field) do
        is_argument = ['argument', 'input_field'].include?(field_type)
        f = "#{is_argument ? 'argument' : 'field'} :#{name}"

        if return_type
          f += ", #{return_type}"
        end

        unless remainder.empty?
          f += ',' + remainder
        end

        if is_argument
          if nullable
            f += ', required: false'
          elsif non_nullable
            f += ', required: true'
          end
        else
          if nullable
            f += ', null: true'
          elsif non_nullable
            f += ', null: false'
          end
        end

        if field_type == 'connection'
          f += ', connection: true'
        end

        if with_block
          f += ' do'
        end

        f
      end
    end
  end

  input_text
end