Coverage for src/lexigram/graphql/schema/diff.py: 0%
29 statements
« prev ^ index » next coverage.py v7.15.4, created at 2026-08-25 04:37 +0800
« prev ^ index » next coverage.py v7.15.4, created at 2026-08-25 04:37 +0800
1"""GraphQL schema diffing utilities.
3This module provides schema comparison and diffing capabilities.
4"""
6from __future__ import annotations
8from dataclasses import dataclass, field
9from typing import Any
12@dataclass
13class SchemaDiff:
14 """Result of schema diff.
16 Attributes:
17 added: Set of added types/fields.
18 removed: Set of removed types/fields.
19 changed: Set of changed types/fields.
20 breaking: Whether there are breaking changes.
21 """
23 added: set[str] = field(default_factory=set)
24 removed: set[str] = field(default_factory=set)
25 changed: set[str] = field(default_factory=set)
26 breaking: bool = False
28 def has_changes(self) -> bool:
29 """Check if there are any changes."""
30 return bool(self.added or self.removed or self.changed)
33class SchemaDiffer:
34 """Compare two GraphQL schemas.
36 Example:
37 differ = SchemaDiffer()
38 diff = differ.diff(old_schema, new_schema)
40 if diff.breaking:
41 # Handle breaking changes
42 """
44 def diff(self, old_schema: Any, new_schema: Any) -> SchemaDiff:
45 """Compare two schemas.
47 Args:
48 old_schema: Original schema.
49 new_schema: New schema.
51 Returns:
52 SchemaDiff with changes.
53 """
54 diff = SchemaDiff()
56 # This is a simplified implementation
57 # Full implementation would compare types, fields, arguments, etc.
59 # Get types from both schemas
60 old_types = self._get_types(old_schema)
61 new_types = self._get_types(new_schema)
63 # Find added types
64 diff.removed = old_types - new_types
66 # Find removed types
67 diff.added = new_types - old_types
69 # Check for breaking changes
70 if diff.removed:
71 diff.breaking = True
73 return diff
75 def _get_types(self, schema: Any) -> set[str]:
76 """Get type names from schema."""
77 types = set()
79 if hasattr(schema, "types"):
80 for type_obj in schema.types:
81 if hasattr(type_obj, "name"):
82 types.add(type_obj.name)
84 return types
87__all__ = [
88 "SchemaDiff",
89 "SchemaDiffer",
90]