diff --git a/lib/search_object/plugin/graphql.rb b/lib/search_object/plugin/graphql.rb index 64005ee..96a7bf2 100644 --- a/lib/search_object/plugin/graphql.rb +++ b/lib/search_object/plugin/graphql.rb @@ -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) diff --git a/spec/search_object/plugin/graphql_spec.rb b/spec/search_object/plugin/graphql_spec.rb index 6954ab7..13c5c39 100644 --- a/spec/search_object/plugin/graphql_spec.rb +++ b/spec/search_object/plugin/graphql_spec.rb @@ -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| @@ -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