GraphService¶
GraphService is the concrete implementation of IGraphService. It adds a type-validation layer on top of GraphEngine: before any node or edge is persisted, it looks up the graph's GraphType in the TypeRegistry and validates the attributes against the declared schema.
Validation Flow¶
sequenceDiagram
participant UI
participant GS as GraphService
participant TR as TypeRegistry
participant GT as GraphType
participant E as GraphEngine
UI->>GS: add_node(graph_id, "OutlineItem", {title: "Intro"})
GS->>E: get_graph(graph_id)
E-->>GS: graph
GS->>TR: get(graph.type_name)
TR-->>GS: OutlineGraphType
GS->>GT: validate_node("OutlineItem", {title: "Intro"})
GT-->>GS: OK (no errors)
GS->>E: add_node(node)
E-->>GS: (persisted)
GS-->>UI: node
If validation fails, ValueError is raised before the engine is called and no data is written.
Constructor¶
from knowledge_platform.services.graph_service import GraphService
from knowledge_platform.core.engine import GraphEngine
from knowledge_platform.domain.registry import TypeRegistry
service = GraphService(engine=engine, type_registry=type_registry)
Update Node: Partial Merge¶
update_node() performs a partial attribute merge — only the keys you pass are changed; all other existing attributes are preserved:
# Node currently has: {"title": "Old", "content": "Some text", "position": 0}
updated = service.update_node(node_id, {"title": "New Title"})
# Result: {"title": "New Title", "content": "Some text", "position": 0}
The merged result is still validated against the NodeSchema before persisting.
Error Handling¶
| Method | Raises | Condition |
|---|---|---|
create_graph |
KeyError |
type_name not in TypeRegistry |
add_node |
KeyError |
Graph not found |
add_node |
ValueError |
Schema validation failure |
update_node |
KeyError |
Node not found in any cached graph |
update_node |
ValueError |
Schema validation failure after merge |
add_edge |
KeyError |
Graph, source node, or target node not found |
add_edge |
ValueError |
Schema validation failure |
API Reference¶
knowledge_platform.services.graph_service.GraphService ¶
Facade that validates type constraints then delegates to :class:GraphEngine.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
engine
|
GraphEngine
|
The graph engine for all persistence-backed mutations. |
required |
type_registry
|
TypeRegistry
|
Registry used to resolve graph types for validation. |
required |
Source code in src/knowledge_platform/services/graph_service.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | |
Functions¶
add_edge ¶
add_edge(graph_id: GraphId, source_id: NodeId, target_id: NodeId, type_name: str, attributes: dict[str, object]) -> Edge
Validate and add an edge.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph_id
|
GraphId
|
Owning graph. |
required |
source_id
|
NodeId
|
Origin node. |
required |
target_id
|
NodeId
|
Destination node. |
required |
type_name
|
str
|
Edge type registered on the graph's type. |
required |
attributes
|
dict[str, object]
|
Initial attribute payload. |
required |
Returns:
| Type | Description |
|---|---|
Edge
|
The created :class: |
Raises:
| Type | Description |
|---|---|
ValueError
|
On schema validation failure. |
KeyError
|
If graph or edge type is not found. |
Source code in src/knowledge_platform/services/graph_service.py
add_node ¶
Validate and add a node to a graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph_id
|
GraphId
|
Owning graph. |
required |
type_name
|
str
|
Node type registered on the graph's type. |
required |
attributes
|
dict[str, object]
|
Initial attribute payload. |
required |
Returns:
| Type | Description |
|---|---|
Node
|
The created :class: |
Raises:
| Type | Description |
|---|---|
ValueError
|
On schema validation failure. |
KeyError
|
If graph or node type is not found. |
Source code in src/knowledge_platform/services/graph_service.py
bind_repository ¶
Switch the service to a different persistence repository.
The desktop app manages one active workspace at a time. Rebinding the repository swaps the active graph engine to that workspace's SQLite database.
Source code in src/knowledge_platform/services/graph_service.py
create_graph ¶
Create a new validated graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
workspace_id
|
WorkspaceId
|
Owning workspace. |
required |
type_name
|
str
|
Must be registered in the :class: |
required |
name
|
str
|
Display name. |
required |
Returns:
| Type | Description |
|---|---|
Graph
|
Newly created :class: |
Raises:
| Type | Description |
|---|---|
KeyError
|
If type_name is not registered. |
Source code in src/knowledge_platform/services/graph_service.py
delete_graph ¶
Delete a graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph_id
|
GraphId
|
Target identifier. |
required |
get_graph ¶
Retrieve a graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph_id
|
GraphId
|
Target identifier. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
The |
Graph
|
class: |
Raises:
| Type | Description |
|---|---|
KeyError
|
If not found. |
Source code in src/knowledge_platform/services/graph_service.py
list_graphs ¶
List graphs in a workspace.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
workspace_id
|
WorkspaceId
|
Owning workspace. |
required |
Returns:
| Type | Description |
|---|---|
list[Graph]
|
List of graphs (may be empty). |
Source code in src/knowledge_platform/services/graph_service.py
remove_edge ¶
Remove an edge.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph_id
|
GraphId
|
Owning graph. |
required |
edge_id
|
EdgeId
|
Target edge. |
required |
Source code in src/knowledge_platform/services/graph_service.py
remove_node ¶
Remove a node and its incident edges.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph_id
|
GraphId
|
Owning graph. |
required |
node_id
|
NodeId
|
Target node. |
required |
Source code in src/knowledge_platform/services/graph_service.py
update_node ¶
Update node attributes.
Locates the owning graph, applies a partial attribute merge, and validates the merged result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_id
|
NodeId
|
Target node. |
required |
attributes
|
dict[str, object]
|
Attributes to merge. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Updated |
Node
|
class: |
Raises:
| Type | Description |
|---|---|
KeyError
|
If node or graph is not found. |
ValueError
|
On schema validation failure. |