|
2 | 2 | require "spec_helper" |
3 | 3 |
|
4 | 4 | class ExecutionInputValuesTest < Minitest::Test |
5 | | - TEST_SCHEMA = GraphQL::Schema.from_definition(%| |
6 | | - enum TestStatus { |
7 | | - ACTIVE |
8 | | - INACTIVE |
9 | | - } |
| 5 | + class TestSchema < GraphQL::Schema |
| 6 | + class TestStatus < GraphQL::Schema::Enum |
| 7 | + value :ACTIVE |
| 8 | + value :INACTIVE |
| 9 | + end |
10 | 10 |
|
11 | | - input TestInput { |
12 | | - string: String |
13 | | - float: Float |
14 | | - int: Int |
15 | | - enum: TestStatus |
16 | | - } |
| 11 | + class TestInput < GraphQL::Schema::InputObject |
| 12 | + argument :string, String, required: false |
| 13 | + argument :float, Float, required: false |
| 14 | + argument :int, Int, required: false |
| 15 | + argument :enum, TestStatus, required: false |
| 16 | + end |
17 | 17 |
|
18 | | - type Mutation { |
19 | | - testInput(input: TestInput): Boolean |
20 | | - } |
| 18 | + class Mutation < GraphQL::Schema::Object |
| 19 | + field :test_input, Boolean do |
| 20 | + argument :input, TestInput, required: false |
| 21 | + end |
21 | 22 |
|
22 | | - type Query { |
23 | | - ping: Boolean |
24 | | - } |
25 | | - |) |
| 23 | + field :test_list_input, Boolean do |
| 24 | + argument :input, [TestInput, null: true], required: false |
| 25 | + end |
| 26 | + end |
26 | 27 |
|
| 28 | + mutation(Mutation) |
| 29 | + query(Mutation) # Just to have something |
| 30 | + end |
27 | 31 |
|
28 | 32 | class DummyRunner |
29 | 33 | def add_step(s); end |
30 | | - def schema; TEST_SCHEMA; end |
| 34 | + def schema; TestSchema; end |
31 | 35 | end |
32 | 36 |
|
33 | | - def get_input_values(variables_string: nil, variables: nil) |
34 | | - query_str = "query#{variables_string ? "(#{variables_string})" : ""} { __typename }" |
35 | | - query = GraphQL::Query.new(TEST_SCHEMA, query_str, validate: false, variables: variables) |
| 37 | + def get_input_values(query_string: nil, variables_string: nil, variables: nil) |
| 38 | + query_string ||= "query#{variables_string ? "(#{variables_string})" : ""} { __typename }" |
| 39 | + query = GraphQL::Query.new(TestSchema, query_string, validate: false, variables: variables) |
36 | 40 | GraphQL::Execution::InputValues.new(query, DummyRunner.new) |
37 | 41 | end |
38 | 42 |
|
@@ -67,6 +71,38 @@ def test_it_produces_argument_values_for_simple_scalars |
67 | 71 |
|
68 | 72 | def test_it_produces_argument_values_for_input_objects |
69 | 73 | input = get_input_values |
70 | | - assert_equal({input: { string: "a", enum: "ACTIVE" } }, input.argument_values(TEST_SCHEMA.find("Mutation.testInput"), get_argument_nodes("input: { string: \"a\", enum: ACTIVE }"), nil)) |
| 74 | + assert_equal_input( {input: { string: "a", enum: "ACTIVE" } }, input.argument_values(TestSchema.find("Mutation.testInput"), get_argument_nodes("input: { string: \"a\", enum: ACTIVE }"), nil)) |
| 75 | + end |
| 76 | + |
| 77 | + def assert_equal_input(expected_ruby_hash, graphql_input, path = []) |
| 78 | + case expected_ruby_hash |
| 79 | + when Array |
| 80 | + assert_instance_of Array, graphql_input, "Matches at `#{path.join(".")}`" |
| 81 | + expected_ruby_hash.each_with_index do |next_expected, idx| |
| 82 | + assert_equal_input(next_expected, graphql_input[idx], path + [idx]) |
| 83 | + end |
| 84 | + when Hash |
| 85 | + if path.empty? |
| 86 | + assert_instance_of Hash, graphql_input, "Matches at `#{path.join(".")}`" |
| 87 | + else |
| 88 | + assert_kind_of GraphQL::Schema::InputObject, graphql_input, "Matches at `#{path.join(".")}`" |
| 89 | + graphql_input = graphql_input.to_h |
| 90 | + end |
| 91 | + expected_ruby_hash.each do |k, v| |
| 92 | + assert_equal_input(v, graphql_input[k], path + [k]) |
| 93 | + end |
| 94 | + else |
| 95 | + assert_equal expected_ruby_hash, graphql_input, "Matches at `#{path.join(".")}`" |
| 96 | + end |
| 97 | + end |
| 98 | + |
| 99 | + def test_it_works_with_arrays_of_input_objects |
| 100 | + input = get_input_values(variables_string: "$string: String = \"abc\", $string2: String, $input: TestInput!", variables: { string2: "xyz", input: { string: "nested" }}) |
| 101 | + assert_equal_input({input: [{}]}, input.argument_values(TestSchema.find("Mutation.testListInput"), get_argument_nodes("input: { string: $s }"), nil)) |
| 102 | + assert_equal_input({input: [{ string: "Str" }]}, input.argument_values(TestSchema.find("Mutation.testListInput"), get_argument_nodes("input: { string: \"Str\" }"), nil)) |
| 103 | + assert_equal_input({input: [{ string: "abc" }]}, input.argument_values(TestSchema.find("Mutation.testListInput"), get_argument_nodes("input: { string: $string }"), nil)) |
| 104 | + assert_equal_input({input: [{ string: "xyz" }]}, input.argument_values(TestSchema.find("Mutation.testListInput"), get_argument_nodes("input: { string: $string2 }"), nil)) |
| 105 | + assert_equal_input({input: [{ string: "nested" }]}, input.argument_values(TestSchema.find("Mutation.testListInput"), get_argument_nodes("input: $input"), nil)) |
| 106 | + assert_equal_input({input: [{}, {string: "Str"}, {string: "abc"}, {string: "xyz"}, {string: "nested"}]}, input.argument_values(TestSchema.find("Mutation.testListInput"), get_argument_nodes("input: [{string: $s}, {string: \"Str\"}, {string: $string }, { string: $string2 }, $input]"), nil)) |
71 | 107 | end |
72 | 108 | end |
0 commit comments