class GraphQL::Query::Arguments
Read-only access to values, normalizing all keys to strings
{Arguments} recursively wraps the input in {Arguments} instances.
Constants
- NO_ARGS
- NULL_ARGUMENT_VALUE
- NoArguments
Attributes
Public Class Methods
# File lib/graphql/query/arguments.rb, line 11 def self.construct_arguments_class(argument_owner) argument_definitions = argument_owner.arguments # rubocop:disable Development/ContextIsPassedCop -- legacy-related argument_owner.arguments_class = Class.new(self) do self.argument_owner = argument_owner self.argument_definitions = argument_definitions argument_definitions.each do |_arg_name, arg_definition| if arg_definition.method_access? expose_as = arg_definition.expose_as.to_s.freeze expose_as_underscored = GraphQL::Schema::Member::BuildType.underscore(expose_as).freeze method_names = [expose_as, expose_as_underscored].uniq method_names.each do |method_name| # Don't define a helper method if it would override something. if method_defined?(method_name) GraphQL::Deprecation.warn( "Unable to define a helper for argument with name '#{method_name}' "\ "as this is a reserved name. Add `method_access: false` to stop this warning." ) else define_method(method_name) do # Always use `expose_as` here, since #[] doesn't accept underscored names self[expose_as] end end end end end end end
# File lib/graphql/query/arguments.rb, line 43 def initialize(values, context:, defaults_used:) @argument_values = values.inject({}) do |memo, (inner_key, inner_value)| arg_name = inner_key.to_s arg_defn = self.class.argument_definitions[arg_name] || raise("Not found #{arg_name} among #{self.class.argument_definitions.keys}") arg_default_used = defaults_used.include?(arg_name) arg_value = wrap_value(inner_value, arg_defn.type, context) string_key = arg_defn.expose_as memo[string_key] = ArgumentValue.new(string_key, arg_value, arg_defn, arg_default_used) memo end end
Public Instance Methods
@param key [String, Symbol] name or index of value to access @return [Object] the argument at that key
# File lib/graphql/query/arguments.rb, line 57 def [](key) key_s = key.is_a?(String) ? key : key.to_s @argument_values.fetch(key_s, NULL_ARGUMENT_VALUE).value end
@param key [String, Symbol] name of value to access @return [Boolean] true if the argument default was passed as the argument value to the resolver
# File lib/graphql/query/arguments.rb, line 71 def default_used?(key) key_s = key.is_a?(String) ? key : key.to_s @argument_values.fetch(key_s, NULL_ARGUMENT_VALUE).default_used? end
Access each key, value and type for the arguments in this set. @yield [argument_value] The {ArgumentValue} for each argument @yieldparam argument_value [ArgumentValue]
# File lib/graphql/query/arguments.rb, line 99 def each_value @argument_values.each_value do |argument_value| yield(argument_value) end end
@param key [String, Symbol] name of value to access @return [Boolean] true if the argument was present in this field
# File lib/graphql/query/arguments.rb, line 64 def key?(key) key_s = key.is_a?(String) ? key : key.to_s @argument_values.key?(key_s) end
# File lib/graphql/query/arguments.rb, line 92 def prepare self end
Get the hash of all values, with stringified keys @return [Hash] the stringified hash
# File lib/graphql/query/arguments.rb, line 78 def to_h @to_h ||= begin h = {} each_value do |arg_value| arg_key = arg_value.definition.expose_as h[arg_key] = unwrap_value(arg_value.value) end h end end
Convert this instance into valid Ruby keyword arguments @return [{Symbol=>Object}]
# File lib/graphql/query/arguments.rb, line 117 def to_kwargs ruby_kwargs = {} keys.each do |key| ruby_kwargs[Schema::Member::BuildType.underscore(key).to_sym] = self[key] end ruby_kwargs end
Private Instance Methods
# File lib/graphql/query/arguments.rb, line 172 def unwrap_value(value) case value when Array value.map { |item| unwrap_value(item) } when Hash value.inject({}) do |memo, (key, value)| memo[key] = unwrap_value(value) memo end when GraphQL::Query::Arguments, GraphQL::Schema::InputObject value.to_h else value end end
# File lib/graphql/query/arguments.rb, line 150 def wrap_value(value, arg_defn_type, context) if value.nil? nil else case arg_defn_type when GraphQL::ListType value.map { |item| wrap_value(item, arg_defn_type.of_type, context) } when GraphQL::NonNullType wrap_value(value, arg_defn_type.of_type, context) when GraphQL::InputObjectType if value.is_a?(Hash) result = arg_defn_type.arguments_class.new(value, context: context, defaults_used: Set.new) result.prepare else value end else value end end end