Skip to content
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
9 changes: 6 additions & 3 deletions lib/search_object/plugin/graphql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@ def option(name, options = {}, &block)
type = options.fetch(:type) { raise MissingTypeDefinitionError, name }
options[:enum] = type.values.map { |value, enum_value| enum_value.value || value } if type.respond_to?(:values)

argument_options = { required: options[:required] || false }
argument_options[:camelize] = options[:camelize] if options.include?(:camelize)
argument_options[:default_value] = options[:default] if options.include?(:default)
argument_options[:description] = options[:description] if options.include?(:description)

argument(
name.to_s,
options[:type],
required: options[:required] || false,
description: options[:description],
camelize: options[:camelize]
**argument_options
)

super(name, options, &block)
Expand Down
70 changes: 70 additions & 0 deletions spec/search_object/plugin/graphql_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,42 @@ def define_search_class_and_return_schema(&block)
)
end

it 'sets default_value on the argument' do
schema = define_search_class_and_return_schema do
type PostType, null: true

option('option', type: types.String, default: 'default') { [] }
end

result = schema.execute <<~GRAPHQL
{
__type(name: "Query") {
name
fields {
args {
name
defaultValue
}
}
}
}
GRAPHQL

expect(result).to eq(
'data' => {
'__type' => {
'name' => 'Query',
'fields' => [{
'args' => [{
'name' => 'option',
'defaultValue' => '"default"'
}]
}]
}
}
)
end

it 'accepts "required"' do
schema = define_search_class_and_return_schema do
option(:id, type: types.String, required: true) do |_scope, value|
Expand Down Expand Up @@ -299,6 +335,40 @@ def define_search_class_and_return_schema(&block)
)
end

it 'does not override the default camelize option' do
schema = define_search_class_and_return_schema do
type PostType, null: true

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

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

expect(result.to_h).to eq(
'data' => {
'__type' => {
'name' => 'Query',
'fields' => [{
'args' => [{
'name' => 'optionField'
}]
}]
}
}
)
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