Class: GraphqlRails::Attributes::AttributeNameParser

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql_rails/attributes/attribute_name_parser.rb

Overview

Parses attribute name and can generates graphql scalar type, graphql name and etc. based on that

Instance Method Summary collapse

Constructor Details

#initialize(original_name, options: {}) ⇒ AttributeNameParser

Returns a new instance of AttributeNameParser.



8
9
10
11
# File 'lib/graphql_rails/attributes/attribute_name_parser.rb', line 8

def initialize(original_name, options: {})
  @original_name = original_name.to_s
  @options = options
end

Instance Method Details

#field_nameObject



13
14
15
16
17
18
19
20
# File 'lib/graphql_rails/attributes/attribute_name_parser.rb', line 13

def field_name
  @field_name ||= \
    if original_format?
      preprocessed_name
    else
      preprocessed_name.camelize(:lower)
    end
end

#graphql_typeObject



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/graphql_rails/attributes/attribute_name_parser.rb', line 22

def graphql_type
  @graphql_type ||= \
    case name
    when 'id', /_id\Z/
      GraphQL::Types::ID
    when /\?\Z/
      GraphQL::Types::Boolean
    else
      GraphQL::Types::String
    end
end

#nameObject



38
39
40
# File 'lib/graphql_rails/attributes/attribute_name_parser.rb', line 38

def name
  @name ||= original_name.tr('!', '')
end

#required?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/graphql_rails/attributes/attribute_name_parser.rb', line 34

def required?
  original_name['!'].present? || original_name.end_with?('?')
end