Repository navigation
Expand file tree
/
Copy pathdebug_batch.rs
More file actions
66 lines (55 loc) · 1.71 KB
/
Copy pathdebug_batch.rs
File metadata and controls
66 lines (55 loc) · 1.71 KB
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
#![allow(dead_code)] // Silencia warnings de código não utilizado
use async_graphql_dataloader::{BatchLoad, DataLoader};
use std::collections::HashMap;
use std::time::Instant;
#[derive(Clone, Debug)]
struct User {
id: i32,
name: String,
}
struct UserLoader;
#[async_trait::async_trait]
impl BatchLoad for UserLoader {
type Key = i32;
type Value = User;
type Error = String;
async fn load(&self, keys: &[i32]) -> HashMap<i32, Result<User, String>> {
println!(
"🔥 BATCH EXECUTED - Loading {} users: {:?}",
keys.len(),
keys
);
keys.iter()
.map(|&id| {
(
id,
Ok(User {
id,
name: format!("User {}", id),
}),
)
})
.collect()
}
}
#[tokio::main]
async fn main() {
println!("🚀 STARTING DEBUG EXAMPLE...");
let start = Instant::now();
let user_loader = DataLoader::new(UserLoader)
.with_delay(std::time::Duration::from_millis(1)) // Delay muito curto
.with_max_batch_size(10);
println!("1. Loading single user...");
let result1 = user_loader.load(1).await;
println!(" Result 1: {:?}", result1);
println!("2. Loading multiple users...");
let result2 = user_loader.load(2).await;
let result3 = user_loader.load(3).await;
println!(" Result 2: {:?}", result2);
println!(" Result 3: {:?}", result3);
println!("3. Testing cache...");
let cached = user_loader.load(1).await;
println!(" Cached: {:?}", cached);
let duration = start.elapsed();
println!("🎉 DEBUG COMPLETED in {:?}", duration);
}