grpc_graphql_gateway 1.2.4

A Rust implementation of gRPC-GraphQL gateway - generates GraphQL execution code from gRPC services
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
//! Compression Demo - Realistic GraphQL Response Scenarios
//!
//! Demonstrates bandwidth savings on realistic GraphQL responses

use grpc_graphql_gateway::gbp::GbpEncoder;
use serde_json::json;

fn main() {
    println!("\n╔══════════════════════════════════════════════════════════╗");
    println!("║  GraphQL Binary Protocol - Compression Analysis         ║");
    println!("╚══════════════════════════════════════════════════════════╝\n");

    // Scenario 1: Small response (single user query)
    println!("📊 Scenario 1: Single User Query");
    println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");

    let single_user = json!({
        "data": {
            "user": {
                "id": "123",
                "name": "Alice Johnson",
                "email": "alice@example.com",
                "role": "ADMIN",
                "createdAt": "2024-01-15T10:30:00Z"
            }
        }
    });

    analyze_compression("Single User", &single_user);

    // Scenario 2: List of 10 users
    println!("\n📊 Scenario 2: List of 10 Users");
    println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");

    let users_10 = json!({
        "data": {
            "users": (0..10).map(|i| json!({
                "id": format!("user-{}", i),
                "name": format!("User {}", i),
                "email": format!("user{}@example.com", i),
                "role": "MEMBER",
                "status": "ACTIVE",
                "organization": {
                    "id": "org-1",
                    "name": "Acme Corp",
                    "tier": "ENTERPRISE"
                },
                "createdAt": "2024-01-15T10:30:00Z"
            })).collect::<Vec<_>>()
        }
    });

    analyze_compression("10 Users", &users_10);

    // Scenario 3: List of 100 users
    println!("\n📊 Scenario 3: List of 100 Users");
    println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");

    let users_100 = json!({
        "data": {
            "users": (0..100).map(|i| json!({
                "id": format!("user-{}", i),
                "name": format!("User {}", i),
                "email": format!("user{}@example.com", i),
                "role": if i % 10 == 0 { "ADMIN" } else { "MEMBER" },
                "status": "ACTIVE",
                "organization": {
                    "id": "org-1",
                    "name": "Acme Corp",
                    "tier": "ENTERPRISE",
                    "settings": {
                        "theme": "dark",
                        "notifications": true,
                        "language": "en"
                    }
                },
                "permissions": ["READ", "WRITE", "EXECUTE"],
                "createdAt": "2024-01-15T10:30:00Z",
                "updatedAt": "2024-01-20T14:45:00Z"
            })).collect::<Vec<_>>()
        }
    });

    analyze_compression("100 Users", &users_100);

    // Scenario 4: List of 1000 users (realistic production query)
    println!("\n📊 Scenario 4: List of 1000 Users (Production Scale)");
    println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");

    let users_1000 = json!({
        "data": {
            "users": (0..1000).map(|i| json!({
                "id": format!("user-{}", i),
                "name": format!("User {}", i),
                "email": format!("user{}@example.com", i),
                "role": if i % 10 == 0 { "ADMIN" } else { "MEMBER" },
                "status": "ACTIVE",
                "organization": {
                    "id": "org-1",
                    "name": "Acme Corp",
                    "tier": "ENTERPRISE",
                    "settings": {
                        "theme": "dark",
                        "notifications": true,
                        "language": "en"
                    }
                },
                "permissions": ["READ", "WRITE", "EXECUTE"],
                "profile": {
                    "avatar": format!("https://cdn.example.com/avatars/{}.jpg", i),
                    "bio": "Software engineer passionate about GraphQL and performance",
                    "location": "San Francisco, CA"
                },
                "createdAt": "2024-01-15T10:30:00Z",
                "updatedAt": "2024-01-20T14:45:00Z"
            })).collect::<Vec<_>>()
        }
    });

    analyze_compression("1000 Users", &users_1000);

    // Scenario 5: List of 5000 users (Large production query)
    println!("\n📊 Scenario 5: List of 5000 Users (Large Scale)");
    println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");

    let users_5000 = json!({
        "data": {
            "users": (0..5000).map(|i| json!({
                "id": format!("user-{}", i),
                "name": format!("User {}", i),
                "email": format!("user{}@example.com", i),
                "role": if i % 10 == 0 { "ADMIN" } else { "MEMBER" },
                "status": "ACTIVE",
                "organization": {
                    "id": "org-1",
                    "name": "Acme Corp",
                    "tier": "ENTERPRISE",
                    "settings": {
                        "theme": "dark",
                        "notifications": true,
                        "language": "en"
                    }
                },
                "permissions": ["READ", "WRITE", "EXECUTE"],
                "profile": {
                    "avatar": format!("https://cdn.example.com/avatars/{}.jpg", i),
                    "bio": "Software engineer passionate about GraphQL and performance",
                    "location": "San Francisco, CA",
                    "company": "Tech Corp",
                    "title": "Senior Engineer"
                },
                "metadata": {
                    "lastLogin": "2024-01-20T14:45:00Z",
                    "loginCount": 1523,
                    "apiKey": format!("key_{}_abcdef123456", i)
                },
                "createdAt": "2024-01-15T10:30:00Z",
                "updatedAt": "2024-01-20T14:45:00Z"
            })).collect::<Vec<_>>()
        }
    });

    analyze_compression("5000 Users", &users_5000);

    // Scenario 6: List of 10000 users (Very large query)
    println!("\n📊 Scenario 6: List of 10000 Users (Very Large Scale)");
    println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");

    let users_10000 = json!({
        "data": {
            "users": (0..10000).map(|i| json!({
                "id": format!("user-{}", i),
                "name": format!("User {}", i),
                "email": format!("user{}@example.com", i),
                "role": if i % 10 == 0 { "ADMIN" } else { "MEMBER" },
                "status": "ACTIVE",
                "organization": {
                    "id": "org-1",
                    "name": "Acme Corp",
                    "tier": "ENTERPRISE",
                    "settings": {
                        "theme": "dark",
                        "notifications": true,
                        "language": "en"
                    }
                },
                "permissions": ["READ", "WRITE", "EXECUTE"],
                "profile": {
                    "avatar": format!("https://cdn.example.com/avatars/{}.jpg", i),
                    "bio": "Software engineer passionate about GraphQL and performance optimization",
                    "location": "San Francisco, CA",
                    "company": "Tech Corp",
                    "title": "Senior Engineer"
                },
                "metadata": {
                    "lastLogin": "2024-01-20T14:45:00Z",
                    "loginCount": 1523,
                    "apiKey": format!("key_{}_abcdef123456", i),
                    "sessionToken": format!("sess_{}_xyz789", i)
                },
                "createdAt": "2024-01-15T10:30:00Z",
                "updatedAt": "2024-01-20T14:45:00Z"
            })).collect::<Vec<_>>()
        }
    });

    analyze_compression("10000 Users", &users_10000);

    // Scenario 7: List of 50000 users with random enums (Extreme scale)
    println!("\n📊 Scenario 7: List of 50000 Users with Random Enums (EXTREME SCALE)");
    println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");

    let roles = ["ADMIN", "MEMBER", "MODERATOR", "GUEST", "DEVELOPER"];
    let statuses = ["ACTIVE", "INACTIVE", "PENDING", "SUSPENDED", "VERIFIED"];
    let tiers = ["FREE", "BASIC", "PRO", "ENTERPRISE", "ULTIMATE"];
    let themes = ["light", "dark", "auto", "midnight", "ocean"];
    let languages = ["en", "es", "fr", "de", "ja", "zh", "pt", "ru"];
    let locations = [
        "San Francisco, CA",
        "New York, NY",
        "London, UK",
        "Tokyo, Japan",
        "Berlin, Germany",
    ];

    let users_50000 = json!({
        "data": {
            "users": (0..50000).map(|i| {
                let role_idx = i % roles.len();
                let status_idx = (i * 3) % statuses.len();
                let tier_idx = (i * 7) % tiers.len();
                let theme_idx = (i * 11) % themes.len();
                let lang_idx = (i * 13) % languages.len();
                let loc_idx = (i * 17) % locations.len();

                json!({
                    "id": format!("user-{}", i),
                    "name": format!("User {}", i),
                    "email": format!("user{}@example.com", i),
                    "role": roles[role_idx],
                    "status": statuses[status_idx],
                    "organization": {
                        "id": format!("org-{}", i / 100),
                        "name": "Acme Corp",
                        "tier": tiers[tier_idx],
                        "settings": {
                            "theme": themes[theme_idx],
                            "notifications": i % 2 == 0,
                            "language": languages[lang_idx]
                        }
                    },
                    "permissions": ["READ", "WRITE", "EXECUTE"],
                    "profile": {
                        "avatar": format!("https://cdn.example.com/avatars/{}.jpg", i),
                        "bio": "Software engineer passionate about GraphQL and performance optimization",
                        "location": locations[loc_idx],
                        "company": "Tech Corp",
                        "title": if i % 10 == 0 { "Senior Engineer" } else { "Engineer" },
                        "verified": i % 3 == 0
                    },
                    "metadata": {
                        "lastLogin": "2024-01-20T14:45:00Z",
                        "loginCount": 1000 + (i % 5000),
                        "apiKey": format!("key_{}_abcdef123456", i),
                        "sessionToken": format!("sess_{}_xyz789", i),
                        "deviceType": if i % 3 == 0 { "mobile" } else if i % 3 == 1 { "desktop" } else { "tablet" }
                    },
                    "createdAt": "2024-01-15T10:30:00Z",
                    "updatedAt": "2024-01-20T14:45:00Z"
                })
            }).collect::<Vec<_>>()
        }
    });

    analyze_compression("50000 Users", &users_50000);

    // Scenario 8: Medium Case - More repetition (shared organization data)
    println!("\n📊 Scenario 8: 50000 Users - Medium Repetition (Shared Orgs)");
    println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");

    let users_50k_medium = json!({
        "data": {
            "users": (0..50000).map(|i| json!({
                "id": format!("user-{}", i),
                "name": format!("User {}", i),
                "email": format!("user{}@example.com", i),
                "role": "MEMBER",  // Same for all
                "status": "ACTIVE",  // Same for all
                "organization": {
                    "id": "org-1",  // All same org
                    "name": "Acme Corp",  // Same
                    "tier": "ENTERPRISE",  // Same
                    "settings": {
                        "theme": "dark",  // Same
                        "notifications": true,  // Same
                        "language": "en"  // Same
                    }
                },
                "permissions": ["READ", "WRITE", "EXECUTE"],  // Same
                "profile": {
                    "avatar": format!("https://cdn.example.com/avatars/{}.jpg", i),
                    "bio": "Software engineer passionate about GraphQL and performance optimization",  // Same
                    "location": "San Francisco, CA",  // Same
                    "company": "Tech Corp",  // Same
                    "title": "Engineer",  // Same
                    "verified": true  // Same
                },
                "metadata": {
                    "lastLogin": "2024-01-20T14:45:00Z",  // Same
                    "loginCount": 1500,  // Same
                    "apiKey": format!("key_{}_abcdef123456", i),
                    "sessionToken": format!("sess_{}_xyz789", i),
                    "deviceType": "desktop"  // Same
                },
                "createdAt": "2024-01-15T10:30:00Z",  // Same
                "updatedAt": "2024-01-20T14:45:00Z"  // Same
            })).collect::<Vec<_>>()
        }
    });

    analyze_compression("50000 Users (Medium Repetition)", &users_50k_medium);

    // Scenario 8b: Realistic Mid-Case - Product Catalog (E-commerce scenario)
    println!("\n📊 Scenario 8b: 50000 Products - Realistic Mid-Case (85-90% Zone)");
    println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");

    let categories = ["Electronics", "Clothing", "Books", "Home", "Sports"];
    let brands = ["BrandA", "BrandB", "BrandC", "BrandD", "BrandE"];
    let statuses = ["IN_STOCK", "LOW_STOCK", "OUT_OF_STOCK"];
    let conditions = ["NEW", "REFURBISHED", "USED"];

    let products_50k = json!({
        "data": {
            "products": (0..50000).map(|i| {
                let cat_idx = i % categories.len();
                let brand_idx = (i / 100) % brands.len();
                let status_idx = (i / 10) % statuses.len();
                let condition_idx = i % conditions.len();

                json!({
                    "id": i,  // Simple numeric ID
                    "sku": format!("SKU-{}", i),  // Short SKU
                    "name": format!("Product {}", i),
                    "category": categories[cat_idx],
                    "brand": brands[brand_idx],
                    "price": 99.99,  // Same price (common in bulk catalogs)
                    "currency": "USD",
                    "status": statuses[status_idx],
                    "condition": conditions[condition_idx],
                    "inventory": {
                        "warehouse": "WH-01",  // Same warehouse
                        "location": "A-1",  // Same location
                        "quantity": 100,  // Same quantity
                        "reserved": 10  // Same reserved
                    },
                    "shipping": {
                        "weight": 1.5,  // Same weight
                        "dimensions": {
                            "length": 10.0,
                            "width": 8.0,
                            "height": 2.0
                        },
                        "freeShipping": true
                    },
                    "ratings": {
                        "average": 4.5,  // Same rating
                        "count": 1234,  // Same count
                        "distribution": {
                            "5star": 60,
                            "4star": 25,
                            "3star": 10,
                            "2star": 3,
                            "1star": 2
                        }
                    },
                    "tags": ["featured", "bestseller", "new"],  // Same tags
                    "metadata": {
                        "createdAt": "2024-01-15T10:30:00Z",
                        "updatedAt": "2024-01-20T14:45:00Z",
                        "createdBy": "admin",
                        "version": 1
                    }
                })
            }).collect::<Vec<_>>()
        }
    });

    analyze_compression("50000 Products (Mid-Case)", &products_50k);

    // Scenario 9: Extreme Case - Maximum repetition (cache scenario)
    println!("\n📊 Scenario 9: 50000 Users - Extreme Repetition (97%+ Zone)");
    println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");

    let users_50k_extreme = json!({
        "data": {
            "users": (0..50000).map(|i| json!({
                "id": i,  // Just a number
                "name": "User",  // Same
                "email": "user@example.com",  // Same
                "role": "MEMBER",
                "status": "ACTIVE",
                "organization": {
                    "id": "org-1",
                    "name": "Acme Corp",
                    "tier": "ENTERPRISE",
                    "settings": {
                        "theme": "dark",
                        "notifications": true,
                        "language": "en"
                    }
                },
                "permissions": ["READ", "WRITE", "EXECUTE"],
                "profile": {
                    "avatar": "https://cdn.example.com/default.jpg",  // Same
                    "bio": "User profile",  // Same
                    "location": "San Francisco, CA",
                    "company": "Tech Corp",
                    "title": "Engineer",
                    "verified": true
                },
                "metadata": {
                    "lastLogin": "2024-01-20T14:45:00Z",
                    "loginCount": 1500,
                    "apiKey": "key_default",  // Same
                    "sessionToken": "sess_default",  // Same
                    "deviceType": "desktop"
                },
                "createdAt": "2024-01-15T10:30:00Z",
                "updatedAt": "2024-01-20T14:45:00Z"
            })).collect::<Vec<_>>()
        }
    });

    analyze_compression("50000 Users (Extreme Repetition)", &users_50k_extreme);

    // Summary
    println!("\n╔══════════════════════════════════════════════════════════╗");
    println!("║  💡 Key Insights                                         ║");
    println!("╠══════════════════════════════════════════════════════════╣");
    println!("║  • Small payloads (<1KB): 10-20% savings                 ║");
    println!("║  • Medium payloads (1-10KB): 30-50% savings              ║");
    println!("║  • Large payloads (10KB+): 70-99% savings                ║");
    println!("║                                                          ║");
    println!("║  Why GBP is so effective:                                ║");
    println!("║  1. Field names stored once in string pool              ║");
    println!("║  2. Object shapes cached and reused                     ║");
    println!("║  3. Columnar encoding for arrays                         ║");
    println!("║  4. Run-length encoding for repeated values              ║");
    println!("╚══════════════════════════════════════════════════════════╝\n");
}

fn analyze_compression(label: &str, data: &serde_json::Value) {
    let json_bytes = serde_json::to_vec(data).unwrap();
    let json_size = json_bytes.len();

    let mut encoder = GbpEncoder::new();
    let gbp_bytes = encoder.encode(data);
    let gbp_size = gbp_bytes.len();

    let saved_bytes = json_size as i64 - gbp_size as i64;
    let saved_percent = (saved_bytes as f64 / json_size as f64) * 100.0;
    let ratio = json_size as f64 / gbp_size as f64;

    println!("Query: {}", label);
    println!("  JSON:         {:>10}", format_bytes(json_size));
    println!("  Binary (GBP): {:>10}", format_bytes(gbp_size));
    println!("  ─────────────────────────────");
    if saved_bytes >= 0 {
        println!(
            "  Saved:        {:>10} ({:.1}%)",
            format_bytes(saved_bytes as usize),
            saved_percent
        );
    } else {
        println!(
            "  Overhead:     {:>10} ({:.1}%)",
            format_bytes((-saved_bytes) as usize),
            -saved_percent
        );
    }
    println!("  Ratio:        {:.2}x", ratio);

    // Bandwidth savings for 1 million requests
    let monthly_requests = 1_000_000;
    let monthly_json = json_size * monthly_requests;
    let monthly_gbp = gbp_size * monthly_requests;
    let monthly_saved = monthly_json as i64 - monthly_gbp as i64;

    println!("\n  📈 At 1M requests/month:");
    println!("     JSON:     {}", format_bytes(monthly_json));
    println!("     Binary:   {}", format_bytes(monthly_gbp));
    if monthly_saved >= 0 {
        println!(
            "     💰 Saved: {} ({:.1}%)",
            format_bytes(monthly_saved as usize),
            saved_percent
        );
    } else {
        println!(
            "     ⚠️  Cost:  {} extra ({:.1}%)",
            format_bytes((-monthly_saved) as usize),
            -saved_percent
        );
    }
}

fn format_bytes(bytes: usize) -> String {
    if bytes < 1024 {
        format!("{} B", bytes)
    } else if bytes < 1024 * 1024 {
        format!("{:.2} KB", bytes as f64 / 1024.0)
    } else if bytes < 1024 * 1024 * 1024 {
        format!("{:.2} MB", bytes as f64 / (1024.0 * 1024.0))
    } else {
        format!("{:.2} GB", bytes as f64 / (1024.0 * 1024.0 * 1024.0))
    }
}