lib/rails/graphql/field/input_field.rb in rails-graphql-0.2.1 vs lib/rails/graphql/field/input_field.rb in rails-graphql-1.0.0.beta

- old
+ new

@@ -1,9 +1,9 @@ # frozen_string_literal: true -module Rails # :nodoc: - module GraphQL # :nodoc: +module Rails + module GraphQL # = GraphQL Input Field # # An input field works the same way as an argument and they are pretty much # equivalent. The main difference between an argument and a input field is # that input fields holds object-like values and they can be inherited. @@ -11,27 +11,28 @@ # with an InputField. # # ==== Options # # * <tt>:default</tt> - Sets a default value for the argument (defaults to nil). - # * <tt>:directives</tt> - The list of directives associated with the value - # (defaults to nil). class Field::InputField < Field include Field::TypedField attr_reader :default redefine_singleton_method(:input_type?) { true } self.directive_location = :input_field_definition def initialize(*args, default: nil, **xargs, &block) super(*args, **xargs, &block) + @default = default + @default = deserialize(@default) if @default.is_a?(::GQLParser::Token) end - def configure # :nodoc: - raise ArgumentError, 'Input fields can\'t be further configured using blocks' + # Prevent input fields from being further configured using a block + def configure + raise ArgumentError, +'Input fields can\'t be further configured using blocks' end # Allow change the default value for the input def apply_changes(**xargs, &block) @default = xargs[:default] if xargs.key?(:default) @@ -70,33 +71,33 @@ # Checks if the default value of the field is valid def validate!(*) super if defined? super - raise ArgumentError, <<~MSG.squish unless type_klass.input_type? + raise ArgumentError, (+<<~MSG).squish unless type_klass.input_type? The "#{type_klass.gql_name}" is not a valid input type. MSG - raise ArgumentError, <<~MSG.squish unless default.nil? || valid_input?(default) + raise ArgumentError, (+<<~MSG).squish unless default.nil? || valid_input?(default) The given default value "#{default.inspect}" is not valid for this field. MSG end protected # Check if the given +value+ is a valid array as input def valid_input_array?(value, deep) - return false unless value.is_a?(Array) + return false unless value.is_a?(::Array) value.all? do |val| (val.nil? && nullable?) || (leaf_type? || !deep) || type_klass.valid_input?(val) end end # Display the default value when it is present for inspection def inspect_default_value - " = #{to_hash.inspect}" if default? + +" = #{as_json.inspect}" if default_value? end end end end