Class: Foreman::Plugin::GraphqlTypesRegistry

Inherits:
Object
  • Object
show all
Defined in:
app/registries/foreman/plugin/graphql_types_registry.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGraphqlTypesRegistry

Returns a new instance of GraphqlTypesRegistry.


7
8
9
10
11
12
# File 'app/registries/foreman/plugin/graphql_types_registry.rb', line 7

def initialize
  @type_block_extensions = {}
  @type_module_extensions = {}
  @plugin_query_fields = []
  @plugin_mutation_fields = []
end

Instance Attribute Details

#plugin_mutation_fieldsObject (readonly)

Returns the value of attribute plugin_mutation_fields


5
6
7
# File 'app/registries/foreman/plugin/graphql_types_registry.rb', line 5

def plugin_mutation_fields
  @plugin_mutation_fields
end

#plugin_query_fieldsObject (readonly)

Returns the value of attribute plugin_query_fields


5
6
7
# File 'app/registries/foreman/plugin/graphql_types_registry.rb', line 5

def plugin_query_fields
  @plugin_query_fields
end

#type_block_extensionsObject (readonly)

Returns the value of attribute type_block_extensions


5
6
7
# File 'app/registries/foreman/plugin/graphql_types_registry.rb', line 5

def type_block_extensions
  @type_block_extensions
end

#type_module_extensionsObject (readonly)

Returns the value of attribute type_module_extensions


5
6
7
# File 'app/registries/foreman/plugin/graphql_types_registry.rb', line 5

def type_module_extensions
  @type_module_extensions
end

Instance Method Details

#realise_block_extensionsObject


46
47
48
49
50
51
52
53
# File 'app/registries/foreman/plugin/graphql_types_registry.rb', line 46

def realise_block_extensions
  type_block_extensions.each do |type, extensions|
    type = Foreman::Module.resolve(type)
    extensions.each do |extension|
      type.class_eval(&extension)
    end
  end
end

#realise_extensionsObject

Realises previously registered extensions


42
43
44
# File 'app/registries/foreman/plugin/graphql_types_registry.rb', line 42

def realise_extensions
  realise_block_extensions && realise_module_extensions
end

#realise_module_extensionsObject


55
56
57
58
59
60
61
62
63
# File 'app/registries/foreman/plugin/graphql_types_registry.rb', line 55

def realise_module_extensions
  type_module_extensions.each do |type, extensions|
    type = Foreman::Module.resolve(type)
    extensions.each do |extension|
      extension = Foreman::Module.resolve(extension)
      type.send(:include, extension)
    end
  end
end

#register_block_extension(type:, &block) ⇒ Object


31
32
33
34
# File 'app/registries/foreman/plugin/graphql_types_registry.rb', line 31

def register_block_extension(type:, &block)
  @type_block_extensions[type] ||= []
  @type_block_extensions[type] << block
end

#register_extension(type:, with_module: nil, &block) ⇒ Object

Register a new extension for a graphql type


26
27
28
29
# File 'app/registries/foreman/plugin/graphql_types_registry.rb', line 26

def register_extension(type:, with_module: nil, &block)
  return register_block_extension(type: type, &block) if block_given?
  register_module_extension(type: type, with_module: with_module)
end

#register_module_extension(type:, with_module:) ⇒ Object


36
37
38
39
# File 'app/registries/foreman/plugin/graphql_types_registry.rb', line 36

def register_module_extension(type:, with_module:)
  @type_module_extensions[type] ||= []
  @type_module_extensions[type] << with_module
end

#register_plugin_mutation_field(field_name, mutation) ⇒ Object


21
22
23
# File 'app/registries/foreman/plugin/graphql_types_registry.rb', line 21

def register_plugin_mutation_field(field_name, mutation)
  @plugin_mutation_fields << { :field_name => field_name, :mutation => mutation }
end

#register_plugin_query_field(field_name, type, field_type) ⇒ Object


14
15
16
17
18
19
# File 'app/registries/foreman/plugin/graphql_types_registry.rb', line 14

def register_plugin_query_field(field_name, type, field_type)
  unless [:record_field, :collection_field].any? { |field| field == field_type }
    raise "expected :record_field or :collection_field as a field_type, got #{field_type}"
  end
  @plugin_query_fields << { :field_type => field_type, :field_name => field_name, :type => type }
end