API Reference
Protocols
Section titled “Protocols”GraphProtocol
Section titled “GraphProtocol”All operations on a single graph database.
Provides CRUD for nodes and edges, traversal queries, raw query passthrough, bulk operations, and schema management.
async def create_node( labels: list[str], properties: dict[str, Any] | None = None, node_id: str | None = None ) -> NodeResult
Create a node with labels and properties.
Retrieve a node by ID. Returns None if not found.
async def find_nodes( labels: list[str] | None = None, filter: PropertyFilter | None = None, limit: int = 100, skip: int = 0 ) -> list[GraphNode]
Find nodes matching labels and/or property filter.
Update node properties.
Delete a node.
async def neighbors( node_id: str, depth: int = 1, direction: EdgeDirection = EdgeDirection.BOTH, edge_types: list[str] | None = None ) -> list[GraphNode]
Get neighbouring nodes reachable within the given depth.
Return the total number of nodes in the graph.
Return the total number of edges in the graph.
Return all unique node labels in the graph.
Return all unique edge types in the graph.
async def create_edge( source_id: str, target_id: str, edge_type: str, properties: dict[str, Any] | None = None ) -> EdgeResult
Create a directed edge between two nodes.
Retrieve an edge by ID.
async def get_edges( node_id: str, direction: EdgeDirection = EdgeDirection.BOTH, edge_types: list[str] | None = None, limit: int = 100 ) -> list[GraphEdge]
Get edges connected to a node.
Update edge properties.
Delete an edge by ID.
Execute a traversal query and return matching paths.
async def shortest_path( from_id: str, to_id: str, max_depth: int = 10, edge_types: list[str] | None = None, direction: EdgeDirection = EdgeDirection.BOTH ) -> GraphPath | None
Find the shortest path between two nodes.
async def query( query_string: str, parameters: dict[str, Any] | None = None ) -> list[dict[str, Any]]
Execute a raw backend-specific query (Cypher, Gremlin, etc.).
Create multiple nodes in a single operation.
Create multiple edges in a single operation.
Create an index on node label + properties.
Drop an index by name.
Create a schema constraint.
Drop a constraint by name.
GraphStoreProtocol
Section titled “GraphStoreProtocol”Top-level graph store lifecycle and database management.
Manages connections to the graph backend and provides access to individual graph databases.
Establish connection to the graph store.
Close all connections and release resources.
Check store connectivity and readiness.
async def get_graph(name: str | None = None) -> GraphProtocol
Get a handle to a graph database.
| Parameter | Type | Description |
|---|---|---|
| `name` | str | None | Database name. ``None`` returns the default graph. |
| Exception | Description |
|---|---|
| GraphNotFoundError | If the named graph does not exist. |
List all available graph databases.
Create a new graph database.
| Exception | Description |
|---|---|
| GraphAlreadyExistsError | If the graph already exists. |
Delete a graph database and all its data.
| Exception | Description |
|---|---|
| GraphNotFoundError | If the graph does not exist. |
Classes
Section titled “Classes”BaseGraph
Section titled “BaseGraph”Common logic for all graph database implementations.
BaseGraphStore
Section titled “BaseGraphStore”Common logic for all graph store implementations.
GraphConfig
Section titled “GraphConfig”Top-level graph store configuration.
Loaded from the graph: key in application.yaml, with environment
variable overrides via LEX_GRAPH__* prefix.
Check config is safe for the target environment.
| Parameter | Type | Description |
|---|---|---|
| `env` | Environment | None | The target deployment environment. Defaults to the current environment. |
| Type | Description |
|---|---|
| list[ConfigIssue] | A list of ConfigIssue describing any configuration problems found. |
GraphConnectedEvent
Section titled “GraphConnectedEvent”Raised when a graph database connection is established.
Downstream: observability, health monitoring.
GraphDisconnectedEvent
Section titled “GraphDisconnectedEvent”Raised when a graph database connection is closed.
Downstream: observability, cleanup hooks.
GraphEdgeCreatedEvent
Section titled “GraphEdgeCreatedEvent”Raised when an edge (relationship) is created between two nodes.
Downstream: audit logging, event sourcing.
GraphModule
Section titled “GraphModule”Graph storage module — registers GraphStoreProtocol and GraphProtocol.
Registers GraphStoreProtocol and GraphProtocol for constructor injection.
Call configure to configure the graph backend (Neo4j or in-memory), or stub for an isolated setup with no external service dependencies.
Usage
from lexigram.graph.config import GraphConfig
@module( imports=[GraphModule.configure(GraphConfig(backend="neo4j"))])class AppModule(Module): passdef configure( cls, config: GraphConfig | None = None ) -> DynamicModule
Create a GraphModule with explicit configuration.
| Parameter | Type | Description |
|---|---|---|
| `config` | GraphConfig | None | GraphConfig, a plain ``dict`` of config values, or ``None`` to use environment variable defaults. |
| Type | Description |
|---|---|
| DynamicModule | A DynamicModule descriptor. |
| Exception | Description |
|---|---|
| TypeError | If *config* is not a ``GraphConfig``, ``dict``, or ``None``. |
def stub( cls, config: GraphConfig | None = None ) -> DynamicModule
Create a GraphModule suitable for unit and integration testing.
Uses an in-memory backend with no external service dependencies.
| Parameter | Type | Description |
|---|---|---|
| `config` | GraphConfig | None | Optional GraphConfig override. Uses safe in-memory defaults when ``None``. |
| Type | Description |
|---|---|
| DynamicModule | A DynamicModule descriptor. |
GraphNodeCreatedEvent
Section titled “GraphNodeCreatedEvent”Raised when a node is created in the graph.
Downstream: audit logging, event sourcing.
GraphProvider
Section titled “GraphProvider”Register graph store services into the DI container.
async def register(container: ContainerRegistrarProtocol) -> None
Register graph store bindings into the container.
| Parameter | Type | Description |
|---|---|---|
| `container` | ContainerRegistrarProtocol | The DI registrar to register services into. |
| Exception | Description |
|---|---|
| ValueError | If an unknown backend is specified in config. |
async def boot(container: ContainerResolverProtocol) -> None
Connect to the graph store after the container is fully built.
| Parameter | Type | Description |
|---|---|---|
| `container` | ContainerResolverProtocol | The DI resolver to obtain the graph store from. |
Disconnect from the graph store during application shutdown.
Check provider health.
| Parameter | Type | Description |
|---|---|---|
| `timeout` | float | Maximum seconds to wait for health check response. |
| Type | Description |
|---|---|
| HealthCheckResult | HealthCheckResult with status and component details. |
GraphQueryExecutedEvent
Section titled “GraphQueryExecutedEvent”Raised when a graph query is executed (e.g., Cypher).
Downstream: analytics, slow query logging.
Neo4jConfig
Section titled “Neo4jConfig”Configuration for the Neo4j backend.
Neo4jGraph
Section titled “Neo4jGraph”Neo4j graph implementation (Cypher-based).
async def create_node( labels: list[str], properties: dict[str, Any] | None = None, node_id: str | None = None ) -> NodeResult
Create a node in Neo4j.
| Parameter | Type | Description |
|---|---|---|
| `labels` | list[str] | Node labels. |
| `properties` | dict[str, Any] | None | Optional property map. |
| `node_id` | str | None | Optional explicit node ID; stored as the ``id`` property. |
| Type | Description |
|---|---|
| NodeResult | NodeResult with the node ID. |
Retrieve a node by its id property.
| Parameter | Type | Description |
|---|---|---|
| `node_id` | str | The node's ``id`` property value. |
| Type | Description |
|---|---|
| GraphNode | None | The GraphNode or ``None``. |
async def find_nodes( labels: list[str] | None = None, filter: PropertyFilter | None = None, limit: int = 100, skip: int = 0 ) -> list[GraphNode]
Find nodes with optional label filtering.
| Parameter | Type | Description |
|---|---|---|
| `labels` | list[str] | None | Optional label filter; all labels must match. |
| `filter` | PropertyFilter | None | Unused. |
| `limit` | int | Maximum results. |
| `skip` | int | Results to skip. |
| Type | Description |
|---|---|
| list[GraphNode] | Matching GraphNode instances. |
async def create_edge( source_id: str, target_id: str, edge_type: str, properties: dict[str, Any] | None = None ) -> EdgeResult
Create a directed relationship between two nodes.
| Parameter | Type | Description |
|---|---|---|
| `source_id` | str | Source node ``id`` property. |
| `target_id` | str | Target node ``id`` property. |
| `edge_type` | str | Relationship type. |
| `properties` | dict[str, Any] | None | Optional relationship properties. |
| Type | Description |
|---|---|
| EdgeResult | EdgeResult with the element ID of the new relationship. |
Execute a traversal query via compiled Cypher.
| Parameter | Type | Description |
|---|---|---|
| `query` | TraversalQuery | Traversal parameters including start node, depth, and relationship type filters. |
| Type | Description |
|---|---|
| list[GraphPath] | All GraphPath instances found by the traversal. |
async def query( query_string: str, parameters: dict[str, Any] | None = None ) -> list[dict[str, Any]]
Execute a raw Cypher query string.
| Parameter | Type | Description |
|---|---|---|
| `query_string` | str | Cypher query. |
| `parameters` | dict[str, Any] | None | Optional parameter bindings. |
| Type | Description |
|---|---|
| list[dict[str, Any]] | List of record dicts. |
Neo4jGraphStore
Section titled “Neo4jGraphStore”Neo4j managed graph store backend.
def __init__(config: Neo4jConfig) -> None
Connect to Neo4j and create foundational constraints.
Creates a uniqueness constraint for the id property on Node
labels as a best-effort operation.
Close the Neo4j driver.
Verify connectivity to Neo4j.
| Parameter | Type | Description |
|---|---|---|
| `timeout` | float | Unused; Neo4j driver manages its own timeout. |
| Type | Description |
|---|---|
| HealthCheckResult | HealthCheckResult with HEALTHY or UNHEALTHY status. |
async def get_graph(name: str | None = None) -> Neo4jGraph
Return a handle to a named Neo4j database.
| Parameter | Type | Description |
|---|---|---|
| `name` | str | None | Database name. Defaults to the driver default. |
| Type | Description |
|---|---|
| Neo4jGraph | A Neo4jGraph instance scoped to the given database. |
| Exception | Description |
|---|---|
| RuntimeError | If not yet connected. |
List all databases visible to the connected Neo4j instance.
| Type | Description |
|---|---|
| list[GraphInfo] | A list of GraphInfo. |
Execute a raw Cypher query and return all records.
| Parameter | Type | Description |
|---|---|---|
| `query_string` | str | Cypher query to execute. |
| Type | Description |
|---|---|
| list[dict[str, Any]] | List of record dicts. |
| Exception | Description |
|---|---|
| RuntimeError | If not yet connected. |