Sha256: fe8939fe3f9c17a3e4b1c29f3dda89e70665955951b53aa4b3e6e0b8984e6145

Contents?: true

Size: 1.26 KB

Versions: 5

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true

require 'graphql'
require 'graphql_rails/attribute/attribute_type_parser'

module GraphqlRails
  # contains info about single graphql attribute
  class Attribute
    attr_reader :name, :graphql_field_type, :property, :type_name, :description

    def initialize(name, type = nil, description: nil, property: name)
      @original_name = name.to_s
      @name = @original_name.tr('!', '')
      @type_name = type.to_s
      @graphql_field_type = parse_type(type || type_by_attribute_name)
      @description = description
      @property = property.to_s
    end

    def field_name
      field =
        if name.end_with?('?')
          "is_#{name.remove(/\?\Z/)}"
        else
          name
        end

      field.camelize(:lower)
    end

    def field_args
      [field_name, graphql_field_type, { property: property.to_sym, description: description }]
    end

    private

    attr_reader :original_name

    def type_by_attribute_name
      case name
      when 'id', /_id\Z/
        GraphQL::ID_TYPE
      when /\?\Z/
        GraphQL::BOOLEAN_TYPE
      else
        GraphQL::STRING_TYPE
      end
    end

    def parse_type(type)
      type = AttributeTypeParser.new(type).call

      original_name['!'] ? type.to_non_null_type : type
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
graphql_rails-0.3.3 lib/graphql_rails/attribute.rb
graphql_rails-0.3.2 lib/graphql_rails/attribute.rb
graphql_rails-0.3.1 lib/graphql_rails/attribute.rb
graphql_rails-0.3.0 lib/graphql_rails/attribute.rb
graphql_rails-0.2.4 lib/graphql_rails/attribute.rb