Skip to content

Commit 2de409f

Browse files
author
Robert Mosolgo
authored
Merge pull request rmosolgo#331 from rmosolgo/arg-type-info
feat(Query::Arguments) preserve underlying definitions with argument values
2 parents 6d1d4cb + 29c9c44 commit 2de409f

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

lib/graphql/query/arguments.rb

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,16 @@ def initialize(values, argument_definitions:)
1111
@argument_values = values.inject({}) do |memo, (inner_key, inner_value)|
1212
string_key = inner_key.to_s
1313
arg_defn = argument_definitions[string_key]
14-
memo[string_key] = wrap_value(inner_value, arg_defn.type)
14+
arg_value = wrap_value(inner_value, arg_defn.type)
15+
memo[string_key] = ArgumentValue.new(string_key, arg_value, arg_defn)
1516
memo
1617
end
1718
end
1819

1920
# @param key [String, Symbol] name or index of value to access
2021
# @return [Object] the argument at that key
2122
def [](key)
22-
@argument_values[key.to_s]
23+
@argument_values.fetch(key.to_s, NULL_ARGUMENT_VALUE).value
2324
end
2425

2526
# @param key [String, Symbol] name of value to access
@@ -36,8 +37,28 @@ def to_h
3637

3738
def_delegators :string_key_values, :keys, :values, :each
3839

40+
# Access each key, value and type for the arguments in this set.
41+
# @yield [argument_value] The {ArgumentValue} for each argument
42+
# @yieldparam argument_value [ArgumentValue]
43+
def each_value
44+
@argument_values.each_value do |argument_value|
45+
yield(argument_value)
46+
end
47+
end
48+
3949
private
4050

51+
class ArgumentValue
52+
attr_reader :key, :value, :definition
53+
def initialize(key, value, definition)
54+
@key = key
55+
@value = value
56+
@definition = definition
57+
end
58+
end
59+
60+
NULL_ARGUMENT_VALUE = ArgumentValue.new(nil, nil, nil)
61+
4162
def wrap_value(value, arg_defn_type)
4263
case value
4364
when Array

spec/graphql/query/arguments_spec.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,37 @@
4545
assert_equal({ a: 1, b: 2, c: { d: 3, e: 4 } }, arguments.to_h)
4646
end
4747

48+
it "yields key, value, and arg_defnition" do
49+
type_info = []
50+
arguments.each_value do |arg_value|
51+
value = arg_value.value.is_a?(GraphQL::Query::Arguments) ? arg_value.value.to_h : arg_value.value
52+
type_info << [arg_value.key, value, arg_value.definition.type.unwrap.name]
53+
end
54+
expected_type_info =[
55+
["a", 1, "Int"],
56+
["b", 2, "Int"],
57+
["c", { d: 3, e: 4 }, "TestInput1"],
58+
]
59+
assert_equal expected_type_info, type_info
60+
end
61+
62+
it "can be copied to a new Arguments instance" do
63+
transformed_args = {}
64+
types = {}
65+
arguments.each_value do |arg_value|
66+
transformed_args[arg_value.key.upcase] = arg_value.value
67+
types[arg_value.key.upcase] = arg_value.definition
68+
end
69+
70+
new_arguments = GraphQL::Query::Arguments.new(transformed_args, argument_definitions: types)
71+
expected_hash = {
72+
"A" => 1,
73+
"B" => 2,
74+
"C" => { d: 3 , e: 4 },
75+
}
76+
assert_equal expected_hash, new_arguments.to_h
77+
end
78+
4879
describe "nested hashes" do
4980
let(:input_type) {
5081
test_input_type = GraphQL::InputObjectType.define do
@@ -64,6 +95,19 @@
6495
end
6596
end
6697

98+
describe "#[]" do
99+
it "returns the value at that key" do
100+
assert_equal 1, arguments["a"]
101+
assert_equal 1, arguments[:a]
102+
assert arguments["c"].is_a?(GraphQL::Query::Arguments)
103+
end
104+
105+
it "returns nil for missing keys" do
106+
assert_equal nil, arguments["z"]
107+
assert_equal nil, arguments[7]
108+
end
109+
end
110+
67111
describe "#key?" do
68112
let(:arg_values) { [] }
69113
let(:schema) {

0 commit comments

Comments
 (0)