Skip to content

Commit

Permalink
Merge pull request #5030 from rmosolgo/remove-rule-for-no-non-null-de…
Browse files Browse the repository at this point in the history
…faults

Remove rule that prohibits non-null variables from having default values
  • Loading branch information
rmosolgo committed Jul 18, 2024
2 parents 9f44d5b + 2739f4e commit 18fdf6f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,27 @@ module VariableDefaultValuesAreCorrectlyTyped
def on_variable_definition(node, parent)
if !node.default_value.nil?
value = node.default_value
if node.type.is_a?(GraphQL::Language::Nodes::NonNullType)
add_error(GraphQL::StaticValidation::VariableDefaultValuesAreCorrectlyTypedError.new(
"Non-null variable $#{node.name} can't have a default value",
nodes: node,
name: node.name,
error_type: VariableDefaultValuesAreCorrectlyTypedError::VIOLATIONS[:INVALID_ON_NON_NULL]
))
type = context.schema.type_from_ast(node.type, context: context)
if type.nil?
# This is handled by another validator
else
type = context.schema.type_from_ast(node.type, context: context)
if type.nil?
# This is handled by another validator
else
validation_result = context.validate_literal(value, type)
validation_result = context.validate_literal(value, type)

if !validation_result.valid?
problems = validation_result.problems
first_problem = problems && problems.first
if first_problem
error_message = first_problem["explanation"]
end

error_message ||= "Default value for $#{node.name} doesn't match type #{type.to_type_signature}"
add_error(GraphQL::StaticValidation::VariableDefaultValuesAreCorrectlyTypedError.new(
error_message,
nodes: node,
name: node.name,
type: type.to_type_signature,
error_type: VariableDefaultValuesAreCorrectlyTypedError::VIOLATIONS[:INVALID_TYPE],
))
if !validation_result.valid?
problems = validation_result.problems
first_problem = problems && problems.first
if first_problem
error_message = first_problem["explanation"]
end

error_message ||= "Default value for $#{node.name} doesn't match type #{type.to_type_signature}"
add_error(GraphQL::StaticValidation::VariableDefaultValuesAreCorrectlyTypedError.new(
error_message,
nodes: node,
name: node.name,
type: type.to_type_signature,
error_type: VariableDefaultValuesAreCorrectlyTypedError::VIOLATIONS[:INVALID_TYPE],
))
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@
"path"=>["query getCheese"],
"extensions"=>{"code"=>"defaultValueInvalidType", "variableName"=>"badInput", "typeName"=>"DairyProductInput"}
},
{
"message"=>"Non-null variable $nonNull can't have a default value",
"locations"=>[{"line"=>8, "column"=>7}],
"path"=>["query getCheese"],
"extensions"=>{"code"=>"defaultValueInvalidOnNonNullVariable", "variableName"=>"nonNull"}
}
]
assert_equal(expected, errors)
end
Expand Down Expand Up @@ -115,16 +109,16 @@
it "finds errors" do
expected = [
{
"message"=>"Non-null variable $a can't have a default value",
"message"=>"Default value for $a doesn't match type Int!",
"locations"=>[{"line"=>3, "column"=>11}],
"path"=>["query getCheese"],
"extensions"=>{"code"=>"defaultValueInvalidOnNonNullVariable", "variableName"=>"a"}
"extensions"=> {"code"=>"defaultValueInvalidType", "variableName"=>"a", "typeName"=>"Int!"}
},
{
"message"=>"Non-null variable $b can't have a default value",
"message"=>"Default value for $b doesn't match type String!",
"locations"=>[{"line"=>4, "column"=>11}],
"path"=>["query getCheese"],
"extensions"=>{"code"=>"defaultValueInvalidOnNonNullVariable", "variableName"=>"b"}
"extensions"=>{"code"=>"defaultValueInvalidType", "variableName"=>"b", "typeName"=>"String!"}
},
{
"message"=>"Default value for $c doesn't match type ComplexInput",
Expand All @@ -133,7 +127,6 @@
"extensions"=>{"code"=>"defaultValueInvalidType", "variableName"=>"c", "typeName"=>"ComplexInput"}
}
]

assert_equal expected, errors
end
end
Expand Down

0 comments on commit 18fdf6f

Please sign in to comment.