Sha256: 64b1d87f81c6003c500d64cda3b3975fb379e91af895a44f35c5ce9dfa3ae5dd

Contents?: true

Size: 1.93 KB

Versions: 5

Compression:

Stored size: 1.93 KB

Contents

module Souls
  class Generate < Thor
    desc "query [CLASS_NAME]", "Generate GraphQL Query from schema.rb"
    def query(class_name)
      file_dir = "./app/graphql/queries/"
      FileUtils.mkdir_p(file_dir) unless Dir.exist?(file_dir)
      singularized_class_name = class_name.singularize
      create_individual_query(class_name: singularized_class_name)
      create_index_query(class_name: singularized_class_name)
    end

    private

    def create_index_query(class_name: "user")
      file_path = "./app/graphql/queries/#{class_name.pluralize}.rb"
      return "Query already exist! #{file_path}" if File.exist?(file_path)

      File.open(file_path, "w") do |f|
        f.write(<<~TEXT)
          module Queries
            class #{class_name.camelize.pluralize} < Queries::BaseQuery
              type [Types::#{class_name.camelize}Type], null: false

              def resolve
                ::#{class_name.camelize}.all
              end
            end
          end
        TEXT
      end
      Souls::Painter.create_file(file_path.to_s)
      file_path
    rescue StandardError => e
      raise(StandardError, e)
    end

    def create_individual_query(class_name: "user")
      file_path = "./app/graphql/queries/#{class_name}.rb"
      return "Query already exist! #{file_path}" if File.exist?(file_path)

      File.open(file_path, "w") do |f|
        f.write(<<~TEXT)
          module Queries
            class #{class_name.camelize} < Queries::BaseQuery
              type Types::#{class_name.camelize}Type, null: false
              argument :id, String, required: true

              def resolve args
                _, data_id = SoulsApiSchema.from_global_id args[:id]
                ::#{class_name.camelize}.find(data_id)
              end
            end
          end
        TEXT
        Souls::Painter.create_file(file_path.to_s)
        file_path
      rescue StandardError => e
        raise(StandardError, e)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
souls-1.13.4 lib/souls/cli/generate/query.rb
souls-1.13.3 lib/souls/cli/generate/query.rb
souls-1.13.2 lib/souls/cli/generate/query.rb
souls-1.13.1 lib/souls/cli/generate/query.rb
souls-1.13.0 lib/souls/cli/generate/query.rb