Skip to main content

velesdb_server/handlers/graph/
handlers.rs

1//! Graph HTTP handlers for VelesDB REST API.
2//!
3//! All graph operations are routed through `AppState.db.get_graph_collection()`.
4//! No separate GraphService state — graph data persists via GraphCollection/GraphEngine.
5//!
6//! Extended handlers (parity endpoints) live in [`super::handlers_extended`].
7
8use std::sync::Arc;
9
10use axum::{
11    extract::{Path, Query, State},
12    http::StatusCode,
13    response::IntoResponse,
14    Json,
15};
16use velesdb_core::collection::graph::{GraphEdge, TraversalConfig};
17use velesdb_core::observer::QueryOperationKind;
18
19use crate::handlers::helpers::auto_core_error_response;
20use crate::types::ErrorResponse;
21use crate::AppState;
22
23use super::types::{
24    AddEdgeRequest, AddEdgesBatchRequest, AddEdgesBatchResponse, DegreeResponse, EdgeQueryParams,
25    EdgeResponse, EdgesResponse, TraversalStats, TraverseRequest, TraverseResponse,
26};
27
28/// Shared graph preamble: record metric and resolve collection.
29///
30/// Mirrors [`super::super::search::search_preamble`] for graph handlers.
31/// `GraphCollection` does not expose guard rails, so only the metrics
32/// recording and collection resolution steps are performed.
33#[allow(clippy::result_large_err)]
34pub(super) fn graph_preamble(
35    state: &AppState,
36    name: &str,
37) -> Result<velesdb_core::GraphCollection, (StatusCode, Json<ErrorResponse>)> {
38    state.onboarding_metrics.record_graph_request();
39    get_graph_collection_or_404(state, name)
40}
41
42/// Resolves a `GraphCollection` by name.
43///
44/// # Returns
45///
46/// * `Ok(collection)` if a graph collection with this name exists.
47/// * `Err(404 Not Found)` if no collection with this name exists.
48/// * `Err(409 Conflict)` if a collection exists with this name but is not
49///   a graph collection (type mismatch with vector or metadata collection).
50///
51/// # Contract
52///
53/// This function previously auto-created a schemaless graph collection
54/// on first use. That behaviour is retired (F-05): a missing graph
55/// collection now yields a 404 response instead of being created
56/// silently. Callers must issue `POST /collections` with
57/// `collection_type = "graph"` before targeting graph endpoints.
58pub(super) fn get_graph_collection_or_404(
59    state: &AppState,
60    name: &str,
61) -> Result<velesdb_core::GraphCollection, (StatusCode, Json<ErrorResponse>)> {
62    if let Some(c) = state.db.get_graph_collection(name) {
63        return Ok(c);
64    }
65
66    // Check if a non-graph collection exists with this name (type mismatch → 409).
67    if state.db.get_vector_collection(name).is_some()
68        || state.db.get_metadata_collection(name).is_some()
69    {
70        return Err((
71            StatusCode::CONFLICT,
72            Json(ErrorResponse {
73                error: format!(
74                    "Collection '{name}' exists but is not a graph collection. \
75                     Use /collections/{name}/graph only on graph-typed collections.",
76                ),
77                code: None,
78            }),
79        ));
80    }
81
82    // PR #586 Devin fix: propagate `VELES-002 CollectionNotFound` so
83    // typed-error clients surface `CollectionNotFoundError` instead of
84    // a status-derived `'NOT_FOUND'` string. The "create it first"
85    // hint stays in the message for human operators.
86    let err = velesdb_core::Error::CollectionNotFound(name.to_string());
87    Err((
88        StatusCode::NOT_FOUND,
89        Json(ErrorResponse {
90            error: format!(
91                "{err}. Create it first with \
92                 POST /collections and collection_type = \"graph\".",
93            ),
94            code: Some(err.code().to_string()),
95        }),
96    ))
97}
98
99/// Shared graph preamble for **read** operations: resolves the collection via
100/// [`graph_preamble`], then routes the read through the control-plane gate
101/// (CORE-2). Graph reads have no metadata-filter channel to narrow, so a
102/// denied or scope-narrowed decision refuses the request (fail closed)
103/// rather than running it unfiltered.
104///
105/// Mirrors the gate `MATCH` (`handlers::match_query`) and embedding
106/// [`super::handlers_extended::graph_search`] already apply — centralized
107/// here so every plain REST graph read is governed the same way instead of
108/// each handler wiring the check individually.
109#[allow(clippy::result_large_err)]
110pub(super) fn graph_read_preamble(
111    state: &AppState,
112    name: &str,
113    operation: QueryOperationKind,
114) -> Result<velesdb_core::GraphCollection, (StatusCode, Json<ErrorResponse>)> {
115    let coll = graph_preamble(state, name)?;
116    match state.db.authorize_read(name, operation, None, None) {
117        Ok(None) => Ok(coll),
118        Ok(Some(_)) | Err(_) => Err((
119            StatusCode::FORBIDDEN,
120            Json(ErrorResponse {
121                error: "Read denied by governance policy".to_string(),
122                code: None,
123            }),
124        )),
125    }
126}
127
128/// Get edges from a collection's graph filtered by label.
129#[utoipa::path(
130    get,
131    path = "/collections/{name}/graph/edges",
132    params(("name" = String, Path, description = "Collection name"), EdgeQueryParams),
133    responses(
134        (status = 200, description = "Edges retrieved successfully", body = EdgesResponse),
135        (status = 400, description = "Missing required 'label' query parameter", body = ErrorResponse),
136        (status = 404, description = "Collection not found", body = ErrorResponse),
137        (status = 500, description = "Internal server error", body = ErrorResponse)
138    ),
139    tag = "graph"
140)]
141pub async fn get_edges(
142    Path(name): Path<String>,
143    Query(params): Query<EdgeQueryParams>,
144    State(state): State<Arc<AppState>>,
145) -> Result<Json<EdgesResponse>, (StatusCode, Json<ErrorResponse>)> {
146    let label = params.label.ok_or_else(|| {
147        (
148            StatusCode::BAD_REQUEST,
149            Json(ErrorResponse {
150                error: "Query parameter 'label' is required. Listing all edges requires pagination (not yet implemented).".to_string(),
151                code: None,
152            }),
153        )
154    })?;
155
156    let coll = graph_read_preamble(&state, &name, QueryOperationKind::GraphTraversal)?;
157
158    let edges: Vec<EdgeResponse> = coll
159        .get_edges(Some(&label))
160        .into_iter()
161        .map(|e| EdgeResponse {
162            id: e.id(),
163            source: e.source(),
164            target: e.target(),
165            label: e.label().to_string(),
166            properties: serde_json::to_value(e.properties()).unwrap_or_default(),
167        })
168        .collect();
169
170    let count = edges.len();
171    Ok(Json(EdgesResponse { edges, count }))
172}
173
174/// Add an edge to a collection's graph.
175#[utoipa::path(
176    post,
177    path = "/collections/{name}/graph/edges",
178    request_body = AddEdgeRequest,
179    responses(
180        (status = 201, description = "Edge added successfully"),
181        (status = 400, description = "Invalid request", body = ErrorResponse),
182        (status = 404, description = "Collection not found, or source/target node has no stored payload (VELES-022 NodeNotFound)", body = ErrorResponse),
183        (status = 500, description = "Internal server error", body = ErrorResponse)
184    ),
185    tag = "graph"
186)]
187pub async fn add_edge(
188    Path(name): Path<String>,
189    State(state): State<Arc<AppState>>,
190    Json(request): Json<AddEdgeRequest>,
191) -> axum::response::Response {
192    let edge = match build_edge(request) {
193        Ok(e) => e,
194        Err(resp) => return resp.into_response(),
195    };
196
197    let coll = match graph_preamble(&state, &name) {
198        Ok(c) => c,
199        Err(resp) => return resp.into_response(),
200    };
201
202    // Route the core error through `auto_core_error_response` so e.g.
203    // `EdgeExists` surfaces as 409 + VELES-019 instead of a generic 500 string.
204    match coll.add_edge(edge) {
205        Ok(()) => StatusCode::CREATED.into_response(),
206        Err(e) => auto_core_error_response(&e),
207    }
208}
209
210/// Converts an [`AddEdgeRequest`] into a core [`GraphEdge`], validating the
211/// properties shape and edge fields. Shared by [`add_edge`] and
212/// [`add_edges_batch`].
213#[allow(clippy::result_large_err)]
214fn build_edge(request: AddEdgeRequest) -> Result<GraphEdge, (StatusCode, Json<ErrorResponse>)> {
215    let properties: std::collections::HashMap<String, serde_json::Value> = match request.properties
216    {
217        serde_json::Value::Object(map) => map.into_iter().collect(),
218        serde_json::Value::Null => std::collections::HashMap::new(),
219        _ => {
220            return Err((
221                StatusCode::BAD_REQUEST,
222                Json(ErrorResponse {
223                    error: "Properties must be an object or null".to_string(),
224                    code: None,
225                }),
226            ));
227        }
228    };
229
230    let edge = GraphEdge::new(request.id, request.source, request.target, &request.label)
231        .map_err(|e| {
232            (
233                StatusCode::BAD_REQUEST,
234                Json(ErrorResponse {
235                    error: format!("Invalid edge: {e}"),
236                    code: None,
237                }),
238            )
239        })?
240        .with_properties(properties);
241    Ok(edge)
242}
243
244/// Add multiple edges to a collection's graph in one batched operation.
245#[utoipa::path(
246    post,
247    path = "/collections/{name}/graph/edges/batch",
248    request_body = AddEdgesBatchRequest,
249    responses(
250        (status = 201, description = "Edges added successfully", body = AddEdgesBatchResponse),
251        (status = 400, description = "Invalid request", body = ErrorResponse),
252        (status = 404, description = "Collection not found, or a source/target node has no stored payload (VELES-022 NodeNotFound) — the whole batch is rejected", body = ErrorResponse),
253        (status = 500, description = "Internal server error", body = ErrorResponse)
254    ),
255    tag = "graph"
256)]
257pub async fn add_edges_batch(
258    Path(name): Path<String>,
259    State(state): State<Arc<AppState>>,
260    Json(request): Json<AddEdgesBatchRequest>,
261) -> axum::response::Response {
262    let edges = match request
263        .edges
264        .into_iter()
265        .map(build_edge)
266        .collect::<Result<Vec<_>, _>>()
267    {
268        Ok(edges) => edges,
269        Err(resp) => return resp.into_response(),
270    };
271
272    let coll = match graph_preamble(&state, &name) {
273        Ok(c) => c,
274        Err(resp) => return resp.into_response(),
275    };
276
277    // Route the core error through `auto_core_error_response` so e.g.
278    // `EdgeExists` surfaces as 409 + VELES-019 instead of a generic 500 string.
279    match coll.add_edges_batch(edges) {
280        Ok(added) => (StatusCode::CREATED, Json(AddEdgesBatchResponse { added })).into_response(),
281        Err(e) => auto_core_error_response(&e),
282    }
283}
284
285/// Traverse the graph using BFS or DFS from a source node.
286#[utoipa::path(
287    post,
288    path = "/collections/{name}/graph/traverse",
289    request_body = TraverseRequest,
290    responses(
291        (status = 200, description = "Traversal completed successfully", body = TraverseResponse),
292        (status = 400, description = "Invalid request", body = ErrorResponse),
293        (status = 404, description = "Collection not found", body = ErrorResponse),
294        (status = 500, description = "Internal server error", body = ErrorResponse)
295    ),
296    tag = "graph"
297)]
298pub async fn traverse_graph(
299    Path(name): Path<String>,
300    State(state): State<Arc<AppState>>,
301    Json(request): Json<TraverseRequest>,
302) -> Result<Json<TraverseResponse>, (StatusCode, Json<ErrorResponse>)> {
303    let coll = graph_read_preamble(&state, &name, QueryOperationKind::GraphTraversal)?;
304
305    let config = TraversalConfig::with_range(1, request.max_depth)
306        .with_limit(request.limit)
307        .with_rel_types(request.rel_types);
308
309    let raw_results = match request.strategy.to_lowercase().as_str() {
310        "bfs" => coll.traverse_bfs(request.source, &config),
311        "dfs" => coll.traverse_dfs(request.source, &config),
312        _ => {
313            return Err((
314                StatusCode::BAD_REQUEST,
315                Json(ErrorResponse {
316                    error: format!(
317                        "Invalid strategy '{}'. Use 'bfs' or 'dfs'.",
318                        request.strategy
319                    ),
320                    code: None,
321                }),
322            ));
323        }
324    };
325
326    let results: Vec<super::types::TraversalResultItem> = raw_results
327        .into_iter()
328        .map(|r| super::types::TraversalResultItem {
329            target_id: r.target_id,
330            depth: r.depth,
331            path: r.path,
332        })
333        .collect();
334
335    let depth_reached = results.iter().map(|r| r.depth).max().unwrap_or(0);
336    let visited = results.len();
337    let has_more = visited >= request.limit;
338
339    Ok(Json(TraverseResponse {
340        results,
341        has_more,
342        stats: TraversalStats {
343            visited,
344            depth_reached,
345        },
346    }))
347}
348
349/// Get the degree (in and out) of a specific node.
350#[utoipa::path(
351    get,
352    path = "/collections/{name}/graph/nodes/{node_id}/degree",
353    params(
354        ("name" = String, Path, description = "Collection name"),
355        ("node_id" = String, Path, description = "Node ID (u64 as a string; precision-safe above 2^53-1)", pattern = "^[0-9]+$")
356    ),
357    responses(
358        (status = 200, description = "Degree retrieved successfully", body = DegreeResponse),
359        (status = 404, description = "Collection not found", body = ErrorResponse),
360        (status = 500, description = "Internal server error", body = ErrorResponse)
361    ),
362    tag = "graph"
363)]
364pub async fn get_node_degree(
365    Path((name, node_id)): Path<(String, u64)>,
366    State(state): State<Arc<AppState>>,
367) -> Result<Json<DegreeResponse>, (StatusCode, Json<ErrorResponse>)> {
368    let coll = graph_read_preamble(&state, &name, QueryOperationKind::GraphTraversal)?;
369    let (in_degree, out_degree) = coll.node_degree(node_id);
370    Ok(Json(DegreeResponse {
371        in_degree,
372        out_degree,
373    }))
374}