Skip to content

Commit 4366a71

Browse files
authored
Merge pull request rmosolgo#4791 from rmosolgo/fix-variables-with-wrong-type
Fix for when variables are defined as object types
2 parents 51e8a29 + 84aac28 commit 4366a71

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

lib/graphql/query/variables.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def initialize(ctx, ast_variables, provided_variables)
2626
# - Then, fall back to the default value from the query string
2727
# If it's still nil, raise an error if it's required.
2828
variable_type = schema.type_from_ast(ast_variable.type, context: ctx)
29-
if variable_type.nil?
29+
if variable_type.nil? || !variable_type.unwrap.kind.input?
3030
# Pass -- it will get handled by a validator
3131
else
3232
variable_name = ast_variable.name
@@ -80,12 +80,12 @@ def deep_stringify(val)
8080
else
8181
val
8282
end
83-
end
83+
end
8484

8585
def add_max_errors_reached_message
8686
message = "Too many errors processing variables, max validation error limit reached. Execution aborted"
8787
validation_result = GraphQL::Query::InputValidationResult.from_problem(message)
88-
errors << GraphQL::Query::VariableValidationError.new(nil, nil, nil, validation_result, msg: message)
88+
errors << GraphQL::Query::VariableValidationError.new(nil, nil, nil, validation_result, msg: message)
8989
end
9090
end
9191
end

spec/graphql/query_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,22 @@ def self.after_query(q)
591591
end
592592
end
593593
end
594+
595+
describe "when given as an object type and accessed in ruby" do
596+
it "returns an error to the client and is an empty hash" do
597+
result = schema.execute(<<~GRAPHQL)
598+
query($ch: Cheese) {
599+
__typename
600+
}
601+
GRAPHQL
602+
expected_messages = [
603+
"Cheese isn't a valid input type (on $ch)",
604+
"Variable $ch is declared by anonymous query but not used",
605+
]
606+
assert_equal expected_messages, result["errors"].map { |err| err["message"] }
607+
assert_equal({}, result.query.variables.to_h)
608+
end
609+
end
594610
end
595611

596612
describe "max_depth" do

0 commit comments

Comments
 (0)