spec/graphql/schema/resolver_spec.rb in graphql-1.9.7 vs spec/graphql/schema/resolver_spec.rb in graphql-1.9.8

- old
+ new

@@ -184,16 +184,24 @@ end class IntegerWrapper < GraphQL::Schema::Object implements HasValue field :value, Integer, null: false, method: :itself + + def self.authorized?(value, ctx) + if ctx[:max_value] && value > ctx[:max_value] + false + else + true + end + end end class PrepResolver9 < BaseResolver argument :int_id, ID, required: true, loads: HasValue # Make sure the lazy object is resolved properly: - type HasValue, null: false + type HasValue, null: true def object_from_id(type, id, ctx) # Make sure a lazy object is handled appropriately LazyBlock.new { # Make sure that the right type ends up here id.to_i + type.graphql_name.length @@ -553,10 +561,15 @@ res = exec_query("{ int: #{field_name}(int: 200) }") assert_nil res["data"].fetch("int"), "#{description}: No result for authorization error" refute res.key?("errors"), "#{description}: silent auth failure (no top-level error)" end + it "keeps track of the `loads:` option" do + arg = ResolverTest::MutationWithNullableLoadsArgument.arguments["labelId"] + assert_equal ResolverTest::HasValue, arg.loads + end + describe "ready?" do it "can raise errors" do res = exec_query("{ int: prepResolver5(int: 5) }") assert_equal 50, res["data"]["int"] add_error_assertions("prepResolver5", "ready?") @@ -654,9 +667,25 @@ end it "works with no arguments for RelayClassicMutation" do res = exec_query("{ prepResolver14(input: {}) { number } }") assert_equal 1, res["data"]["prepResolver14"]["number"] + end + + it "uses loaded objects" do + query_str = "{ prepResolver9(intId: 9) { value } }" + # This will cause an unauthorized response + # by `HasValue.authorized?` + context = { max_value: 8 } + res = exec_query(query_str, context: context) + assert_nil res["data"]["prepResolver9"] + # This is OK + context = { max_value: 900 } + res = exec_query(query_str, context: context) + assert_equal 51, res["data"]["prepResolver9"]["value"] + # This is the transformation applied by the resolver, + # just make sure it matches the response + assert_equal 51, (9 + "HasValue".size) * 3 end end end describe "Loading inputs" do