Sha256: 5a22a1597f2adf717bc0a23da68c62732cbafb3007886bca43bc05a67998547f

Contents?: true

Size: 1012 Bytes

Versions: 1

Compression:

Stored size: 1012 Bytes

Contents

# frozen_string_literal: true

require "speek/export/base"
require "speek/application"
require "speek"

module Speek
  module Export
    # for generate GraphQL code
    class Graphql < Base
      def export
        <<~GRAPHQL
          type #{class_name} {
            #{@app.schema_data.columns.map { |column| generate_field(column) }.join("\n  ")}
          }
        GRAPHQL
      end

      private

      def class_name = @app.schema_data.table_name.singularize.camelize

      def generate_field(column_data)
        type = convert_type(column_data.type)
        type = "#{column_data.type}!" unless column_data.nullable
        "#{column_data.name.camelize(:lower)}: #{type}"
      end

      def convert_type(type)
        case type
        when :integer, :bigint
          "Int"
        when :string, :text, :datetime
          "String"
        when :boolean
          "Boolean"
        else
          raise UnknownColumnTypeError, "Unknown column type: #{type}"
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
speek-0.1.0 lib/speek/export/graphql.rb