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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
use std::collections::HashMap;
use std::hash::Hash;
use anyhow::Result;
use async_stream::try_stream;
use futures::stream::Stream;
use serde::de::DeserializeOwned;
use serde::Deserialize;
use crate::client::ILazyClient;
use crate::{Query, TxnReadOnlyType};
#[derive(Deserialize)]
struct Chunk<T> {
items: Vec<T>,
}
impl<T: DeserializeOwned> Default for Chunk<T> {
fn default() -> Self {
Self {
items: Vec::with_capacity(0),
}
}
}
impl<C: ILazyClient> TxnReadOnlyType<C> {
async fn fetch_chunk<Q, T>(&mut self, query: Q, vars: HashMap<String, String>) -> Result<Vec<T>>
where
Q: Into<String> + Send + Sync,
T: DeserializeOwned,
{
let chunk: Chunk<T> = self.query_with_vars(query, vars).await?.try_into_owned()?;
Ok(chunk.items)
}
///
/// Readonly transaction is transformed into async stream.
///
/// Input `query` must accept **$first: string, $offset: string** arguments which are used for paginating.
/// Stream items must be returned in query block named **items**.
///
/// # Return
///
/// Stream contains deserialized items returned from query.
/// Stream item is Ok(T) if **items** query data can be serialized into Vec<T>.
///
/// # Arguments
///
/// - `query`: GraphQL+- query segment.
/// - `first`: number of items returned in one chunk
///
/// # Errors
///
/// * gRPC error
/// * If transaction is not initialized properly, return `EmptyTxn` error.
///
/// # Example
///
/// ```
/// use std::collections::HashMap;
/// use anyhow::Result;
/// use futures::pin_mut;
/// use futures::stream::StreamExt;
/// use dgraph_tonic::Client;
/// use serde::Deserialize;
/// #[cfg(feature = "acl")]
/// use dgraph_tonic::{AclClientType, LazyChannel};
///
/// #[cfg(not(feature = "acl"))]
/// async fn client() -> Client {
/// Client::new("http://127.0.0.1:19080").expect("Dgraph client")
/// }
///
/// #[cfg(feature = "acl")]
/// async fn client() -> AclClientType<LazyChannel> {
/// let default = Client::new("http://127.0.0.1:19080").unwrap();
/// default.login("groot", "password").await.expect("Acl client")
/// }
///
/// #[derive(Deserialize, Debug)]
/// struct Person {
/// uid: String,
/// name: String,
/// }
///
///
/// #[tokio::main]
/// async fn main() {
/// let query = r#"query stream($first: string, $offset: string) {
/// items(func: eq(name, "Alice"), first: $first, offset: $offset) {
/// uid
/// name
/// }
/// }"#;
///
/// let client = client().await;
/// let stream = client.new_read_only_txn().into_stream(query,100);
/// pin_mut!(stream);
/// let alices: Vec<Result<Person>> = stream.collect().await;
/// }
/// ```
///
pub fn into_stream<Q, T>(self, query: Q, first: usize) -> impl Stream<Item = Result<T>>
where
Q: Into<String> + Send + Sync,
T: Unpin + DeserializeOwned,
{
self.into_stream_with_vars(query, HashMap::<String, String>::new(), first)
}
///
/// Readonly transaction is transformed into async stream.
///
/// Input `query` must accept **$first: string, $offset: string** arguments which are used for paginating.
/// Stream items must be returned in query block named **items**.
///
/// # Return
///
/// Stream contains deserialized items returned from query.
/// Stream item is Ok(T) if **items** query data can be serialized into Vec<T>.
///
/// # Arguments
///
/// - `query`: GraphQL+- query segment.
/// - `vars`: map of variables for query
/// - `first`: number of items returned in one chunk
///
/// # Errors
///
/// * gRPC error
/// * If transaction is not initialized properly, return `EmptyTxn` error.
///
/// # Example
///
/// ```
/// use std::collections::HashMap;
/// use anyhow::Result;
/// use futures::pin_mut;
/// use futures::stream::StreamExt;
/// use dgraph_tonic::{Client, Query};
/// use serde::Deserialize;
/// #[cfg(feature = "acl")]
/// use dgraph_tonic::{AclClientType, LazyChannel};
///
/// #[cfg(not(feature = "acl"))]
/// async fn client() -> Client {
/// Client::new("http://127.0.0.1:19080").expect("Dgraph client")
/// }
///
/// #[cfg(feature = "acl")]
/// async fn client() -> AclClientType<LazyChannel> {
/// let default = Client::new("http://127.0.0.1:19080").unwrap();
/// default.login("groot", "password").await.expect("Acl client")
/// }
///
/// #[derive(Deserialize, Debug)]
/// struct Person {
/// uid: String,
/// name: String,
/// }
///
///
/// #[tokio::main]
/// async fn main() {
/// let query = r#"query stream($first: string, $offset: string, $name: string) {
/// items(func: eq(name, $name), first: $first, offset: $offset) {
/// uid
/// name
/// }
/// }"#;
///
/// let mut vars = HashMap::new();
/// vars.insert("$name", "Alice");
/// let client = client().await;
/// let stream = client.new_read_only_txn().into_stream_with_vars(query, vars, 100);
/// pin_mut!(stream);
/// let alices: Vec<Result<Person>> = stream.collect().await;
/// }
/// ```
///
pub fn into_stream_with_vars<Q, T, K, V>(
mut self,
query: Q,
vars: HashMap<K, V>,
first: usize,
) -> impl Stream<Item = Result<T>>
where
Q: Into<String> + Send + Sync,
T: Unpin + DeserializeOwned,
K: Into<String> + Send + Sync + Eq + Hash,
V: Into<String> + Send + Sync,
{
assert_ne!(
first, 0,
"First attribute for stream must not be eq to zero"
);
let mut vars = vars.into_iter().fold(HashMap::new(), |mut tmp, (k, v)| {
tmp.insert(k.into(), v.into());
tmp
});
vars.insert(String::from("$first"), format!("{}", first));
let query = query.into();
try_stream! {
let mut offset = 0;
loop {
vars.insert(String::from("$offset"), format!("{}", offset));
let chunk = self
.fetch_chunk(query.to_owned(), vars.to_owned())
.await?;
if chunk.is_empty() {
break;
};
let chunk_len = chunk.len();
for item in chunk {
offset += 1;
yield item
}
if chunk_len < first {
break;
}
}
}
}
}
#[cfg(test)]
mod tests {
use std::collections::HashMap;
use anyhow::Result;
use futures::pin_mut;
use futures::stream::StreamExt;
use serde_derive::{Deserialize, Serialize};
use crate::client::Client;
#[cfg(feature = "acl")]
use crate::client::{AclClientType, LazyChannel};
use crate::{Mutate, Mutation};
#[cfg(not(feature = "acl"))]
async fn client() -> Client {
Client::new("http://127.0.0.1:19080").unwrap()
}
#[cfg(feature = "acl")]
async fn client() -> AclClientType<LazyChannel> {
let default = Client::new("http://127.0.0.1:19080").unwrap();
default.login("groot", "password").await.unwrap()
}
#[derive(Serialize, Deserialize, Default, Debug)]
struct Car {
uid: String,
color: String,
}
#[derive(Serialize, Deserialize, Default, Debug)]
struct Person {
uid: String,
name: String,
}
#[tokio::test]
async fn stream() {
let client = client().await;
client.drop_all().await.expect("Data not dropped");
client
.set_schema("color: string @index(exact) .")
.await
.expect("Schema is not updated");
let txn = client.new_mutated_txn();
let data = vec![
Car {
uid: "_:a".to_string(),
color: "A".to_string(),
},
Car {
uid: "_:b".to_string(),
color: "B".to_string(),
},
Car {
uid: "_:c".to_string(),
color: "C".to_string(),
},
];
let mut mu = Mutation::new();
mu.set_set_json(&data).expect("Invalid JSON");
let response = txn.mutate_and_commit_now(mu).await;
assert!(response.is_ok());
let stream = client.new_read_only_txn().into_stream(
r#"
query stream($first: string, $offset: string) {
items(func: has(color), first: $first, offset: $offset) {{
uid
color
}}
}
"#,
2,
);
pin_mut!(stream);
let cars: Vec<Result<Car>> = stream.collect().await;
assert_eq!(cars.len(), 3);
assert!(cars.iter().all(|car| car.is_ok()))
}
#[tokio::test]
async fn stream_with_vars() {
let client = client().await;
client.drop_all().await.expect("Data not dropped");
client
.set_schema("color: string @index(exact) .")
.await
.expect("Schema is not updated");
let txn = client.new_mutated_txn();
let data = vec![
Car {
uid: "_:a".to_string(),
color: "A".to_string(),
},
Car {
uid: "_:b".to_string(),
color: "A".to_string(),
},
Car {
uid: "_:c".to_string(),
color: "C".to_string(),
},
];
let mut mu = Mutation::new();
mu.set_set_json(&data).expect("Invalid JSON");
let response = txn.mutate_and_commit_now(mu).await;
assert!(response.is_ok());
let mut vars = HashMap::new();
vars.insert("$color", "A");
let stream = client.new_read_only_txn().into_stream_with_vars(
r#"
query stream($first: string, $offset: string, $color: string) {
items(func: eq(color, "A"), first: $first, offset: $offset) {{
uid
color
}}
}
"#,
vars,
2,
);
pin_mut!(stream);
let cars: Vec<Result<Car>> = stream.collect().await;
assert_eq!(cars.len(), 2);
assert!(cars.iter().all(|car| car.is_ok()))
}
#[tokio::test]
async fn invalid_data_in_stream() {
let client = client().await;
client.drop_all().await.expect("Data not dropped");
client
.set_schema("color: string @index(exact) .")
.await
.expect("Schema is not updated");
let txn = client.new_mutated_txn();
let data = vec![
Car {
uid: "_:a".to_string(),
color: "A".to_string(),
},
Car {
uid: "_:b".to_string(),
color: "B".to_string(),
},
Car {
uid: "_:c".to_string(),
color: "C".to_string(),
},
];
let mut mu = Mutation::new();
mu.set_set_json(&data).expect("Invalid JSON");
let response = txn.mutate_and_commit_now(mu).await;
assert!(response.is_ok());
let stream = client.new_read_only_txn().into_stream(
r#"
query stream($first: string, $offset: string) {
items(func: has(color), first: $first, offset: $offset) {{
uid
color
}}
}
"#,
2,
);
pin_mut!(stream);
let cars: Vec<Result<Person>> = stream.collect().await;
assert_eq!(cars.len(), 1);
assert!(cars.iter().all(|car| car.is_err()))
}
}