Skip to content

Commit

Permalink
Merge pull request #25 from glenbray/camelize-option
Browse files Browse the repository at this point in the history
Add support for camelize option
  • Loading branch information
RStankov committed Jun 7, 2020
2 parents ca3dead + 0d16a13 commit 4bc20f1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lib/search_object/plugin/graphql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def resolve_with_support(args = {})
end

module ClassMethods
KEYS = %i[type default description required].freeze
KEYS = %i[type default description required camelize].freeze
def option(name, options = {}, &block)
config[:arguments] ||= {}
config[:arguments][name.to_s] = KEYS.inject({}) do |acc, key|
Expand Down Expand Up @@ -100,14 +100,17 @@ def field_options
resolver_class: self,
deprecation_reason: deprecation_reason,
arguments: (config[:arguments] || {}).inject({}) do |acc, (name, options)|
name = name.to_s.split('_').map(&:capitalize).join
name[0] = name[0].downcase
if options.fetch(:camelize) { true }
name = name.to_s.split('_').map(&:capitalize).join
name[0] = name[0].downcase
end

acc[name] = ::GraphQL::Schema::Argument.new(
name: name.to_s,
type: options.fetch(:type) { raise MissingTypeDefinitionError, name },
description: options[:description],
required: !!options[:required],
camelize: !!options[:camelize],
default_value: options.fetch(:default) { ::GraphQL::Schema::Argument::NO_DEFAULT },
owner: self
)
Expand Down
34 changes: 34 additions & 0 deletions spec/search_object/plugin/graphql_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,40 @@ def define_search_class_and_return_schema(&block)
)
end

it 'accepts camelize' do
schema = define_search_class_and_return_schema do
type PostType

option('option_field', type: types.String, camelize: false)
end

result = schema.execute <<-SQL
{
__type(name: "Query") {
name
fields {
args {
name
}
}
}
}
SQL

expect(result.to_h).to eq(
'data' => {
'__type' => {
'name' => 'Query',
'fields' => [{
'args' => [{
'name' => 'option_field'
}]
}]
}
}
)
end

it 'raises error when no type is given' do
expect { define_search_class { option :name } }.to raise_error described_class::MissingTypeDefinitionError
end
Expand Down

0 comments on commit 4bc20f1

Please sign in to comment.