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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
use failure::Error;
use oatie::doc::*;
use oatie::rtf::*;
use reqwest;
use serde_json;
pub fn get_all_pages_graphql() -> Option<Vec<String>> {
let client = reqwest::Client::new();
let text = client
.post("http://127.0.0.1:8003/graphql/")
.json(&json!({
"query": r#"
query {
pages {
id
}
}
"#,
}))
.send()
.ok()?
.text()
.ok()?;
let ret: ::serde_json::Value = serde_json::from_str(&text).ok()?;
let node = ret.pointer("/data/pages")?;
Some(
node.as_array()?
.iter()
.map(|x| x.pointer("/id").unwrap().as_str().unwrap().to_string())
.collect::<Vec<_>>(),
)
}
pub fn get_single_page_graphql(input_id: &str) -> Option<Doc<RtfSchema>> {
let client = reqwest::Client::new();
let text = client
.post("http://127.0.0.1:8003/graphql/")
.json(&json!({
"query": r#"
query ($id: String!) {
page(id: $id) {
doc
}
"#,
"variables": {
"id": input_id,
},
}))
.send()
.ok()?
.text()
.ok()?;
let ret: ::serde_json::Value = serde_json::from_str(&text).ok()?;
let node = ret.pointer("/data/page/doc")?;
let ron = node.as_str()?.to_string();
let body = ::ron::de::from_str(&ron).ok()?;
Some(Doc(body))
}
pub fn graphql_request(
query: &str,
variables: &serde_json::Value,
) -> Result<serde_json::Value, Error> {
let client = reqwest::Client::new();
let text = client
.post("http://127.0.0.1:8003/graphql/")
.json(&json!({
"query": query,
"variables": variables,
}))
.send()?
.text()?;
Ok(serde_json::from_str(&text)?)
}
pub fn get_or_create_page_graphql(
input_id: &str,
doc: &Doc<RtfSchema>,
) -> Result<Doc<RtfSchema>, Error> {
let ret = graphql_request(
r#"
mutation ($id: String!, $default: String!) {
getOrCreatePage(id: $id, default: $default) {
doc
}
}
"#,
&json!({
"id": input_id,
"default": ::ron::ser::to_string(&doc.0).unwrap(),
}),
)?;
let doc_string = ret
.pointer("/data/getOrCreatePage/doc")
.ok_or(format_err!("unexpected json structure"))?
.as_str()
.unwrap()
.to_string();
Ok(oatie::deserialize::doc_ron(&doc_string).or(oatie::deserialize::doc_json(&doc_string))?)
}
pub fn create_page_graphql(input_id: &str, doc: &Doc<RtfSchema>) -> Option<Doc<RtfSchema>> {
let client = reqwest::Client::new();
let text = client
.post("http://127.0.0.1:8003/graphql/")
.json(&json!({
"query": r#"
mutation ($id: String!, $doc: String!) {
createPage(id: $id, doc: $doc) {
doc
}
}
"#,
"variables": {
"id": input_id,
"doc": ::ron::ser::to_string(&doc.0).unwrap(),
},
}))
.send()
.ok()?
.text()
.ok()?;
let ret: ::serde_json::Value = serde_json::from_str(&text).ok()?;
let node = ret.pointer("/data/createPage/doc")?;
let ron = node.as_str()?.to_string();
let body = ::ron::de::from_str(&ron).ok()?;
Some(Doc(body))
}