| 1 |
from typing import Any, Dict, List, Set |
| 2 |
|
| 3 |
from ...error import GraphQLError |
| 4 |
from ...language import FragmentDefinitionNode, FragmentSpreadNode, VisitorAction, SKIP |
| 5 |
from . import ASTValidationContext, ASTValidationRule |
| 6 |
|
| 7 |
__all__ = ["NoFragmentCyclesRule"] |
| 8 |
|
| 9 |
|
| 10 |
class NoFragmentCyclesRule(ASTValidationRule): |
| 11 |
"""No fragment cycles |
| 12 |
|
| 13 |
The graph of fragment spreads must not form any cycles including spreading itself. |
| 14 |
Otherwise an operation could infinitely spread or infinitely execute on cycles in |
| 15 |
the underlying data. |
| 16 |
|
| 17 |
See https://spec.graphql.org/draft/#sec-Fragment-spreads-must-not-form-cycles |
| 18 |
""" |
| 19 |
|
| 20 |
def __init__(self, context: ASTValidationContext): |
| 21 |
super().__init__(context) |
| 22 |
# Tracks already visited fragments to maintain O(N) and to ensure that |
| 23 |
# cycles are not redundantly reported. |
| 24 |
self.visited_frags: Set[str] = set() |
| 25 |
# List of AST nodes used to produce meaningful errors |
| 26 |
self.spread_path: List[FragmentSpreadNode] = [] |
| 27 |
# Position in the spread path |
| 28 |
self.spread_path_index_by_name: Dict[str, int] = {} |
| 29 |
|
| 30 |
@staticmethod |
| 31 |
def enter_operation_definition(*_args: Any) -> VisitorAction: |
| 32 |
return SKIP |
| 33 |
|
| 34 |
def enter_fragment_definition( |
| 35 |
self, node: FragmentDefinitionNode, *_args: Any |
| 36 |
) -> VisitorAction: |
| 37 |
self.detect_cycle_recursive(node) |
| 38 |
return SKIP |
| 39 |
|
| 40 |
def detect_cycle_recursive(self, fragment: FragmentDefinitionNode) -> None: |
| 41 |
# This does a straight-forward DFS to find cycles. |
| 42 |
# It does not terminate when a cycle was found but continues to explore |
| 43 |
# the graph to find all possible cycles. |
| 44 |
if fragment.name.value in self.visited_frags: |
| 45 |
return |
| 46 |
|
| 47 |
fragment_name = fragment.name.value |
| 48 |
visited_frags = self.visited_frags |
| 49 |
visited_frags.add(fragment_name) |
| 50 |
|
| 51 |
spread_nodes = self.context.get_fragment_spreads(fragment.selection_set) |
| 52 |
if not spread_nodes: |
| 53 |
return |
| 54 |
|
| 55 |
spread_path = self.spread_path |
| 56 |
spread_path_index = self.spread_path_index_by_name |
| 57 |
spread_path_index[fragment_name] = len(spread_path) |
| 58 |
get_fragment = self.context.get_fragment |
| 59 |
|
| 60 |
for spread_node in spread_nodes: |
| 61 |
spread_name = spread_node.name.value |
| 62 |
cycle_index = spread_path_index.get(spread_name) |
| 63 |
|
| 64 |
spread_path.append(spread_node) |
| 65 |
if cycle_index is None: |
| 66 |
spread_fragment = get_fragment(spread_name) |
| 67 |
if spread_fragment: |
| 68 |
self.detect_cycle_recursive(spread_fragment) |
| 69 |
else: |
| 70 |
cycle_path = spread_path[cycle_index:] |
| 71 |
via_path = ", ".join("'" + s.name.value + "'" for s in cycle_path[:-1]) |
| 72 |
self.report_error( |
| 73 |
GraphQLError( |
| 74 |
f"Cannot spread fragment '{spread_name}' within itself" |
| 75 |
+ (f" via {via_path}." if via_path else "."), |
| 76 |
cycle_path, |
| 77 |
) |
| 78 |
) |
| 79 |
spread_path.pop() |
| 80 |
|
| 81 |
del spread_path_index[fragment_name] |