use clap::Subcommand;
use colored::Colorize;
use std::path::PathBuf;
#[derive(Subcommand)]
pub enum GraphAction {
Traverse {
path: PathBuf,
collection: String,
source: u64,
#[arg(short, long, default_value = "bfs")]
strategy: String,
#[arg(short = 'd', long, default_value = "3")]
max_depth: u32,
#[arg(short = 'l', long, default_value = "100")]
limit: usize,
#[arg(short = 'r', long)]
rel_types: Option<String>,
#[arg(short, long, default_value = "table")]
format: String,
#[arg(long)]
stream: bool,
},
Degree {
path: PathBuf,
collection: String,
node_id: u64,
#[arg(short, long, default_value = "table")]
format: String,
},
AddEdge {
path: PathBuf,
collection: String,
id: u64,
source: u64,
target: u64,
label: String,
},
}
pub fn handle(action: GraphAction) {
println!(
"{} Graph operations require a running VelesDB server.",
"ℹ️".cyan()
);
println!(" Start server: velesdb-server --data-dir ./data");
println!(" Then use curl or the TypeScript SDK to interact with graph endpoints:\n");
match action {
GraphAction::Traverse {
path: _,
collection,
source,
strategy,
max_depth,
limit,
rel_types,
format: _,
stream,
} => {
if stream {
println!("{} Streaming traversal via SSE endpoint:", "📡".cyan());
println!(
" curl -N 'http://localhost:8080/collections/{}/graph/stream-traverse?start_node={}&algorithm={}&max_depth={}&limit={}'",
collection, source, strategy, max_depth, limit
);
println!();
println!(
"{} Or use the streaming client (NDJSON output):",
"💡".yellow()
);
println!(
" velesdb-cli graph traverse {} {} --stream | jq -c '.'",
collection, source
);
return;
}
let rel_types_json = rel_types
.map(|s| {
let types: Vec<&str> = s.split(',').map(str::trim).collect();
serde_json::json!(types)
})
.unwrap_or(serde_json::json!([]));
println!(
" curl -X POST http://localhost:8080/collections/{}/graph/traverse \\",
collection
);
println!(" -H 'Content-Type: application/json' \\");
println!(
" -d '{}'",
serde_json::json!({
"source": source,
"strategy": strategy,
"max_depth": max_depth,
"limit": limit,
"rel_types": rel_types_json
})
);
}
GraphAction::Degree {
path: _,
collection,
node_id,
format: _,
} => {
println!(
" curl http://localhost:8080/collections/{}/graph/nodes/{}/degree",
collection, node_id
);
}
GraphAction::AddEdge {
path: _,
collection,
id,
source,
target,
label,
} => {
println!(
" curl -X POST http://localhost:8080/collections/{}/graph/edges \\",
collection
);
println!(" -H 'Content-Type: application/json' \\");
println!(
" -d '{}'",
serde_json::json!({
"id": id,
"source": source,
"target": target,
"label": label,
"properties": {}
})
);
}
}
println!();
println!(
"{} For persistent graph storage, use the Python SDK with Collection.add_edge().",
"💡".yellow()
);
}