Sha256: e1871862f3b2ea5346b99c69e89f208908d4f903abcc3b7eb5da9aef004a4240

Contents?: true

Size: 1.89 KB

Versions: 11

Compression:

Stored size: 1.89 KB

Contents

module Souls
  class Generate < Thor
    desc "type_rbs [CLASS_NAME]", "Generate GraphQL Type RBS from schema.rb"
    def type_rbs(class_name)
      singularized_class_name = class_name.underscore.singularize
      file_path = ""
      Dir.chdir(Souls.get_mother_path.to_s) do
        file_dir = "./sig/api/app/graphql/types"
        FileUtils.mkdir_p(file_dir) unless Dir.exist?(file_dir)
        file_path = "#{file_dir}/#{singularized_class_name}_type.rbs"
        raise(Thor::Error, "Type RBS already exist! #{file_path}") if File.exist?(file_path)

        params = Souls.get_relation_params(class_name: singularized_class_name)
        File.open(file_path, "w") do |f|
          f.write(<<~TEXT)
            module Types
              class #{singularized_class_name.camelize}Type < BaseObject
                def self.implements: (*untyped) -> untyped
                def self.global_id_field: (:id) -> String
          TEXT
        end
        File.open(file_path, "a") do |f|
          params[:params].each_with_index do |param, i|
            type = Souls.rbs_type_check(param[:type])
            type = "[#{type}]" if param[:array]
            rbs_type = Souls.rbs_type_check(param[:type])
            if i.zero?
              f.write("    def self.field: (:#{param[:column_name]}, #{type}, null: false) -> #{rbs_type}\n")
            else
              f.write("                  | (:#{param[:column_name]}, #{type}, null: false) -> #{rbs_type}\n")
            end
          end
        end

        File.open(file_path, "a") do |f|
          f.write(<<~TEXT)
              def self.edge_type: () -> void
                def self.connection_type: () -> void
              end
            end
          TEXT
        end
      end
      puts(Paint % ["Created file! : %{white_text}", :green, { white_text: [file_path.to_s, :white] }])
      file_path
    rescue Thor::Error => e
      raise(Thor::Error, e)
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
souls-0.58.3 lib/souls/api/generate/type_rbs.rb
souls-0.58.2 lib/souls/api/generate/type_rbs.rb
souls-0.58.1 lib/souls/api/generate/type_rbs.rb
souls-0.58.0 lib/souls/api/generate/type_rbs.rb
souls-0.57.1 lib/souls/api/generate/type_rbs.rb
souls-0.57.0 lib/souls/api/generate/type_rbs.rb
souls-0.56.4 lib/souls/api/generate/type_rbs.rb
souls-0.56.3 lib/souls/api/generate/type_rbs.rb
souls-0.56.2 lib/souls/api/generate/type_rbs.rb
souls-0.56.1 lib/souls/api/generate/type_rbs.rb
souls-0.56.0 lib/souls/api/generate/type_rbs.rb