Module: Zenspec::Matchers::GraphQLTypeMatchers::FieldNameHelper

Defined in:
lib/zenspec/matchers/graphql_type_matchers.rb

Overview

Helper module for field name conversion

Instance Method Summary collapse

Instance Method Details

#camelize(name) ⇒ Object

Convert snake_case to camelCase for GraphQL field lookup GraphQL-Ruby automatically converts snake_case field definitions to camelCase

Handles edge cases:

  • Empty strings: "" -> ""
  • No underscores: "foo" -> "foo"
  • Leading underscores: "_foo_bar" -> "fooBar"
  • Trailing underscores: "foo_bar_" -> "fooBar"
  • Consecutive underscores: "foo__bar" -> "fooBar"


19
20
21
22
23
24
25
26
27
28
# File 'lib/zenspec/matchers/graphql_type_matchers.rb', line 19

def camelize(name)
  str = name.to_s
  return str if str.empty? || !str.include?("_")

  # Split on underscores and reject empty parts (handles consecutive underscores)
  parts = str.split("_").reject(&:empty?)
  return str if parts.empty?

  parts.map.with_index { |word, i| i.zero? ? word : word.capitalize }.join
end

#get_argument_with_conversion(field, arg_name) ⇒ Object

Try to get an argument by name, attempting both the original name and camelCase version



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/zenspec/matchers/graphql_type_matchers.rb', line 57

def get_argument_with_conversion(field, arg_name)
  return nil unless field.respond_to?(:arguments)

  arg_name_str = arg_name.to_s

  # Try the exact name first (for backward compatibility)
  arg = field.arguments[arg_name_str]
  return arg if arg

  # Try the camelCase version if the original name contains underscores
  if arg_name_str.include?("_")
    camel_name = camelize(arg_name_str)
    arg = field.arguments[camel_name]
    return arg if arg
  end

  nil
end

#get_field_exact(type, field_name) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/zenspec/matchers/graphql_type_matchers.rb', line 48

def get_field_exact(type, field_name)
  if type.respond_to?(:fields)
    type.fields[field_name]
  elsif type.respond_to?(:get_field)
    type.get_field(field_name)
  end
end

#get_field_with_conversion(type, field_name) ⇒ Object

Try to get a field by name, attempting both the original name and camelCase version



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/zenspec/matchers/graphql_type_matchers.rb', line 31

def get_field_with_conversion(type, field_name)
  field_name_str = field_name.to_s

  # Try the exact name first (for backward compatibility)
  field = get_field_exact(type, field_name_str)
  return field if field

  # Try the camelCase version if the original name contains underscores
  if field_name_str.include?("_")
    camel_name = camelize(field_name_str)
    field = get_field_exact(type, camel_name)
    return field if field
  end

  nil
end

#list?(type) ⇒ Boolean

Check if a type is a List type

Returns:

  • (Boolean)


108
109
110
111
# File 'lib/zenspec/matchers/graphql_type_matchers.rb', line 108

def list?(type)
  type.is_a?(GraphQL::Schema::List) ||
    (type.respond_to?(:list?) && type.list?)
end

#non_null?(type) ⇒ Boolean

Check if a type is a NonNull type

Returns:

  • (Boolean)


114
115
116
117
# File 'lib/zenspec/matchers/graphql_type_matchers.rb', line 114

def non_null?(type)
  type.is_a?(GraphQL::Schema::NonNull) ||
    (type.respond_to?(:non_null?) && type.non_null?)
end

#type_to_string(type) ⇒ Object

Convert a GraphQL type object to its string representation Handles NonNull (!), List ([]), and nested types



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/zenspec/matchers/graphql_type_matchers.rb', line 80

def type_to_string(type)
  case type
  when GraphQL::Schema::NonNull
    "#{type_to_string(type.of_type)}!"
  when GraphQL::Schema::List
    "[#{type_to_string(type.of_type)}]"
  when GraphQL::Schema::Member
    type.graphql_name
  else
    # Handle wrapped types from graphql-ruby
    if type.respond_to?(:unwrap)
      unwrapped = type.unwrap
      base_name = unwrapped.respond_to?(:graphql_name) ? unwrapped.graphql_name : unwrapped.to_s

      # Build the type string with wrappers
      result = base_name
      result = "[#{result}]" if list?(type)
      result = "#{result}!" if non_null?(type)
      result
    elsif type.respond_to?(:graphql_name)
      type.graphql_name
    else
      type.to_s
    end
  end
end