Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #32214 - Register query field with a resolver #8794

Merged
merged 1 commit into from
Oct 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/registries/foreman/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -619,8 +619,8 @@ def extend_graphql_type(type:, with_module: nil, &block)
graphql_types_registry.register_extension(type: type, with_module: with_module, &block)
end

def register_graphql_query_field(field_name, type, field_type)
graphql_types_registry.register_plugin_query_field(field_name, type, field_type)
def register_graphql_query_field(field_name, type, field_type, options = nil)
graphql_types_registry.register_plugin_query_field(field_name, type, field_type, options)
end

def register_graphql_mutation_field(field_name, mutation_class)
Expand Down
6 changes: 5 additions & 1 deletion app/registries/foreman/plugin/graphql_plugin_fields.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ module GraphqlPluginFields
module ClassMethods
def realize_plugin_query_extensions(source = Foreman::Plugin.graphql_types_registry.plugin_query_fields)
source.map do |plugin_type|
send plugin_type[:field_type], plugin_type[:field_name], Foreman::Module.resolve(plugin_type[:type])
if plugin_type[:options].empty?
send plugin_type[:field_type], plugin_type[:field_name], Foreman::Module.resolve(plugin_type[:type])
ezr-ondrej marked this conversation as resolved.
Show resolved Hide resolved
else
send plugin_type[:field_type], plugin_type[:field_name], Foreman::Module.resolve(plugin_type[:type]), plugin_type[:options]
end
end
end

Expand Down
11 changes: 7 additions & 4 deletions app/registries/foreman/plugin/graphql_types_registry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ def initialize
@plugin_mutation_fields = []
end

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}"
def register_plugin_query_field(field_name, type, field_type, options = {})
unless [:record_field, :collection_field, :field].any? { |field| field == field_type }
raise "expected :record_field, :collection_field or :field as a field_type, got #{field_type}"
end
@plugin_query_fields << { :field_type => field_type, :field_name => field_name, :type => type }
if [:record_field, :collection_field].any? { |field| field == field_type && !options.empty? }
raise "options are allowed only for :field"
end
@plugin_query_fields << { :field_type => field_type, :field_name => field_name, :type => type, :options => options }
end

def register_plugin_mutation_field(field_name, mutation)
Expand Down
42 changes: 34 additions & 8 deletions test/unit/foreman/plugin/graphql_plugin_fields_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,29 @@ class GraphqlType
include ::Foreman::Plugin::GraphqlPluginFields

class << self
attr_accessor :record_fields, :collection_fields, :mutation_fields
attr_accessor :record_fields, :collection_fields, :mutation_fields, :fields

def record_field(name, type)
push_item :record_fields, name, type
push_item :record_fields, name, type, {}
end

def collection_field(name, type)
push_item :collection_fields, name, type
push_item :collection_fields, name, type, {}
end

def field(name, mutation)
item = { :name => name, :mutation => mutation }
@mutation_fields ? @mutation_fields << item : @mutation_fields = [item]
def field(name, type_or_opts, opts = {})
if type_or_opts.is_a?(Hash) && type_or_opts[:mutation]
item = { :name => name, :mutation => type_or_opts[:mutation] }
@mutation_fields ? @mutation_fields << item : @mutation_fields = [item]
else
push_item :fields, name, type_or_opts, opts
end
end

private

def push_item(place, name, type)
item = { :name => name, :type => type }
def push_item(place, name, type, opts)
item = { :name => name, :type => type }.merge(opts)
instance_var = instance_variable_get("@#{place}")
instance_var ? instance_var << item : instance_variable_set("@#{place}", [item])
end
Expand Down Expand Up @@ -62,6 +66,28 @@ def push_item(place, name, type)
assert_equal 1, klass.collection_fields.size
assert_equal :woof, klass.collection_fields.first[:name]
end

it 'adds a field with custom resolver' do
registry = Foreman::Plugin::GraphqlTypesRegistry.new.tap do |reg|
reg.register_plugin_query_field :quack, -> { [String] }, :field, :resolver => Object
end

klass = Class.new(GraphqlType)

assert_nil klass.record_fields
assert_nil klass.collection_fields
assert_nil klass.mutation_fields
assert_nil klass.fields
klass.realize_plugin_query_extensions registry.plugin_query_fields

assert_nil klass.record_fields
assert_nil klass.collection_fields
assert_nil klass.mutation_fields

assert_equal 1, klass.fields.size
assert_equal :quack, klass.fields.first[:name]
assert klass.fields.first[:resolver]
end
end

describe 'graphql plugin mutation fields' do
Expand Down
2 changes: 1 addition & 1 deletion test/unit/foreman/plugin/graphql_types_registry_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def foo
err = assert_raises RuntimeError do
registry.register_plugin_query_field :woof, 'TestType', :custom_field
end
assert_equal err.message, "expected :record_field or :collection_field as a field_type, got custom_field"
assert_equal err.message, "expected :record_field, :collection_field or :field as a field_type, got custom_field"
end

it 'registeres plugin mutation fields' do
Expand Down