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

1"""GraphQL schema diffing utilities. 

2 

3This module provides schema comparison and diffing capabilities. 

4""" 

5 

6from __future__ import annotations 

7 

8from dataclasses import dataclass, field 

9from typing import Any 

10 

11 

12@dataclass 

13class SchemaDiff: 

14 """Result of schema diff. 

15 

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 """ 

22 

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 

27 

28 def has_changes(self) -> bool: 

29 """Check if there are any changes.""" 

30 return bool(self.added or self.removed or self.changed) 

31 

32 

33class SchemaDiffer: 

34 """Compare two GraphQL schemas. 

35 

36 Example: 

37 differ = SchemaDiffer() 

38 diff = differ.diff(old_schema, new_schema) 

39 

40 if diff.breaking: 

41 # Handle breaking changes 

42 """ 

43 

44 def diff(self, old_schema: Any, new_schema: Any) -> SchemaDiff: 

45 """Compare two schemas. 

46 

47 Args: 

48 old_schema: Original schema. 

49 new_schema: New schema. 

50 

51 Returns: 

52 SchemaDiff with changes. 

53 """ 

54 diff = SchemaDiff() 

55 

56 # This is a simplified implementation 

57 # Full implementation would compare types, fields, arguments, etc. 

58 

59 # Get types from both schemas 

60 old_types = self._get_types(old_schema) 

61 new_types = self._get_types(new_schema) 

62 

63 # Find added types 

64 diff.removed = old_types - new_types 

65 

66 # Find removed types 

67 diff.added = new_types - old_types 

68 

69 # Check for breaking changes 

70 if diff.removed: 

71 diff.breaking = True 

72 

73 return diff 

74 

75 def _get_types(self, schema: Any) -> set[str]: 

76 """Get type names from schema.""" 

77 types = set() 

78 

79 if hasattr(schema, "types"): 

80 for type_obj in schema.types: 

81 if hasattr(type_obj, "name"): 

82 types.add(type_obj.name) 

83 

84 return types 

85 

86 

87__all__ = [ 

88 "SchemaDiff", 

89 "SchemaDiffer", 

90]