Skip to content
This repository was archived by the owner on Jun 13, 2025. It is now read-only.

Commit 5f6cb7e

Browse files
add types
1 parent 93053ff commit 5f6cb7e

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

graphql_api/tests/test_validation.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ def validate_query(query, *rules):
2323
return validate(schema, ast, rules=rules)
2424

2525

26-
# Tests for MaxDepthRule
2726
def test_max_depth_rule_allows_within_depth():
2827
query = """
2928
query {

graphql_api/validation.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
1+
from typing import Any, Type
2+
13
from graphql import GraphQLError, ValidationRule
24
from graphql.language.ast import DocumentNode, FieldNode, OperationDefinitionNode
5+
from graphql.validation import ValidationContext
36

47

5-
def create_max_depth_rule(max_depth: int):
8+
def create_max_depth_rule(max_depth: int) -> Type[ValidationRule]:
69
class MaxDepthRule(ValidationRule):
7-
def __init__(self, context):
10+
def __init__(self, context: ValidationContext) -> None:
811
super().__init__(context)
9-
self.operation_depth = 1
10-
self.max_depth_reached = False
11-
self.max_depth = max_depth
12+
self.operation_depth: int = 1
13+
self.max_depth_reached: bool = False
14+
self.max_depth: int = max_depth
1215

13-
def enter_operation_definition(self, node: OperationDefinitionNode, *_args):
16+
def enter_operation_definition(
17+
self, node: OperationDefinitionNode, *_args: Any
18+
) -> None:
1419
self.operation_depth = 1
1520
self.max_depth_reached = False
1621

17-
def enter_field(self, node: FieldNode, *_args):
22+
def enter_field(self, node: FieldNode, *_args: Any) -> None:
1823
self.operation_depth += 1
1924

2025
if self.operation_depth > self.max_depth and not self.max_depth_reached:
@@ -26,25 +31,25 @@ def enter_field(self, node: FieldNode, *_args):
2631
)
2732
)
2833

29-
def leave_field(self, node: FieldNode, *_args):
34+
def leave_field(self, node: FieldNode, *_args: Any) -> None:
3035
self.operation_depth -= 1
3136

3237
return MaxDepthRule
3338

3439

35-
def create_max_aliases_rule(max_aliases: int):
40+
def create_max_aliases_rule(max_aliases: int) -> Type[ValidationRule]:
3641
class MaxAliasesRule(ValidationRule):
37-
def __init__(self, context):
42+
def __init__(self, context: ValidationContext) -> None:
3843
super().__init__(context)
39-
self.alias_count = 0
40-
self.has_reported_error = False
41-
self.max_aliases = max_aliases
44+
self.alias_count: int = 0
45+
self.has_reported_error: bool = False
46+
self.max_aliases: int = max_aliases
4247

43-
def enter_document(self, node: DocumentNode, *_args):
48+
def enter_document(self, node: DocumentNode, *_args: Any) -> None:
4449
self.alias_count = 0
4550
self.has_reported_error = False
4651

47-
def enter_field(self, node: FieldNode, *_args):
52+
def enter_field(self, node: FieldNode, *_args: Any) -> None:
4853
if node.alias:
4954
self.alias_count += 1
5055

0 commit comments

Comments
 (0)