1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use crate::graph_loader::source::neo4j_loader::Neo4JConnection;
use neo4rs::*;
use raphtory::core::Prop;
use raphtory::db::graph as rap;

fn load_movies(row: Row, graph: &rap::Graph) {
    let film: Node = row.get("film").unwrap();
    let film_title: String = film.get("title").unwrap();
    let film_tagline: String = film.get("tagline").unwrap_or("No tagline :(".to_string());
    let film_release: i64 = film.get("released").unwrap();

    let actor: Node = row.get("actor").unwrap();
    let actor_name: String = actor.get("name").unwrap();
    let actor_born: i64 = actor.get("born").unwrap_or(film_release);

    let relation: Relation = row.get("relation").unwrap();
    let relation_type = relation.typ();

    graph
        .add_vertex(actor_born, actor_name.clone(), &vec![])
        .unwrap();
    graph
        .add_vertex_properties(
            actor_name.clone(),
            &vec![("type".into(), Prop::Str("actor".into()))],
        )
        .unwrap();
    graph
        .add_vertex(film_release, film_title.clone(), &vec![])
        .unwrap();
    graph
        .add_vertex_properties(
            film_title.clone(),
            &vec![
                ("type".into(), Prop::Str("film".into())),
                ("tagline".into(), Prop::Str(film_tagline)),
            ],
        )
        .unwrap();
    graph
        .add_edge(
            film_release,
            actor_name,
            film_title,
            &vec![],
            Some(relation_type.as_str()),
        )
        .unwrap();
}

pub async fn neo4j_movie_graph(
    uri: String,
    username: String,
    password: String,
    database: String,
    shards: usize,
) -> rap::Graph {
    let g = rap::Graph::new(shards);
    let neo = Neo4JConnection::new(uri, username, password, database)
        .await
        .unwrap();

    neo.load_query_into_graph(
        &g,
        query("MATCH (actor:Person) -[relation]-> (film:Movie) RETURN actor,relation,film"),
        load_movies,
    )
    .await
    .unwrap();
    g
}