Skip to content

Commit 4070240

Browse files
committed
Incorrect validation errors when variable descriptions are used
Replicates graphql/graphql-js@3f8f27a3
1 parent 66adfb4 commit 4070240

3 files changed

Lines changed: 39 additions & 2 deletions

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
@@ -120,6 +120,11 @@ def enter_int_value(self, node: IntValueNode, *_args: Any) -> None:
120120
def enter_float_value(self, node: FloatValueNode, *_args: Any) -> None:
121121
self.is_valid_value_node(node)
122122

123+
# Descriptions are string values that would not validate according
124+
# to the below logic, but since (per the specification) descriptions must
125+
# not affect validation, they are ignored entirely when visiting the AST
126+
# and do not require special handling.
127+
# See https://spec.graphql.org/draft/#sec-Descriptions
123128
def enter_string_value(self, node: StringValueNode, *_args: Any) -> None:
124129
self.is_valid_value_node(node)
125130

src/graphql/validation/validate.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
from typing import Collection, List, Optional, Type
1+
from typing import Collection, Dict, List, Optional, Tuple, Type
22

33
from ..error import GraphQLError
44
from ..language import DocumentNode, ParallelVisitor, visit
5+
from ..language.ast import QUERY_DOCUMENT_KEYS
56
from ..pyutils import inspect, is_collection
67
from ..type import GraphQLSchema, assert_valid_schema
78
from ..utilities import TypeInfo, TypeInfoVisitor
@@ -16,6 +17,14 @@ class ValidationAbortedError(RuntimeError):
1617
"""Error when a validation has been aborted (error limit reached)."""
1718

1819

20+
# Per the specification, descriptions must not affect validation.
21+
# See https://spec.graphql.org/draft/#sec-Descriptions
22+
query_document_keys_to_validate: Dict[str, Tuple[str, ...]] = {
23+
kind: tuple(key for key in keys if key != "description")
24+
for kind, keys in QUERY_DOCUMENT_KEYS.items()
25+
}
26+
27+
1928
def validate(
2029
schema: GraphQLSchema,
2130
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
pass
89102
return errors

tests/validation/test_validation.py

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

193193
with raises(RuntimeError, match="^Error from custom rule!$"):
194194
validate(test_schema, doc, [CustomRule], max_errors=1)
195+
196+
197+
def describe_operation_and_variable_definition_descriptions():
198+
def validates_operation_with_description_and_variable_descriptions():
199+
schema = build_schema("type Query { field(a: Int, b: String): String }")
200+
query = '''
201+
"Operation description"
202+
query myQuery(
203+
"Variable a description"
204+
$a: Int,
205+
"""Variable b\nmultiline description"""
206+
$b: String
207+
) {
208+
field(a: $a, b: $b)
209+
}
210+
'''
211+
ast = parse(query)
212+
errors = validate(schema, ast)
213+
assert errors == []

0 commit comments

Comments
 (0)