<commit_before>module GraphQL
  class TypeCreator

    # Return a GraphQL type for a 'database_type'
    TYPES_CONVERSION = Hash.new(GraphQL::STRING_TYPE).merge(
      integer: GraphQL::INT_TYPE,
      boolean: GraphQL::BOOLEAN_TYPE,
      float: GraphQL::FLOAT_TYPE,
      double: GraphQL::FLOAT_TYPE
    )

    def self.create(model, field_names, api_types)

      new_graphql_type = GraphQL::ObjectType.define do

        name(model.name)
        description("Generated programmatically from model: #{model.name}")

        # Make a field for each column
        field_names.each do |field_name|
          if model.column_names.include?(field_name.to_s)
            field(field_name.to_s, TYPES_CONVERSION[field_name])
          else
            association = model.reflect_on_all_associations.find { |a| a.name == field_name }
            case association.macro
            when :has_one
              field(association.name, -> { api_types[association.klass] })
            when :belongs_to
              field(association.name, -> { api_types[association.klass] })
            when :has_many
              connection(association.name, api_types[association.klass].connection_type {
                description "Description for a field with pagination"
              })
            end
          end
        end
      end<commit_msg>Fixed bug in TypeCreator and improved field descriptions<commit_after>module GraphQL
  class TypeCreator

    # Return a GraphQL type for a 'database_type'
    TYPES_CONVERSION = Hash.new(GraphQL::STRING_TYPE).merge(
      integer: GraphQL::INT_TYPE,
      boolean: GraphQL::BOOLEAN_TYPE,
      float: GraphQL::FLOAT_TYPE,
      double: GraphQL::FLOAT_TYPE
    )

    def self.create(model, field_names, api_types)

      new_graphql_type = GraphQL::ObjectType.define do

        name(model.name)
        description("#{model.model_name.human}")

        # Make a field for each column
        field_names.each do |field_name|
          if model.column_names.include?(field_name.to_s)
            field(field_name.to_s, TYPES_CONVERSION[field_name])
          else
            association = model.reflect_on_all_associations.find { |a| a.name == field_name }
            case association.macro
            when :has_one
              field(association.name, -> { api_types[association.klass] })
            when :belongs_to
              field(association.name, -> { api_types[association.klass] })
            when :has_many
              connection(association.name, api_types[association.klass].connection_type {
                description "#{association.klass.model_name.human.pluralize}"
                resolve -> (object, arguments, context) {
                  association.klass.all
                }
              })
            end
          end
        end
      end
{'fraction_non_alphanumeric': 0.08771306818181818, 'fraction_numerical': 0.0, 'mean_word_length': 2.2342135476463834, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '333069', 'n_tokens_mistral': 798, 'n_tokens_neox': 800, 'n_words': 158}