Class: GraphQL::Query::Arguments
- Inherits:
-
Object
- Object
- GraphQL::Query::Arguments
- Extended by:
- Forwardable
- Includes:
- Dig
- Defined in:
- lib/graphql/query/arguments.rb
Overview
Defined Under Namespace
Classes: ArgumentValue
Constant Summary collapse
- NoArguments =
Class.new(self) do self.argument_definitions = [] end
- NO_ARGS =
NoArguments.new({}, context: nil, defaults_used: Set.new)
Class Attribute Summary collapse
-
.argument_definitions ⇒ Object
Returns the value of attribute argument_definitions.
Instance Attribute Summary collapse
-
#argument_values ⇒ Object
readonly
Returns the value of attribute argument_values.
Class Method Summary collapse
Instance Method Summary collapse
-
#[](key) ⇒ Object
The argument at that key.
-
#default_used?(key) ⇒ Boolean
True if the argument default was passed as the argument value to the resolver.
-
#each_value {|argument_value| ... } ⇒ Object
Access each key, value and type for the arguments in this set.
-
#initialize(values, context:, defaults_used:) ⇒ Arguments
constructor
A new instance of Arguments.
-
#key?(key) ⇒ Boolean
True if the argument was present in this field.
-
#prepare ⇒ Object
-
#to_h ⇒ Hash
Get the hash of all values, with stringified keys.
-
#to_kwargs ⇒ {Symbol=>Object}
(also: #to_hash)
Convert this instance into valid Ruby keyword arguments.
Methods included from Dig
Constructor Details
#initialize(values, context:, defaults_used:) ⇒ Arguments
Returns a new instance of Arguments
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/graphql/query/arguments.rb', line 42 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 |
Class Attribute Details
.argument_definitions ⇒ Object
Returns the value of attribute argument_definitions
104 105 106 |
# File 'lib/graphql/query/arguments.rb', line 104 def argument_definitions @argument_definitions end |
Instance Attribute Details
#argument_values ⇒ Object (readonly)
Returns the value of attribute argument_values
40 41 42 |
# File 'lib/graphql/query/arguments.rb', line 40 def argument_values @argument_values end |
Class Method Details
.construct_arguments_class(argument_owner) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/graphql/query/arguments.rb', line 11 def self.construct_arguments_class(argument_owner) argument_definitions = argument_owner.arguments argument_owner.arguments_class = Class.new(self) do 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) 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 |
Instance Method Details
#[](key) ⇒ Object
Returns the argument at that key
56 57 58 59 |
# File 'lib/graphql/query/arguments.rb', line 56 def [](key) key_s = key.is_a?(String) ? key : key.to_s @argument_values.fetch(key_s, NULL_ARGUMENT_VALUE).value end |
#default_used?(key) ⇒ Boolean
Returns true if the argument default was passed as the argument value to the resolver
70 71 72 73 |
# File 'lib/graphql/query/arguments.rb', line 70 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 |
#each_value {|argument_value| ... } ⇒ Object
Access each key, value and type for the arguments in this set.
97 98 99 100 101 |
# File 'lib/graphql/query/arguments.rb', line 97 def each_value @argument_values.each_value do |argument_value| yield(argument_value) end end |
#key?(key) ⇒ Boolean
Returns true if the argument was present in this field
63 64 65 66 |
# File 'lib/graphql/query/arguments.rb', line 63 def key?(key) key_s = key.is_a?(String) ? key : key.to_s @argument_values.key?(key_s) end |
#prepare ⇒ Object
90 91 92 |
# File 'lib/graphql/query/arguments.rb', line 90 def prepare self end |
#to_h ⇒ Hash
Get the hash of all values, with stringified keys
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/graphql/query/arguments.rb', line 77 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 |
#to_kwargs ⇒ {Symbol=>Object} Also known as: to_hash
Convert this instance into valid Ruby keyword arguments
115 116 117 118 119 120 121 122 123 |
# File 'lib/graphql/query/arguments.rb', line 115 def to_kwargs ruby_kwargs = {} keys.each do |key| ruby_kwargs[Schema::Member::BuildType.underscore(key).to_sym] = self[key] end ruby_kwargs end |