Class: Rails::GraphQL::Field::InputField
- Inherits:
-
Rails::GraphQL::Field
- Object
- Rails::GraphQL::Field
- Rails::GraphQL::Field::InputField
- Includes:
- TypedField
- Defined in:
- lib/rails/graphql/field/input_field.rb
Overview
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. Arguments can hold object-like values only when their type is associated with an InputField.
Options
-
:default
- Sets a default value for the argument (defaults to nil).
Instance Attribute Summary collapse
-
#default ⇒ Object
readonly
Returns the value of attribute default.
Attributes included from TypedField
Attributes inherited from Rails::GraphQL::Field
Instance Method Summary collapse
-
#apply_changes(**xargs, &block) ⇒ Object
Allow change the default value for the input.
-
#as_json(value = nil) ⇒ Object
A little override to use the default value.
-
#default_value? ⇒ Boolean
Checks if a default value was provided.
-
#deserialize(value = nil) ⇒ Object
Return the default value if the given
value
is nil. -
#initialize(*args, default: nil, **xargs, &block) ⇒ InputField
constructor
A new instance of InputField.
-
#to_json(value = nil) ⇒ Object
A little override to use the default value.
-
#valid_input?(value, deep: true) ⇒ Boolean
This checks if a given serialized value is valid for this field.
-
#validate! ⇒ Object
Checks if the default value of the field is valid.
Methods included from TypedField
#=~, #all_events, #all_listeners, #events?, #initialize_copy, #listeners?, #of_type?, #type_klass, #valid_field_types
Methods inherited from Rails::GraphQL::Field
#=~, #all_owners, #array?, #configure, #description, #disable!, #disabled?, #enable!, #enabled?, #initialize_copy, input_type?, #inspect, #internal?, leaf_type?, #method_name, mutation?, #null?, #nullable?, output_type?, proxy?, proxyable_methods, #required!, #required?, #required_items!, subscription?, #to_proxy, #valid?, #valid_output?
Methods included from Helpers::WithDescription
#desc, #description, #description=, #description?
Methods included from Helpers::WithDirectives
#all_directive_events, #all_directive_listeners, #directive_events?, #directive_listeners?, extended, included, #initialize_copy, #use, #using?
Constructor Details
#initialize(*args, default: nil, **xargs, &block) ⇒ InputField
Returns a new instance of InputField.
24 25 26 27 28 29 |
# File 'lib/rails/graphql/field/input_field.rb', line 24 def initialize(*args, default: nil, **xargs, &block) super(*args, **xargs, &block) @default = default @default = deserialize(@default) if @default.is_a?(::GQLParser::Token) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Rails::GraphQL::Field
Instance Attribute Details
#default ⇒ Object (readonly)
Returns the value of attribute default.
19 20 21 |
# File 'lib/rails/graphql/field/input_field.rb', line 19 def default @default end |
Instance Method Details
#apply_changes(**xargs, &block) ⇒ Object
Allow change the default value for the input
32 33 34 35 |
# File 'lib/rails/graphql/field/input_field.rb', line 32 def apply_changes(**xargs, &block) @default = xargs[:default] if xargs.key?(:default) super end |
#as_json(value = nil) ⇒ Object
A little override to use the default value
63 64 65 |
# File 'lib/rails/graphql/field/input_field.rb', line 63 def as_json(value = nil) super(value.nil? ? default : value) end |
#default_value? ⇒ Boolean
Checks if a default value was provided
38 39 40 |
# File 'lib/rails/graphql/field/input_field.rb', line 38 def default_value? !default.nil? end |
#deserialize(value = nil) ⇒ Object
Return the default value if the given value
is nil
53 54 55 |
# File 'lib/rails/graphql/field/input_field.rb', line 53 def deserialize(value = nil) value.nil? ? default : super end |
#to_json(value = nil) ⇒ Object
A little override to use the default value
58 59 60 |
# File 'lib/rails/graphql/field/input_field.rb', line 58 def to_json(value = nil) super(value.nil? ? default : value) end |
#valid_input?(value, deep: true) ⇒ Boolean
This checks if a given serialized value is valid for this field
43 44 45 46 47 48 49 50 |
# File 'lib/rails/graphql/field/input_field.rb', line 43 def valid_input?(value, deep: true) return false unless super return null? if value.nil? return valid_input_array?(value, deep) if array? return true unless leaf_type? || deep type_klass.valid_input?(value) end |
#validate! ⇒ Object
Checks if the default value of the field is valid
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/rails/graphql/field/input_field.rb', line 68 def validate!(*) super if defined? super 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) The given default value "#{default.inspect}" is not valid for this field. MSG end |