Skip to content

Commit 9a10b47

Browse files
committed
Incorrect validation errors when variable descriptions are used
Replicates graphql/graphql-js@3f8f27a3
1 parent f305dcd commit 9a10b47

3 files changed

Lines changed: 38 additions & 1 deletion

File tree

src/graphql/validation/rules/values_of_correct_type.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ def enter_int_value(self, node: IntValueNode, *_args: Any) -> None:
129129
def enter_float_value(self, node: FloatValueNode, *_args: Any) -> None:
130130
self.is_valid_value_node(node)
131131

132+
# Descriptions are string values that would not validate according
133+
# to the below logic, but since (per the specification) descriptions must
134+
# not affect validation, they are ignored entirely when visiting the AST
135+
# and do not require special handling.
136+
# See https://spec.graphql.org/draft/#sec-Descriptions
132137
def enter_string_value(self, node: StringValueNode, *_args: Any) -> None:
133138
self.is_valid_value_node(node)
134139

src/graphql/validation/validate.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from ..error import GraphQLError
88
from ..language import DocumentNode, ParallelVisitor, visit
9+
from ..language.ast import QUERY_DOCUMENT_KEYS
910
from ..type import GraphQLSchema, assert_valid_schema
1011
from ..utilities import TypeInfo, TypeInfoVisitor
1112
from .specified_rules import specified_rules, specified_sdl_rules
@@ -34,6 +35,14 @@ class ValidationAbortedError(GraphQLError):
3435
)
3536

3637

38+
# Per the specification, descriptions must not affect validation.
39+
# See https://spec.graphql.org/draft/#sec-Descriptions
40+
query_document_keys_to_validate: dict[str, tuple[str, ...]] = {
41+
kind: tuple(key for key in keys if key != "description")
42+
for kind, keys in QUERY_DOCUMENT_KEYS.items()
43+
}
44+
45+
3746
def validate(
3847
schema: GraphQLSchema,
3948
document_ast: DocumentNode,
@@ -83,7 +92,11 @@ def on_error(error: GraphQLError) -> None:
8392

8493
# Visit the whole document with each instance of all provided rules.
8594
try:
86-
visit(document_ast, TypeInfoVisitor(type_info, ParallelVisitor(visitors)))
95+
visit(
96+
document_ast,
97+
TypeInfoVisitor(type_info, ParallelVisitor(visitors)),
98+
query_document_keys_to_validate,
99+
)
87100
except ValidationAbortedError:
88101
errors.append(validation_aborted_error)
89102
return errors

tests/validation/test_validation.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,22 @@ def enter_field(self, *_args):
165165

166166
with pytest.raises(RuntimeError, match=r"^Error from custom rule!$"):
167167
validate(test_schema, doc, [CustomRule], max_errors=1)
168+
169+
170+
def describe_operation_and_variable_definition_descriptions():
171+
def validates_operation_with_description_and_variable_descriptions():
172+
schema = build_schema("type Query { field(a: Int, b: String): String }")
173+
query = '''
174+
"Operation description"
175+
query myQuery(
176+
"Variable a description"
177+
$a: Int,
178+
"""Variable b\nmultiline description"""
179+
$b: String
180+
) {
181+
field(a: $a, b: $b)
182+
}
183+
'''
184+
ast = parse(query)
185+
errors = validate(schema, ast)
186+
assert errors == []

0 commit comments

Comments
 (0)