Graph Type System¶
The graph type system enforces semantic constraints on what nodes and edges are valid in a graph. It is the domain equivalent of a database schema.
Building a GraphType¶
Subclass GraphType and declare node_schemas and edge_schemas as class-level dicts:
from knowledge_platform.domain.graph_type import (
AttributeField, NodeSchema, EdgeSchema, GraphType,
)
class TaskGraphType(GraphType):
type_name = "task"
node_schemas = {
"Task": NodeSchema(
type_name="Task",
fields=(
AttributeField("title", str, required=True, description="Task title"),
AttributeField("done", bool, required=False, default=False),
AttributeField("priority", int, required=False, default=0),
),
description="A single actionable task.",
),
}
edge_schemas = {
"BlockedBy": EdgeSchema(
type_name="BlockedBy",
allowed_source_types=frozenset({"Task"}),
allowed_target_types=frozenset({"Task"}),
description="Task A is blocked by Task B.",
),
}
Validation Flow¶
When GraphService.add_node() is called:
- It looks up the graph's
type_namein theTypeRegistry. - It calls
graph_type.validate_node(type_name, attributes). validate_nodefinds theNodeSchemaand runsschema.validate(attributes).validate()checks required fields are present and types match.- If any errors are found,
ValueErroris raised and the node is not added.
The same flow applies to edges via validate_edge().
AttributeField¶
| Parameter | Type | Default | Description |
|---|---|---|---|
name |
str |
— | Attribute key |
python_type |
type |
— | Expected Python type for runtime validation |
required |
bool |
True |
Whether the key must be present |
default |
Any |
None |
Value used when absent (only if required=False) |
description |
str |
"" |
Human-readable documentation |
API Reference¶
knowledge_platform.domain.graph_type.AttributeField
dataclass
¶
Declaration of a single attribute on a node or edge type.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Attribute key. |
python_type |
type
|
Expected Python type (used for runtime validation). |
required |
bool
|
Whether the attribute must be present. |
default |
Any
|
Default value when the attribute is absent (ignored if required). |
description |
str
|
Human-readable documentation string. |
Source code in src/knowledge_platform/domain/graph_type.py
knowledge_platform.domain.graph_type.NodeSchema
dataclass
¶
Semantic schema for a node type within a :class:GraphType.
Attributes:
| Name | Type | Description |
|---|---|---|
type_name |
str
|
Unique type identifier within the graph type. |
fields |
tuple[AttributeField, ...]
|
Ordered attribute field declarations. |
description |
str
|
Human-readable purpose of this node type. |
Source code in src/knowledge_platform/domain/graph_type.py
Functions¶
validate ¶
Return a list of validation error messages (empty if valid).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
attributes
|
dict[str, Any]
|
Node attribute dict to validate. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
List of human-readable error strings. Empty means valid. |
Source code in src/knowledge_platform/domain/graph_type.py
knowledge_platform.domain.graph_type.EdgeSchema
dataclass
¶
Semantic schema for an edge type within a :class:GraphType.
Attributes:
| Name | Type | Description |
|---|---|---|
type_name |
str
|
Unique type identifier within the graph type. |
allowed_source_types |
frozenset[str]
|
Node types permitted as edge sources. Empty means any source type is allowed. |
allowed_target_types |
frozenset[str]
|
Node types permitted as edge targets. Empty means any target type is allowed. |
fields |
tuple[AttributeField, ...]
|
Ordered attribute field declarations. |
description |
str
|
Human-readable purpose of this edge type. |
Source code in src/knowledge_platform/domain/graph_type.py
Functions¶
validate ¶
Return validation error messages for an edge.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source_type
|
str
|
Type name of the source node. |
required |
target_type
|
str
|
Type name of the target node. |
required |
attributes
|
dict[str, Any]
|
Edge attribute dict to validate. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
List of human-readable error strings. Empty means valid. |
Source code in src/knowledge_platform/domain/graph_type.py
knowledge_platform.domain.graph_type.GraphType ¶
Base class for all graph types.
Subclasses declare node schemas and edge schemas that together define the
semantics of a graph. The graph engine uses the registered
:class:GraphType to validate mutations.
Subclasses must define:
- :attr:
type_name(class-levelstr) - :attr:
node_schemas(class-leveldict[str, NodeSchema]) - :attr:
edge_schemas(class-leveldict[str, EdgeSchema])
Example::
class DocumentGraph(GraphType):
type_name = "document"
node_schemas = {
"Section": NodeSchema("Section", fields=(...,)),
}
edge_schemas = {
"ChildOf": EdgeSchema("ChildOf", ...),
}
Source code in src/knowledge_platform/domain/graph_type.py
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 | |
Functions¶
validate_edge ¶
validate_edge(type_name: str, source_type: str, target_type: str, attributes: dict[str, Any]) -> None
Validate edge attributes and endpoint types against the registered schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type_name
|
str
|
Edge type identifier. |
required |
source_type
|
str
|
Type name of the source node. |
required |
target_type
|
str
|
Type name of the target node. |
required |
attributes
|
dict[str, Any]
|
Attribute dict to validate. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
On the first detected schema violation. |
KeyError
|
If type_name is not registered on this graph type. |
Source code in src/knowledge_platform/domain/graph_type.py
validate_node ¶
Validate node attributes against the registered schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type_name
|
str
|
Node type identifier. |
required |
attributes
|
dict[str, Any]
|
Attribute dict to validate. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
On the first detected schema violation. |
KeyError
|
If type_name is not registered on this graph type. |