Skip to content
GitHubDiscord

Graph (lexigram-graph)

Graph database support for the Lexigram Framework (Neo4j, in-memory).


lexigram-graph provides graph storage backends with DI wiring for in-memory and Neo4j implementations behind the graph contracts. It supports node and edge creation, graph traversal queries, Cypher compilation for Neo4j, and lazy graph creation with lifecycle events.


Terminal window
uv add lexigram-graph
# With Neo4j support
uv add "lexigram-graph[neo4j]"
from lexigram import Application
from lexigram.di.module import Module, module
from lexigram.graph import GraphConfig, GraphModule
from lexigram.contracts.data.graph import GraphStoreProtocol, TraversalQuery, StartSpec, TraversalStep
@module(imports=[GraphModule.configure(GraphConfig(backend="memory"))])
class AppModule(Module):
pass
async def main() -> None:
async with Application.boot(modules=[AppModule]) as app:
store = await app.container.resolve(GraphStoreProtocol)
graph = await store.get_graph()
await graph.create_node(["Person"], {"name": "Alice"}, node_id="alice")
await graph.create_node(["Person"], {"name": "Bob"}, node_id="bob")
await graph.create_edge("alice", "bob", "KNOWS")
paths = await graph.traverse(
TraversalQuery(
start=StartSpec(node_ids=("alice",)),
steps=(TraversalStep(edge_types=("KNOWS",)),),
)
)
assert paths
if __name__ == "__main__":
import asyncio
asyncio.run(main())

Zero-config usage: Call GraphModule.configure() with no arguments to use all defaults (in-memory backend).

application.yaml
graph:
enabled: true
backend: neo4j
default_traversal_max_depth: 10
neo4j:
uri: bolt://localhost:7687
password: "${NEO4J_PASSWORD}"
Section titled “Option 2 — Profiles + Environment Variables (recommended)”
Terminal window
export LEX_GRAPH__BACKEND=neo4j
export LEX_GRAPH__NEO4J__URI=bolt://localhost:7687
from lexigram.graph import GraphConfig, GraphModule
from lexigram.graph.config import Neo4jConfig
GraphModule.configure(
GraphConfig(
backend="neo4j",
neo4j=Neo4jConfig(
uri="bolt://localhost:7687",
password="${NEO4J_PASSWORD}",
),
)
)
FieldDefaultEnv varDescription
enabledtrueLEX_GRAPH__ENABLEDEnable or disable the graph subsystem
backendmemoryLEX_GRAPH__BACKENDGraph backend to use (memory or neo4j)
default_traversal_max_depth10LEX_GRAPH__DEFAULT_TRAVERSAL_MAX_DEPTHMaximum depth for graph traversals
default_query_limit100LEX_GRAPH__DEFAULT_QUERY_LIMITDefault result limit for graph queries
bulk_batch_size1000LEX_GRAPH__BULK_BATCH_SIZEBatch size for bulk insert and update operations
max_retries3LEX_GRAPH__MAX_RETRIESRetry attempts on transient graph errors
retry_delay1.0LEX_GRAPH__RETRY_DELAYSeconds between retry attempts
neo4j.uribolt://localhost:7687LEX_GRAPH__NEO4J__URINeo4j Bolt connection URI
neo4j.usernameneo4jLEX_GRAPH__NEO4J__USERNAMENeo4j authentication username
neo4j.passwordLEX_GRAPH__NEO4J__PASSWORDNeo4j authentication password (required for production)
neo4j.databaseneo4jLEX_GRAPH__NEO4J__DATABASETarget Neo4j database name
neo4j.max_connection_pool_size100LEX_GRAPH__NEO4J__MAX_CONNECTION_POOL_SIZEMaximum driver connection pool size
memory.max_nodes1000000LEX_GRAPH__MEMORY__MAX_NODESNode capacity for the in-memory backend
MethodDescription
GraphModule.configure(config=None)Register GraphProvider with a config
GraphModule.stub(config=None)Lightweight test module with in-memory backend
  • In-memory backend — no external service needed; for development and tests
  • Neo4j backend — async Neo4j driver with Cypher query compilation
  • Graph traversalTraversalQuery, StartSpec, TraversalStep for graph walks
  • Named graphs — lazy graph creation per name with lifecycle events
  • Connection pooling — configurable pool size for Neo4j driver
async with Application.boot(modules=[GraphModule.stub()]) as app:
store = await app.container.resolve(GraphStoreProtocol)
graph = await store.get_graph()
# Test with in-memory backend
FileWhat it contains
src/lexigram/graph/module.pyGraphModule.configure(), .stub()
src/lexigram/graph/config.pyGraphConfig, Neo4jConfig
src/lexigram/graph/di/provider.pyGraphProvider boot and registration
src/lexigram/graph/backends/memory/backend.pyInMemoryGraphStore implementation
src/lexigram/graph/backends/neo4j/backend.pyNeo4jGraphStore implementation
src/lexigram/graph/backends/neo4j/cypher.pyCypherCompiler