Module: Rails::GraphQL::Helpers::Registerable

Included in:
Directive, Schema, Type
Defined in:
lib/rails/graphql/helpers/registerable.rb

Overview

Helper module responsible for registering the objects to the type map, which also checks for the uniqueness of the name of things.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(other) ⇒ Object

Here we define a couple of attributes used by registration



10
11
12
13
14
15
16
17
18
19
# File 'lib/rails/graphql/helpers/registerable.rb', line 10

def self.extended(other)
  other.extend(Helpers::Unregisterable)
  other.extend(Helpers::WithNamespace)
  other.extend(Helpers::WithName)
  other.extend(Helpers::WithDescription)

  # Marks if the object is one of those defined on the spec, which
  # marks the object as part of the introspection system
  other.class_attribute :spec_object, instance_accessor: false, default: false
end

Instance Method Details

#aliases(*list) ⇒ Object

Get or set a list of aliases for this object



53
54
55
56
57
58
59
60
61
# File 'lib/rails/graphql/helpers/registerable.rb', line 53

def aliases(*list)
  if list.empty?
    defined?(@aliases) ? @aliases : Set.new
  else
    (@aliases ||= Set.new).merge list.flatten.map do |item|
      item.to_s.underscore.to_sym
    end
  end
end

#inherited(subclass) ⇒ Object

Here we wait for the class to be fully loaded so that we can correctly trigger the registration



23
24
25
26
# File 'lib/rails/graphql/helpers/registerable.rb', line 23

def inherited(subclass)
  super if defined? super
  GraphQL.type_map.postpone_registration(subclass)
end

#register!Object

The process to register a class and it’s name on the index

Raises:



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rails/graphql/helpers/registerable.rb', line 34

def register!
  return if abstract? || gql_name.blank?

  raise DuplicatedError, (+<<~MSG).squish if registered?
    The "#{gql_name}" is already defined, the only way to change its
    definition is by using extensions.
  MSG

  invalid_internal = !spec_object && gql_name.start_with?('__')
  raise NameError, (+<<~MSG).squish if invalid_internal
    The name "#{gql_name}" is invalid. Only internal objects from the
    spec can have a name starting with "__".
  MSG

  super if defined? super
  GraphQL.type_map.register(self)
end

#registered?Boolean

Check if the class is already registered in the type map

Returns:

  • (Boolean)


29
30
31
# File 'lib/rails/graphql/helpers/registerable.rb', line 29

def registered?
  GraphQL.type_map.object_exist?(self, exclusive: true)
end