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


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
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
# File 'lib/graphql/upgrader/member.rb', line 675

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