Skip to content

Commit

Permalink
Add some blank validator tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rmosolgo committed Oct 22, 2024
1 parent ec80ea5 commit 062d7b7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
13 changes: 12 additions & 1 deletion spec/graphql/schema/validator/allow_blank_validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 10 additions & 1 deletion spec/graphql/schema/validator/validator_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 062d7b7

Please sign in to comment.