diff --git a/spec/graphql/schema/validator/allow_blank_validator_spec.rb b/spec/graphql/schema/validator/allow_blank_validator_spec.rb index 408b825264..4992e5fe99 100644 --- a/spec/graphql/schema/validator/allow_blank_validator_spec.rb +++ b/spec/graphql/schema/validator/allow_blank_validator_spec.rb @@ -20,9 +20,20 @@ end it "can be used standalone" do - schema = build_schema(String, { allow_blank: false }) + schema = build_schema(String, { allow_blank: false, allow_null: false }) result = schema.execute("query($str: String) { validated(value: $str) }", variables: { str: ValidatorHelpers::BlankString.new }) assert_nil result["data"]["validated"] assert_equal ["value can't be blank"], result["errors"].map { |e| e["message"] } + + result = schema.execute("query($str: String) { validated: validatedArgResolver(input: $str) }", variables: { str: "abc" }) + assert_equal "ABC", result["data"]["validated"] + + result = schema.execute("query($str: String) { validated: validatedArgResolver(input: $str) }", variables: { str: ValidatorHelpers::BlankString.new }) + assert_nil result.fetch("data") + assert_equal ["input can't be blank"], result["errors"].map { |e| e["message"] } + + # The validator doesn't run if the argument isn't present: + result = schema.execute("query($str: String) { validated: validatedArgResolver(input: $str) }", variables: { }) + assert_equal "NO_INPUT", result["data"]["validated"] end end diff --git a/spec/graphql/schema/validator/validator_helpers.rb b/spec/graphql/schema/validator/validator_helpers.rb index 200388e54d..e247fee1e8 100644 --- a/spec/graphql/schema/validator/validator_helpers.rb +++ b/spec/graphql/schema/validator/validator_helpers.rb @@ -40,6 +40,15 @@ def resolve(a: 0, b: 0, c: 0) end end + validated_arg_resolver = Class.new(GraphQL::Schema::Resolver) do + graphql_name "ValidatedArgResolver" + argument :input, arg_type, required: false, validates: validates_config + type(String, null: false) + def resolve(input: :NO_INPUT) + input.to_s.upcase + end + end + query_type = Class.new(GraphQL::Schema::Object) do graphql_name "Query" field :validated, arg_type do @@ -69,7 +78,7 @@ def validated_input(input:) end field :validated_resolver, resolver: validated_resolver - + field :validated_arg_resolver, resolver: validated_arg_resolver field :list, [self], null: false def list