Class: GraphQL::InputObjectType
- Defined in:
- lib/graphql/input_object_type.rb
Overview
InputObjectTypes are key-value inputs for fields.
Input objects have arguments which are identical to Field arguments. They map names to types and support default values. Their input types can be any input types, including InputObjectTypes.
In a ‘resolve` function, you can access the values by making nested lookups on `args`.
Instance Attribute Summary collapse
-
#arguments ⇒ Hash<String => GraphQL::Argument>
(also: #input_fields)
Map String argument names to their Argument implementations.
-
#mutation ⇒ GraphQL::Relay::Mutation?
The mutation this field was derived from, if it was derived from a mutation.
Instance Method Summary collapse
- #coerce_non_null_input(value) ⇒ Object
-
#initialize ⇒ InputObjectType
constructor
A new instance of InputObjectType.
- #kind ⇒ Object
- #validate_non_null_input(input) ⇒ Object
Methods inherited from BaseType
#==, #coerce_input, #connection_type, #define_connection, #define_edge, #edge_type, #get_field, resolve_related_type, #resolve_type, #to_list_type, #to_non_null_type, #to_s, #unwrap, #valid_input?, #validate_input
Methods included from Define::InstanceDefinable
#definition_proc=, included, #metadata
Methods included from Define::NonNullWithBang
Constructor Details
#initialize ⇒ InputObjectType
Returns a new instance of InputObjectType.
46 47 48 |
# File 'lib/graphql/input_object_type.rb', line 46 def initialize @arguments = {} end |
Instance Attribute Details
#arguments ⇒ Hash<String => GraphQL::Argument> Also known as: input_fields
Returns Map String argument names to their Argument implementations.
38 39 40 41 |
# File 'lib/graphql/input_object_type.rb', line 38 def arguments ensure_defined @arguments end |
#mutation ⇒ GraphQL::Relay::Mutation?
Returns The mutation this field was derived from, if it was derived from a mutation.
|
|
# File 'lib/graphql/input_object_type.rb', line 34
|
Instance Method Details
#coerce_non_null_input(value) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/graphql/input_object_type.rb', line 75 def coerce_non_null_input(value) input_values = {} arguments.each do |input_key, input_field_defn| field_value = value[input_key] field_value = input_field_defn.type.coerce_input(field_value) # Try getting the default value if field_value.nil? field_value = input_field_defn.default_value end if !field_value.nil? input_values[input_key] = field_value end end GraphQL::Query::Arguments.new(input_values) end |
#kind ⇒ Object
50 51 52 |
# File 'lib/graphql/input_object_type.rb', line 50 def kind GraphQL::TypeKinds::INPUT_OBJECT end |
#validate_non_null_input(input) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/graphql/input_object_type.rb', line 54 def validate_non_null_input(input) result = GraphQL::Query::InputValidationResult.new # Items in the input that are unexpected input.each do |name, value| if arguments[name].nil? result.add_problem("Field is not defined on #{self.name}", [name]) end end # Items in the input that are expected, but have invalid values invalid_fields = arguments.map do |name, field| field_result = field.type.validate_input(input[name]) if !field_result.valid? result.merge_result!(name, field_result) end end result end |