Class: GraphQL::Schema::InputObject
- Extended by:
- Forwardable, Member::AcceptsDefinition, Member::CachedGraphQLDefinition::DeprecatedToGraphQL, Member::HasArguments, Member::HasArguments::ArgumentObjectLoader, Member::HasValidators, Member::ValidatesInput
- Includes:
- Dig
- Defined in:
- lib/graphql/schema/input_object.rb
Constant Summary collapse
- INVALID_OBJECT_MESSAGE =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
"Expected %{object} to be a key-value object responding to `to_h` or `to_unsafe_h`."
Constants included from Member::HasArguments
Member::HasArguments::NO_ARGUMENTS
Constants included from FindInheritedValue::EmptyObjects
FindInheritedValue::EmptyObjects::EMPTY_ARRAY, FindInheritedValue::EmptyObjects::EMPTY_HASH
Constants included from Member::HasDirectives
Member::HasDirectives::NO_DIRECTIVES
Constants included from Member::GraphQLTypeNames
Member::GraphQLTypeNames::Boolean, Member::GraphQLTypeNames::ID, Member::GraphQLTypeNames::Int
Class Attribute Summary collapse
Instance Attribute Summary collapse
-
#arguments ⇒ GraphQL::Query::Arguments, GraphQL::Execution::Interpereter::Arguments
readonly
The underlying arguments instance.
-
#context ⇒ GraphQL::Query::Context
readonly
The context for this query.
Class Method Summary collapse
-
.argument(*args, **kwargs, &block) ⇒ Object
-
.authorized?(obj, value, ctx) ⇒ Boolean
-
.coerce_input(value, ctx) ⇒ Object
-
.coerce_result(value, ctx) ⇒ Object
It’s funny to think of a result of an input object.
-
.kind ⇒ Object
-
.to_graphql ⇒ Object
-
.validate_non_null_input(input, ctx) ⇒ Object
Instance Method Summary collapse
-
#[](key) ⇒ Object
Lookup a key on this object, it accepts new-style underscored symbols Or old-style camelized identifiers.
-
#initialize(arguments = nil, ruby_kwargs: nil, context:, defaults_used:) ⇒ InputObject
constructor
A new instance of InputObject.
-
#key?(key) ⇒ Boolean
-
#prepare ⇒ Object
-
#to_h ⇒ Object
-
#to_hash ⇒ Object
-
#to_kwargs ⇒ Object
A copy of the Ruby-style hash.
-
#unwrap_value(value) ⇒ Object
Methods included from Member::HasArguments
add_argument, all_argument_definitions, argument, argument_class, arguments_statically_coercible?, coerce_arguments, get_argument, own_arguments, validate_directive_argument
Methods included from Member::HasArguments::ArgumentObjectLoader
authorize_application_object, load_and_authorize_application_object, load_application_object, load_application_object_failed, object_from_id
Methods included from Member::ValidatesInput
coerce_isolated_input, coerce_isolated_result, valid_input?, valid_isolated_input?, validate_input
Methods included from Member::HasValidators
Methods included from Member::CachedGraphQLDefinition::DeprecatedToGraphQL
Methods included from Dig
Methods included from Member::CachedGraphQLDefinition
#deprecated_to_graphql, #graphql_definition, #initialize_copy, #type_class
Methods included from Relay::TypeExtensions
#connection_type, #define_connection, #define_edge, #edge_type
Methods included from Member::BaseDSLMethods
#accessible?, #authorized?, #default_graphql_name, #description, #graphql_name, #introspection, #introspection?, #mutation, #name, #overridden_graphql_name, #to_graphql, #visible?
Methods included from Member::BaseDSLMethods::ConfigurationExtension
Methods included from Member::TypeSystemHelpers
#kind, #list?, #non_null?, #to_list_type, #to_non_null_type, #to_type_signature
Methods included from Member::Scoped
Methods included from Member::RelayShortcuts
#connection_type, #connection_type_class, #edge_type, #edge_type_class
Methods included from Member::HasPath
Methods included from Member::HasAstNode
Methods included from Member::HasDirectives
#directive, #directives, #remove_directive
Constructor Details
#initialize(arguments = nil, ruby_kwargs: nil, context:, defaults_used:) ⇒ InputObject
Returns a new instance of InputObject.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/graphql/schema/input_object.rb', line 22 def initialize(arguments = nil, ruby_kwargs: nil, context:, defaults_used:) @context = context if ruby_kwargs @ruby_style_hash = ruby_kwargs @arguments = arguments else @arguments = self.class.arguments_class.new(arguments, context: context, defaults_used: defaults_used) # Symbolized, underscored hash: @ruby_style_hash = @arguments.to_kwargs end # Apply prepares, not great to have it duplicated here. maybe_lazies = [] self.class.arguments(context).each_value do |arg_defn| ruby_kwargs_key = arg_defn.keyword if @ruby_style_hash.key?(ruby_kwargs_key) loads = arg_defn.loads # Resolvers do this loading themselves; # With the interpreter, it's done during `coerce_arguments` if loads && !arg_defn.from_resolver? && !context.interpreter? value = @ruby_style_hash[ruby_kwargs_key] loaded_value = arg_defn.(self, value, context) maybe_lazies << context.schema.after_lazy(loaded_value) do |loaded_value| overwrite_argument(ruby_kwargs_key, loaded_value) end end # Weirdly, procs are applied during coercion, but not methods. # Probably because these methods require a `self`. if arg_defn.prepare.is_a?(Symbol) || context.nil? || !context.interpreter? prepared_value = arg_defn.prepare_value(self, @ruby_style_hash[ruby_kwargs_key]) overwrite_argument(ruby_kwargs_key, prepared_value) end end end @maybe_lazies = maybe_lazies end |
Class Attribute Details
.arguments_class ⇒ Class<GraphQL::Arguments>
136 137 138 |
# File 'lib/graphql/schema/input_object.rb', line 136 def arguments_class @arguments_class end |
Instance Attribute Details
#arguments ⇒ GraphQL::Query::Arguments, GraphQL::Execution::Interpereter::Arguments (readonly)
Returns The underlying arguments instance.
17 18 19 |
# File 'lib/graphql/schema/input_object.rb', line 17 def arguments @arguments end |
#context ⇒ GraphQL::Query::Context (readonly)
Returns The context for this query.
15 16 17 |
# File 'lib/graphql/schema/input_object.rb', line 15 def context @context end |
Class Method Details
.argument(*args, **kwargs, &block) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/graphql/schema/input_object.rb', line 138 def argument(*args, **kwargs, &block) argument_defn = super(*args, **kwargs, &block) # Add a method access method_name = argument_defn.keyword class_eval <<-RUBY, __FILE__, __LINE__ def #{method_name} self[#{method_name.inspect}] end RUBY argument_defn end |
.authorized?(obj, value, ctx) ⇒ Boolean
82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/graphql/schema/input_object.rb', line 82 def self.(obj, value, ctx) # Authorize each argument (but this doesn't apply if `prepare` is implemented): if value.is_a?(InputObject) arguments(ctx).each do |_name, input_obj_arg| input_obj_arg = input_obj_arg.type_class if value.key?(input_obj_arg.keyword) && !input_obj_arg.(obj, value[input_obj_arg.keyword], ctx) return false end end end # It didn't early-return false: true end |
.coerce_input(value, ctx) ⇒ Object
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/graphql/schema/input_object.rb', line 219 def coerce_input(value, ctx) if value.nil? return nil end arguments = coerce_arguments(nil, value, ctx) ctx.schema.after_lazy(arguments) do |resolved_arguments| if resolved_arguments.is_a?(GraphQL::Error) raise resolved_arguments else input_obj_instance = self.new(resolved_arguments, ruby_kwargs: resolved_arguments.keyword_arguments, context: ctx, defaults_used: nil) input_obj_instance.prepare end end end |
.coerce_result(value, ctx) ⇒ Object
It’s funny to think of a result of an input object. This is used for rendering the default value in introspection responses.
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/graphql/schema/input_object.rb', line 238 def coerce_result(value, ctx) # Allow the application to provide values as :snake_symbols, and convert them to the camelStrings value = value.reduce({}) { |memo, (k, v)| memo[Member::BuildType.camelize(k.to_s)] = v; memo } result = {} arguments(ctx).each do |input_key, input_field_defn| input_value = value[input_key] if value.key?(input_key) result[input_key] = if input_value.nil? nil else input_field_defn.type.coerce_result(input_value, ctx) end end end result end |
.kind ⇒ Object
169 170 171 |
# File 'lib/graphql/schema/input_object.rb', line 169 def kind GraphQL::TypeKinds::INPUT_OBJECT end |
.to_graphql ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/graphql/schema/input_object.rb', line 152 def to_graphql type_defn = GraphQL::InputObjectType.new type_defn.name = graphql_name type_defn.description = description type_defn.[:type_class] = self type_defn.mutation = mutation type_defn.ast_node = ast_node all_argument_definitions.each do |arg| type_defn.arguments[arg.graphql_definition(silence_deprecation_warning: true).name] = arg.graphql_definition(silence_deprecation_warning: true) # rubocop:disable Development/ContextIsPassedCop -- legacy-related end # Make a reference to a classic-style Arguments class self.arguments_class = GraphQL::Query::Arguments.construct_arguments_class(type_defn) # But use this InputObject class at runtime type_defn.arguments_class = self type_defn end |
.validate_non_null_input(input, ctx) ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/graphql/schema/input_object.rb', line 176 def validate_non_null_input(input, ctx) result = GraphQL::Query::InputValidationResult.new warden = ctx.warden if input.is_a?(Array) result.add_problem(INVALID_OBJECT_MESSAGE % { object: JSON.generate(input, quirks_mode: true) }) return result end if !(input.respond_to?(:to_h) || input.respond_to?(:to_unsafe_h)) # We're not sure it'll act like a hash, so reject it: result.add_problem(INVALID_OBJECT_MESSAGE % { object: JSON.generate(input, quirks_mode: true) }) return result end # Inject missing required arguments missing_required_inputs = self.arguments(ctx).reduce({}) do |m, (argument_name, argument)| if !input.key?(argument_name) && argument.type.non_null? && warden.get_argument(self, argument_name) m[argument_name] = nil end m end [input, missing_required_inputs].each do |args_to_validate| args_to_validate.each do |argument_name, value| argument = warden.get_argument(self, argument_name) # Items in the input that are unexpected unless argument result.add_problem("Field is not defined on #{self.graphql_name}", [argument_name]) next end # Items in the input that are expected, but have invalid values argument_result = argument.type.validate_input(value, ctx) result.merge_result!(argument_name, argument_result) unless argument_result.valid? end end result end |
Instance Method Details
#[](key) ⇒ Object
Lookup a key on this object, it accepts new-style underscored symbols Or old-style camelized identifiers.
115 116 117 118 119 120 121 122 123 |
# File 'lib/graphql/schema/input_object.rb', line 115 def [](key) if @ruby_style_hash.key?(key) @ruby_style_hash[key] elsif @arguments @arguments[key] else nil end end |
#key?(key) ⇒ Boolean
125 126 127 |
# File 'lib/graphql/schema/input_object.rb', line 125 def key?(key) @ruby_style_hash.key?(key) || (@arguments && @arguments.key?(key)) || false end |
#prepare ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/graphql/schema/input_object.rb', line 69 def prepare if @context @context.schema.after_any_lazies(@maybe_lazies) do object = @context[:current_object] # Pass this object's class with `as` so that messages are rendered correctly from inherited validators Schema::Validator.validate!(self.class.validators, object, @context, @ruby_style_hash, as: self.class) self end else self end end |
#to_h ⇒ Object
61 62 63 |
# File 'lib/graphql/schema/input_object.rb', line 61 def to_h unwrap_value(@ruby_style_hash) end |
#to_hash ⇒ Object
65 66 67 |
# File 'lib/graphql/schema/input_object.rb', line 65 def to_hash to_h end |
#to_kwargs ⇒ Object
A copy of the Ruby-style hash
130 131 132 |
# File 'lib/graphql/schema/input_object.rb', line 130 def to_kwargs @ruby_style_hash.dup end |
#unwrap_value(value) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/graphql/schema/input_object.rb', line 97 def unwrap_value(value) case value when Array value.map { |item| unwrap_value(item) } when Hash value.reduce({}) do |h, (key, value)| h.merge!(key => unwrap_value(value)) end when InputObject value.to_h else value end end |