Skip to content

Commit 0d67023

Browse files
committed
Revert "Remove deprecated type_info argument of validate() function"
This reverts commit 2d90cc1. Replicates graphql/graphql-js@75635f0
1 parent 318fc43 commit 0d67023

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

src/graphql/validation/validate.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from ..error import GraphQLError
44
from ..language import DocumentNode, ParallelVisitor, visit
5-
from ..pyutils import is_collection
5+
from ..pyutils import inspect, is_collection
66
from ..type import GraphQLSchema, assert_valid_schema
77
from ..utilities import TypeInfo, TypeInfoVisitor
88
from .rules import ASTValidationRule
@@ -22,6 +22,7 @@ def validate(
2222
document_ast: DocumentNode,
2323
rules: Optional[Collection[Type[ASTValidationRule]]] = None,
2424
max_errors: Optional[int] = None,
25+
type_info: Optional[TypeInfo] = None,
2526
) -> List[GraphQLError]:
2627
"""Implements the "Validation" section of the spec.
2728
@@ -38,6 +39,8 @@ def validate(
3839
Validate will stop validation after a ``max_errors`` limit has been reached.
3940
Attackers can send pathologically invalid queries to induce a DoS attack,
4041
so by default ``max_errors`` set to 100 errors.
42+
43+
Providing a custom TypeInfo instance is deprecated and will be removed in v3.3.
4144
"""
4245
if not document_ast or not isinstance(document_ast, DocumentNode):
4346
raise TypeError("Must provide document.")
@@ -47,6 +50,10 @@ def validate(
4750
max_errors = 100
4851
elif not isinstance(max_errors, int):
4952
raise TypeError("The maximum number of errors must be passed as an int.")
53+
if type_info is None:
54+
type_info = TypeInfo(schema)
55+
elif not isinstance(type_info, TypeInfo):
56+
raise TypeError(f"Not a TypeInfo object: {inspect(type_info)}.")
5057
if rules is None:
5158
rules = specified_rules
5259
elif not is_collection(rules) or not all(
@@ -69,7 +76,6 @@ def on_error(error: GraphQLError) -> None:
6976
raise ValidationAbortedError
7077
errors.append(error)
7178

72-
type_info = TypeInfo(schema)
7379
context = ValidationContext(schema, document_ast, type_info, on_error)
7480

7581
# This uses a specialized visitor which runs multiple visitors in parallel,

tests/validation/test_validation.py

+40-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from graphql.error import GraphQLError
44
from graphql.language import parse
5-
from graphql.utilities import build_schema
5+
from graphql.utilities import TypeInfo, build_schema
66
from graphql.validation import ValidationRule, validate
77

88
from .harness import test_schema
@@ -15,6 +15,14 @@ def rejects_invalid_documents():
1515
assert validate(test_schema, None) # type: ignore
1616
assert str(exc_info.value) == "Must provide document."
1717

18+
def rejects_invalid_type_info():
19+
with raises(TypeError) as exc_info:
20+
# noinspection PyTypeChecker
21+
assert validate(
22+
test_schema, parse("query { name }"), type_info={} # type: ignore
23+
)
24+
assert str(exc_info.value) == "Not a TypeInfo object: {}."
25+
1826
def rejects_invalid_rules():
1927
with raises(TypeError) as exc_info:
2028
# noinspection PyTypeChecker
@@ -72,6 +80,37 @@ def detects_unknown_fields():
7280
{"message": "Cannot query field 'unknown' on type 'QueryRoot'."}
7381
]
7482

83+
def deprecated_validates_using_a_custom_type_info():
84+
# This TypeInfo will never return a valid field.
85+
type_info = TypeInfo(test_schema, None, lambda *args: None)
86+
87+
doc = parse(
88+
"""
89+
query {
90+
human {
91+
pets {
92+
... on Cat {
93+
meowsVolume
94+
}
95+
... on Dog {
96+
barkVolume
97+
}
98+
}
99+
}
100+
}
101+
"""
102+
)
103+
104+
errors = validate(test_schema, doc, None, None, type_info)
105+
106+
assert [error.message for error in errors] == [
107+
"Cannot query field 'human' on type 'QueryRoot'. Did you mean 'human'?",
108+
"Cannot query field 'meowsVolume' on type 'Cat'."
109+
" Did you mean 'meowsVolume'?",
110+
"Cannot query field 'barkVolume' on type 'Dog'."
111+
" Did you mean 'barkVolume'?",
112+
]
113+
75114
def validates_using_a_custom_rule():
76115
schema = build_schema(
77116
"""

0 commit comments

Comments
 (0)