Skip to content

Commit 5538731

Browse files
committed
fix: add explicit record casting to all graph functions for SurrealDB 2.x
Critical fix for SurrealDB 2.x compatibility: Since SurrealDB 2.0, strings are no longer automatically converted to record types. All graph functions were broken because they accepted string parameters but used them in FROM clauses which require record types. Fixed functions: 1. fn::node_reference() - Added type::thing() conversion before calling fn::node_info() 2. fn::get_transitive_dependencies() - Added $record conversion for FROM ONLY clause 3. fn::trace_call_chain() - Added $record conversion for FROM ONLY clause 4. fn::calculate_coupling_metrics() - Added $record for FROM ONLY and fn::node_info() call 5. fn::get_reverse_dependencies() - Added $record conversion for FROM ONLY clause Pattern used: LET $record = type::thing('nodes', $node_id); FROM ONLY $record // or fn::node_info($record) This ensures all string node IDs are properly cast to record<nodes> type before use in graph traversal or function calls. Ref: https://surrealdb.com/docs/surrealql/datamodel/casting
1 parent 2eef1f1 commit 5538731

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

schema/codegraph.surql

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,8 @@ DEFINE FUNCTION fn::node_info($node_id: record) {
344344
-- FUNCTION: node_reference (UNCHANGED)
345345
-- -----------------------------------------------------------------------------
346346
DEFINE FUNCTION fn::node_reference($node_id: string) {
347-
LET $info = fn::node_info($node_id);
347+
LET $record = type::thing('nodes', $node_id);
348+
LET $info = fn::node_info($record);
348349
IF $info = NONE {
349350
RETURN NONE;
350351
};
@@ -369,10 +370,11 @@ DEFINE FUNCTION fn::edge_types() {
369370
DEFINE FUNCTION fn::get_transitive_dependencies($node_id: string, $edge_type: string, $depth: int) {
370371
LET $safe_depth = IF $depth > 0 AND $depth <= 10 THEN $depth ELSE 3 END;
371372
LET $edge_name = $edge_type ?? 'Calls';
373+
LET $record = type::thing('nodes', $node_id);
372374

373375
LET $lvl1 = IF $safe_depth >= 1 THEN
374376
SELECT VALUE id
375-
FROM ONLY $node_id
377+
FROM ONLY $record
376378
->edges[WHERE edge_type = $edge_name]
377379
->to
378380
ELSE [] END;
@@ -499,6 +501,7 @@ DEFINE FUNCTION fn::detect_circular_dependencies($edge_type: string) {
499501
-- -----------------------------------------------------------------------------
500502
DEFINE FUNCTION fn::trace_call_chain($from_node: string, $max_depth: int) {
501503
LET $safe_depth = IF $max_depth > 0 AND $max_depth <= 10 THEN $max_depth ELSE 5 END;
504+
LET $record = type::thing('nodes', $from_node);
502505

503506
LET $raw = (
504507
SELECT
@@ -510,7 +513,7 @@ DEFINE FUNCTION fn::trace_call_chain($from_node: string, $max_depth: int) {
510513
).caller) AS called_by
511514
FROM (
512515
SELECT ->edges[WHERE edge_type = 'Calls']
513-
FROM ONLY $from_node
516+
FROM ONLY $record
514517
)->to
515518
);
516519

@@ -534,17 +537,18 @@ DEFINE FUNCTION fn::trace_call_chain($from_node: string, $max_depth: int) {
534537
-- -----------------------------------------------------------------------------
535538
DEFINE FUNCTION fn::calculate_coupling_metrics($node_id: string) {
536539
LET $edge_list = fn::edge_types();
540+
LET $record = type::thing('nodes', $node_id);
537541

538542
LET $dependents = array::distinct(
539543
SELECT VALUE id
540-
FROM ONLY $node_id
544+
FROM ONLY $record
541545
<-edges[WHERE edge_type INSIDE $edge_list]
542546
<-from
543547
);
544548

545549
LET $dependencies = array::distinct(
546550
SELECT VALUE id
547-
FROM ONLY $node_id
551+
FROM ONLY $record
548552
->edges[WHERE edge_type INSIDE $edge_list]
549553
->to
550554
);
@@ -567,7 +571,7 @@ DEFINE FUNCTION fn::calculate_coupling_metrics($node_id: string) {
567571
LET $instability = IF $total > 0 THEN math::round($efferent / $total, 6) ELSE 0 END;
568572

569573
RETURN {
570-
node: fn::node_info($node_id),
574+
node: fn::node_info($record),
571575
metrics: {
572576
afferent_coupling: $afferent,
573577
efferent_coupling: $efferent,
@@ -678,10 +682,11 @@ DEFINE FUNCTION fn::get_hub_nodes($min_degree: int) {
678682
DEFINE FUNCTION fn::get_reverse_dependencies($node_id: string, $edge_type: string, $depth: int) {
679683
LET $safe_depth = IF $depth > 0 AND $depth <= 10 THEN $depth ELSE 3 END;
680684
LET $edge_name = $edge_type ?? 'Calls';
685+
LET $record = type::thing('nodes', $node_id);
681686

682687
LET $lvl1 = IF $safe_depth >= 1 THEN
683688
SELECT VALUE id
684-
FROM ONLY $node_id
689+
FROM ONLY $record
685690
<-edges[WHERE edge_type = $edge_name]
686691
<-from
687692
ELSE [] END;

0 commit comments

Comments
 (0)