1- import { FalkorDB , Graph } from "falkordb " ;
1+ import { getEnvVariables } from "@/app/api/utils " ;
22import { NextRequest , NextResponse } from "next/server" ;
33
4- export async function GET ( request : NextRequest , { params } : { params : { graph : string , node : string } } ) {
5-
6- const nodeId = parseInt ( params . node ) ;
7- const graphId = params . graph ;
4+ export async function POST ( request : NextRequest , { params } : { params : Promise < { graph : string , node : string } > } ) {
85
9- const db = await FalkorDB . connect ( { url : process . env . FALKORDB_URL || 'falkor://localhost:6379' , } ) ;
10- const graph = db . selectGraph ( graphId ) ;
6+ const p = await params ;
117
12- // Get node's neighbors
13- const q_params = { nodeId : nodeId } ;
14- const query = `MATCH (src)-[e]-(n)
15- WHERE ID(src) = $nodeId
16- RETURN collect(distinct { label:labels(n)[0], id:ID(n), name: n.name } ) as nodes,
17- collect( { src: ID(startNode(e)), id: ID(e), dest: ID(endNode(e)), type: type(e) } ) as edges` ;
8+ const repo = p . graph ;
9+ const src = Number ( p . node ) ;
10+ const dest = Number ( request . nextUrl . searchParams . get ( 'targetId' ) )
1811
19- let res : any = await graph . query ( query , { params : q_params } ) ;
20- let nodes = res . data [ 0 ] [ 'nodes' ] ;
21- let edges = res . data [ 0 ] [ 'edges' ] ;
12+ try {
2213
23- return NextResponse . json ( { id : graphId , nodes : nodes , edges : edges } , { status : 200 } )
14+ if ( ! dest ) {
15+ throw new Error ( "targetId is required" ) ;
16+ }
17+
18+ const { url, token } = getEnvVariables ( )
19+
20+ const result = await fetch ( `${ url } /find_paths` , {
21+ method : 'POST' ,
22+ headers : {
23+ "Authorization" : token ,
24+ 'Content-Type' : 'application/json'
25+ } ,
26+ body : JSON . stringify ( {
27+ repo,
28+ src,
29+ dest
30+ } ) ,
31+ cache : 'no-store'
32+ } )
33+
34+ if ( ! result . ok ) {
35+ throw new Error ( await result . text ( ) )
36+ }
37+
38+ const json = await result . json ( )
39+
40+ return NextResponse . json ( { result : json } , { status : 200 } )
41+ } catch ( err ) {
42+ console . error ( err )
43+ return NextResponse . json ( ( err as Error ) . message , { status : 400 } )
44+ }
2445}
0 commit comments