Skip to content

Commit 89a1a96

Browse files
author
Robert Mosolgo
authored
Merge pull request rmosolgo#1401 from rmosolgo/friendlier-variables
Accept symbol-keyed hashes in variables
2 parents f67751d + c57ca53 commit 89a1a96

File tree

4 files changed

+64
-5
lines changed

4 files changed

+64
-5
lines changed

lib/graphql/query/variables.rb

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ class Variables
1313
def initialize(ctx, ast_variables, provided_variables)
1414
schema = ctx.schema
1515
@context = ctx
16-
@provided_variables = provided_variables
16+
17+
@provided_variables = deep_stringify(provided_variables)
1718
@errors = []
1819
@storage = ast_variables.each_with_object({}) do |ast_variable, memo|
1920
# Find the right value for this variable:
@@ -27,7 +28,7 @@ def initialize(ctx, ast_variables, provided_variables)
2728
variable_name = ast_variable.name
2829
default_value = ast_variable.default_value
2930
provided_value = @provided_variables[variable_name]
30-
value_was_provided = @provided_variables.key?(variable_name)
31+
value_was_provided = @provided_variables.key?(variable_name)
3132

3233
validation_result = variable_type.validate_input(provided_value, ctx)
3334
if !validation_result.valid?
@@ -45,6 +46,23 @@ def initialize(ctx, ast_variables, provided_variables)
4546
end
4647

4748
def_delegators :@storage, :length, :key?, :[], :fetch, :to_h
49+
50+
private
51+
52+
def deep_stringify(val)
53+
case val
54+
when Array
55+
val.map { |v| deep_stringify(v) }
56+
when Hash
57+
new_val = {}
58+
val.each do |k, v|
59+
new_val[k.to_s] = deep_stringify(v)
60+
end
61+
new_val
62+
else
63+
val
64+
end
65+
end
4866
end
4967
end
5068
end

spec/graphql/query/variables_spec.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,47 @@
4848
end
4949
end
5050

51+
describe "symbol keys" do
52+
let(:query_string) { <<-GRAPHQL
53+
query testVariables(
54+
$dairy_product_1: DairyProductInput!
55+
$dairy_product_2: DairyProductInput!
56+
) {
57+
__typename
58+
}
59+
GRAPHQL
60+
}
61+
62+
let(:provided_variables) {
63+
{
64+
dairy_product_1: { source: "COW", fatContent: 0.99 },
65+
"dairy_product_2" => { source: "DONKEY", "fatContent": 0.89 },
66+
}
67+
}
68+
69+
it "checks for string matches" do
70+
# These get merged into all the values above
71+
default_values = {
72+
"originDairy"=>"Sugar Hollow Dairy",
73+
"organic"=>false,
74+
"order_by"=>{"direction"=>"ASC"}
75+
}
76+
77+
expected_input_1 = {
78+
"source" => 1,
79+
"fatContent" => 0.99,
80+
}.merge(default_values)
81+
82+
assert_equal(expected_input_1, variables["dairy_product_1"].to_h)
83+
84+
expected_input_2 = {
85+
"source" => :donkey,
86+
"fatContent" => 0.89,
87+
}.merge(default_values)
88+
assert_equal(expected_input_2, variables["dairy_product_2"].to_h)
89+
end
90+
end
91+
5192
describe "validating input objects" do
5293
let(:query_string) {%|
5394
query searchMyDairy (

spec/graphql/schema/enum_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
it "works as input" do
6868
query_str = "query($family: Family!) { instruments(family: $family) { name } }"
6969
expected_names = ["Piano", "Organ"]
70-
result = Jazz::Schema.execute(query_str, variables: { "family" => "KEYS" })
70+
result = Jazz::Schema.execute(query_str, variables: { family: "KEYS" })
7171
assert_equal expected_names, result["data"]["instruments"].map { |i| i["name"] }
7272
end
7373
end

spec/graphql/schema/object_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,10 @@
141141
}
142142
GRAPHQL
143143

144-
res = Jazz::Schema.execute(mutation_str, variables: { "name" => "Miles Davis Quartet" })
144+
res = Jazz::Schema.execute(mutation_str, variables: { name: "Miles Davis Quartet" })
145145
new_id = res["data"]["addEnsemble"]["id"]
146146

147-
res2 = Jazz::Schema.execute(query_str, variables: { "id" => new_id })
147+
res2 = Jazz::Schema.execute(query_str, variables: { id: new_id })
148148
assert_equal "Miles Davis Quartet", res2["data"]["find"]["name"]
149149
end
150150

0 commit comments

Comments
 (0)