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
use dynamic_graphql::SimpleObject;
use raphtory::{core::Lifespan, vectors::Document};

#[derive(SimpleObject)]
pub struct GqlDocument {
    /// Return a vector with the name of the node or the names of src and dst of the edge: [src, dst]
    name: Vec<String>, // size 1 for nodes, size 2 for edges: [src, dst]
    /// Return the type of entity: "node" or "edge"
    entity_type: String,
    content: String,
    life: Vec<i64>,
}

impl From<Document> for GqlDocument {
    fn from(value: Document) -> Self {
        match value {
            Document::Graph {
                name,
                content,
                life,
            } => Self {
                name: vec![name],
                entity_type: "graph".to_owned(),
                content,
                life: lifespan_into_vec(life),
            },
            Document::Node {
                name,
                content,
                life,
            } => Self {
                name: vec![name],
                entity_type: "node".to_owned(),
                content,
                life: lifespan_into_vec(life),
            },
            Document::Edge {
                src,
                dst,
                content,
                life,
            } => Self {
                name: vec![src, dst],
                entity_type: "edge".to_owned(),
                content,
                life: lifespan_into_vec(life),
            },
        }
    }
}

fn lifespan_into_vec(life: Lifespan) -> Vec<i64> {
    match life {
        Lifespan::Inherited => vec![],
        Lifespan::Event { time } => vec![time],
        Lifespan::Interval { start, end } => vec![start, end],
    }
}