Class: GraphQL::InputObjectType
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
Attributes inherited from BaseType
#description, #name
Instance Method Summary
collapse
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
#define, #metadata, #redefine
#!
Constructor Details
44
45
46
|
# File 'lib/graphql/input_object_type.rb', line 44
def initialize
@arguments = {}
end
|
Instance Attribute Details
#arguments ⇒ Hash<String => GraphQL::Argument>
Also known as:
input_fields
40
41
42
|
# File 'lib/graphql/input_object_type.rb', line 40
def arguments
@arguments
end
|
37
38
39
|
# File 'lib/graphql/input_object_type.rb', line 37
def mutation
@mutation
end
|
Instance Method Details
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
# File 'lib/graphql/input_object_type.rb', line 80
def coerce_non_null_input(value)
input_values = {}
arguments.each do |input_key, input_field_defn|
field_value = value[input_key]
if value.key?(input_key)
coerced_value = input_field_defn.type.coerce_input(field_value)
else
coerced_value = input_field_defn.default_value
end
input_values[input_key] = coerced_value if coerced_value || value.key?(input_key)
end
GraphQL::Query::Arguments.new(input_values, argument_definitions: arguments)
end
|
#coerce_result(value) ⇒ Object
98
99
100
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/graphql/input_object_type.rb', line 98
def coerce_result(value)
value = value.reduce({}) { |memo, (k, v)| memo[k.to_s] = v; memo }
result = {}
arguments.each do |input_key, input_field_defn|
input_value = value[input_key]
result[input_key] = input_value.nil? ? nil : input_field_defn.type.coerce_result(input_value) if value.key?(input_key)
end
result
end
|
#initialize_copy(other) ⇒ Object
48
49
50
51
|
# File 'lib/graphql/input_object_type.rb', line 48
def initialize_copy(other)
super
@arguments = other.arguments.dup
end
|
#kind ⇒ Object
53
54
55
|
# File 'lib/graphql/input_object_type.rb', line 53
def kind
GraphQL::TypeKinds::INPUT_OBJECT
end
|
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/graphql/input_object_type.rb', line 57
def validate_non_null_input(input, warden)
result = GraphQL::Query::InputValidationResult.new
visible_arguments_map = warden.input_fields(self).reduce({}) { |m, f| m[f.name] = f; m}
input.each do |name, value|
if visible_arguments_map[name].nil?
result.add_problem("Field is not defined on #{self.name}", [name])
end
end
invalid_fields = visible_arguments_map.map do |name, field|
field_result = field.type.validate_input(input[name], warden)
if !field_result.valid?
result.merge_result!(name, field_result)
end
end
result
end
|