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
72
73
74
use crate::query::Query;
use crate::ToQueryString;
use crate::mutation::Mutation;
use crate::condition::Condition;
use serde::Serialize;

#[derive(Clone, Serialize)]
pub struct Upsert {
    pub query: String,
    pub mutations: Vec<UpsertBlock>
}

impl Upsert {
    pub fn new(query: Query) -> Self {
        Self {
            query: query.to_query_string(),
            mutations: vec![]
        }
    }

    pub fn upsert_block(mut self, block: UpsertBlock) -> Self {
        self.mutations.push(block);
        self
    }

    pub fn upsert_block_ref(&mut self, block: UpsertBlock) {
        self.mutations.push(block);
    }
}


#[derive(Serialize, Clone)]
pub struct UpsertBlock {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cond: Option<String>,
    #[serde(flatten)]
    pub mutation: Mutation
}

impl UpsertBlock {
    pub fn new(mutation: Mutation) -> Self {
        Self {
            cond: None,
            mutation
        }
    }

    pub fn cond(mut self, condition: Condition) -> Self {
        self.cond = Some(format!("@if({})", condition.to_query_string()));
        self
    }
}

/*

{
  "query": "{q1(func: regexp(ip_value, /^185.+$/)) {u1 as uid} }",
  "mutations": [
    {
      "cond": "@if(eq(len(u1), 0))",
      "set": [
        {
          "uid": "uid(u1)",
          "name": "FAKE"
        }
      ]
    }
  ]
}


Example ^

 */