Skip to content

Commit 7e504ca

Browse files
committed
ASTValidation covers all validation rules that do not use Schema
Replicates graphql/graphql-js@93efa96
1 parent 80c5571 commit 7e504ca

File tree

3 files changed

+46
-46
lines changed

3 files changed

+46
-46
lines changed

graphql/validation/rules/no_fragment_cycles.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from ...error import GraphQLError
44
from ...language import FragmentDefinitionNode, FragmentSpreadNode
5-
from . import ValidationContext, ValidationRule
5+
from . import ASTValidationContext, ASTValidationRule
66

77
__all__ = ["NoFragmentCyclesRule", "cycle_error_message"]
88

@@ -12,10 +12,10 @@ def cycle_error_message(frag_name: str, spread_names: List[str]) -> str:
1212
return f"Cannot spread fragment '{frag_name}' within itself{via}."
1313

1414

15-
class NoFragmentCyclesRule(ValidationRule):
15+
class NoFragmentCyclesRule(ASTValidationRule):
1616
"""No fragment cycles"""
1717

18-
def __init__(self, context: ValidationContext) -> None:
18+
def __init__(self, context: ASTValidationContext) -> None:
1919
super().__init__(context)
2020
# Tracks already visited fragments to maintain O(N) and to ensure that
2121
# cycles are not redundantly reported.

graphql/validation/rules/no_unused_fragments.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from ...error import GraphQLError
44
from ...language import FragmentDefinitionNode, OperationDefinitionNode
5-
from . import ValidationContext, ValidationRule
5+
from . import ASTValidationContext, ASTValidationRule
66

77
__all__ = ["NoUnusedFragmentsRule", "unused_fragment_message"]
88

@@ -11,14 +11,14 @@ def unused_fragment_message(frag_name: str) -> str:
1111
return f"Fragment '{frag_name}' is never used."
1212

1313

14-
class NoUnusedFragmentsRule(ValidationRule):
14+
class NoUnusedFragmentsRule(ASTValidationRule):
1515
"""No unused fragments
1616
1717
A GraphQL document is only valid if all fragment definitions are spread within
1818
operations, or spread within other fragments spread within operations.
1919
"""
2020

21-
def __init__(self, context: ValidationContext) -> None:
21+
def __init__(self, context: ASTValidationContext) -> None:
2222
super().__init__(context)
2323
self.operation_defs: List[OperationDefinitionNode] = []
2424
self.fragment_defs: List[FragmentDefinitionNode] = []

graphql/validation/validation_context.py

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -67,51 +67,14 @@ class ASTValidationContext:
6767
def __init__(self, ast: DocumentNode) -> None:
6868
self.document = ast
6969
self.errors = []
70-
71-
def report_error(self, error: GraphQLError):
72-
self.errors.append(error)
73-
74-
75-
class SDLValidationContext(ASTValidationContext):
76-
"""Utility class providing a context for validation of an SDL ast.
77-
78-
An instance of this class is passed as the context attribute to all Validators,
79-
allowing access to commonly useful contextual information from within a validation
80-
rule.
81-
"""
82-
83-
schema: Optional[GraphQLSchema]
84-
85-
def __init__(self, ast: DocumentNode, schema: GraphQLSchema = None) -> None:
86-
super().__init__(ast)
87-
self.schema = schema
88-
89-
90-
class ValidationContext(ASTValidationContext):
91-
"""Utility class providing a context for validation using a GraphQL schema.
92-
93-
An instance of this class is passed as the context attribute to all Validators,
94-
allowing access to commonly useful contextual information from within a validation
95-
rule.
96-
"""
97-
98-
schema: GraphQLSchema
99-
100-
def __init__(
101-
self, schema: GraphQLSchema, ast: DocumentNode, type_info: TypeInfo
102-
) -> None:
103-
super().__init__(ast)
104-
self.schema = schema
105-
self._type_info = type_info
10670
self._fragments: Optional[Dict[str, FragmentDefinitionNode]] = None
10771
self._fragment_spreads: Dict[SelectionSetNode, List[FragmentSpreadNode]] = {}
10872
self._recursively_referenced_fragments: Dict[
10973
OperationDefinitionNode, List[FragmentDefinitionNode]
11074
] = {}
111-
self._variable_usages: Dict[NodeWithSelectionSet, List[VariableUsage]] = {}
112-
self._recursive_variable_usages: Dict[
113-
OperationDefinitionNode, List[VariableUsage]
114-
] = {}
75+
76+
def report_error(self, error: GraphQLError):
77+
self.errors.append(error)
11578

11679
def get_fragment(self, name: str) -> Optional[FragmentDefinitionNode]:
11780
fragments = self._fragments
@@ -172,6 +135,43 @@ def get_recursively_referenced_fragments(
172135
self._recursively_referenced_fragments[operation] = fragments
173136
return fragments
174137

138+
139+
class SDLValidationContext(ASTValidationContext):
140+
"""Utility class providing a context for validation of an SDL ast.
141+
142+
An instance of this class is passed as the context attribute to all Validators,
143+
allowing access to commonly useful contextual information from within a validation
144+
rule.
145+
"""
146+
147+
schema: Optional[GraphQLSchema]
148+
149+
def __init__(self, ast: DocumentNode, schema: GraphQLSchema = None) -> None:
150+
super().__init__(ast)
151+
self.schema = schema
152+
153+
154+
class ValidationContext(ASTValidationContext):
155+
"""Utility class providing a context for validation using a GraphQL schema.
156+
157+
An instance of this class is passed as the context attribute to all Validators,
158+
allowing access to commonly useful contextual information from within a validation
159+
rule.
160+
"""
161+
162+
schema: GraphQLSchema
163+
164+
def __init__(
165+
self, schema: GraphQLSchema, ast: DocumentNode, type_info: TypeInfo
166+
) -> None:
167+
super().__init__(ast)
168+
self.schema = schema
169+
self._type_info = type_info
170+
self._variable_usages: Dict[NodeWithSelectionSet, List[VariableUsage]] = {}
171+
self._recursive_variable_usages: Dict[
172+
OperationDefinitionNode, List[VariableUsage]
173+
] = {}
174+
175175
def get_variable_usages(self, node: NodeWithSelectionSet) -> List[VariableUsage]:
176176
usages = self._variable_usages.get(node)
177177
if usages is None:

0 commit comments

Comments
 (0)