reinhardt-graphql 0.3.1

GraphQL API support for Reinhardt (facade crate)
Documentation
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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
use async_graphql::extensions::Analyzer;
use async_graphql::{Context, EmptySubscription, ID, Object, Result as GqlResult, Schema};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;

/// Error types for GraphQL schema operations.
#[derive(Debug, thiserror::Error)]
pub enum GraphQLError {
	/// A schema construction or configuration error.
	#[error("Schema error: {0}")]
	Schema(String),
	/// An error occurred during resolver execution.
	#[error("Resolver error: {0}")]
	Resolver(String),
	/// The requested resource was not found.
	#[error("Not found: {0}")]
	NotFound(String),
}

/// A specialized `Result` type for GraphQL schema operations.
pub type GraphQLResult<T> = Result<T, GraphQLError>;

/// Default maximum query depth limit.
///
/// Limits how deeply nested a query can be to prevent resource exhaustion
/// from deeply nested selections.
pub const DEFAULT_MAX_QUERY_DEPTH: usize = 10;

/// Default maximum query complexity limit.
///
/// Limits total complexity score for a single query to prevent
/// resource exhaustion from expensive operations.
pub const DEFAULT_MAX_QUERY_COMPLEXITY: usize = 100;

/// Default maximum query size in bytes.
///
/// Prevents excessively large query strings from consuming parsing resources.
pub const DEFAULT_MAX_QUERY_SIZE: usize = 32_768; // 32 KB

/// Default maximum number of fields in a single query.
///
/// Prevents queries that request an excessive number of fields,
/// which could lead to resource exhaustion.
pub const DEFAULT_MAX_FIELD_COUNT: usize = 200;

/// Default maximum page size for paginated queries.
///
/// Prevents unbounded result sets that could cause memory exhaustion.
pub const DEFAULT_MAX_PAGE_SIZE: usize = 100;

/// Default page size for paginated queries.
pub const DEFAULT_PAGE_SIZE: usize = 20;

/// Maximum allowed length for user name input.
const MAX_NAME_LENGTH: usize = 100;

/// Maximum allowed length for email input.
const MAX_EMAIL_LENGTH: usize = 254;

/// Check whether a string exceeds the given character limit.
///
/// Uses short-circuit counting: stops as soon as `max + 1` characters
/// have been scanned, avoiding a full O(n) traversal for large inputs.
fn exceeds_max_chars(s: &str, max: usize) -> bool {
	s.chars().nth(max).is_some()
}

/// Configuration for GraphQL query protection limits.
///
/// Controls query depth, complexity, size, and field count limits to prevent
/// denial-of-service attacks through resource exhaustion.
///
/// # Examples
///
/// ```
/// use reinhardt_graphql::schema::QueryLimits;
///
/// // Use defaults
/// let limits = QueryLimits::default();
/// assert_eq!(limits.max_depth, 10);
/// assert_eq!(limits.max_complexity, 100);
/// assert_eq!(limits.max_query_size, 32_768);
/// assert_eq!(limits.max_field_count, 200);
///
/// // Custom limits
/// let limits = QueryLimits::new(15, 200);
/// assert_eq!(limits.max_depth, 15);
/// assert_eq!(limits.max_complexity, 200);
/// ```
#[derive(Debug, Clone, Copy)]
pub struct QueryLimits {
	/// Maximum allowed query depth
	pub max_depth: usize,
	/// Maximum allowed query complexity
	pub max_complexity: usize,
	/// Maximum allowed query string size in bytes
	pub max_query_size: usize,
	/// Maximum allowed number of fields in a query
	pub max_field_count: usize,
}

impl QueryLimits {
	/// Create a new `QueryLimits` with custom depth and complexity values.
	///
	/// Uses default values for query size and field count limits.
	pub fn new(max_depth: usize, max_complexity: usize) -> Self {
		Self {
			max_depth,
			max_complexity,
			max_query_size: DEFAULT_MAX_QUERY_SIZE,
			max_field_count: DEFAULT_MAX_FIELD_COUNT,
		}
	}

	/// Create a new `QueryLimits` with all values specified.
	pub fn full(
		max_depth: usize,
		max_complexity: usize,
		max_query_size: usize,
		max_field_count: usize,
	) -> Self {
		Self {
			max_depth,
			max_complexity,
			max_query_size,
			max_field_count,
		}
	}
}

impl Default for QueryLimits {
	fn default() -> Self {
		Self {
			max_depth: DEFAULT_MAX_QUERY_DEPTH,
			max_complexity: DEFAULT_MAX_QUERY_COMPLEXITY,
			max_query_size: DEFAULT_MAX_QUERY_SIZE,
			max_field_count: DEFAULT_MAX_FIELD_COUNT,
		}
	}
}

/// Validate a GraphQL query string against size and field count limits.
///
/// Returns `Ok(())` if the query passes all checks, or an error message
/// describing which limit was exceeded.
pub fn validate_query(query: &str, limits: &QueryLimits) -> Result<(), String> {
	// Check query size
	if query.len() > limits.max_query_size {
		return Err(format!(
			"Query size {} bytes exceeds maximum of {} bytes",
			query.len(),
			limits.max_query_size
		));
	}

	// Approximate field count by counting field-like tokens
	// A more accurate count would require parsing, but this provides
	// a reasonable heuristic for DoS prevention
	let field_count = count_query_fields(query);
	if field_count > limits.max_field_count {
		return Err(format!(
			"Query field count {} exceeds maximum of {}",
			field_count, limits.max_field_count
		));
	}

	Ok(())
}

/// GraphQL keywords that should not be counted as fields.
const GRAPHQL_KEYWORDS: &[&str] = &[
	"query",
	"mutation",
	"subscription",
	"fragment",
	"on",
	"true",
	"false",
	"null",
];

/// Check whether a token is a field-like identifier.
///
/// Returns `true` when the token looks like a GraphQL field name:
/// an alphanumeric identifier that is not a keyword and does not
/// start with a fragment spread (`...`).
fn is_field_identifier(token: &str) -> bool {
	!token.is_empty()
		&& !token.starts_with("...")
		&& !GRAPHQL_KEYWORDS.contains(&token)
		&& token
			.chars()
			.next()
			.is_some_and(|c| c.is_ascii_alphabetic() || c == '_')
}

/// Count approximate number of fields in a GraphQL query.
///
/// Counts field-like identifiers that appear inside selection sets
/// (brace depth > 0). Each identifier token is evaluated immediately
/// during character processing, so multiple fields on the same line
/// are counted correctly.
///
/// Handles inline fragment type conditions (`... on Type { }`) by
/// tracking the `on` keyword and skipping the subsequent type name.
/// Also handles block strings (`"""..."""`) to avoid miscounting
/// content inside them as fields.
fn count_query_fields(query: &str) -> usize {
	let mut count = 0;
	let mut in_string = false;
	let mut in_block_string = false;
	let mut depth: usize = 0;
	let mut token = String::new();
	let mut in_comment = false;
	let mut escaped = false;
	// Track whether the last flushed token was the `on` keyword,
	// so the next identifier (a type condition) is not counted as a field.
	let mut after_on_keyword = false;

	let chars: Vec<char> = query.chars().collect();
	let len = chars.len();
	let mut i = 0;

	while i < len {
		let ch = chars[i];

		if escaped {
			escaped = false;
			i += 1;
			continue;
		}

		// Handle block strings: skip everything until closing """
		if in_block_string {
			if ch == '"' && i + 2 < len && chars[i + 1] == '"' && chars[i + 2] == '"' {
				in_block_string = false;
				i += 3; // skip closing """
			} else {
				i += 1;
			}
			continue;
		}

		// Handle line comments: everything after '#' (outside strings) is ignored
		if ch == '\n' {
			in_comment = false;
			// Flush any accumulated token at end of line
			if depth > 0 && !in_string && is_field_identifier(&token) {
				if after_on_keyword {
					after_on_keyword = false;
				} else {
					count += 1;
				}
			}
			if !is_field_identifier(&token) {
				after_on_keyword = false;
			}
			token.clear();
			i += 1;
			continue;
		}

		if in_comment {
			i += 1;
			continue;
		}

		if in_string {
			match ch {
				'\\' => escaped = true,
				'"' => in_string = false,
				_ => {}
			}
			i += 1;
			continue;
		}

		match ch {
			'#' => {
				// Flush token before comment starts
				if depth > 0 && is_field_identifier(&token) {
					if after_on_keyword {
						after_on_keyword = false;
					} else {
						count += 1;
					}
				}
				token.clear();
				in_comment = true;
			}
			'"' => {
				// Check for block string opening: """
				if i + 2 < len && chars[i + 1] == '"' && chars[i + 2] == '"' {
					// Flush token before block string starts
					if depth > 0 && is_field_identifier(&token) {
						if after_on_keyword {
							after_on_keyword = false;
						} else {
							count += 1;
						}
					}
					token.clear();
					in_block_string = true;
					i += 3; // skip opening """
					continue;
				}
				// Flush token before string starts
				if depth > 0 && is_field_identifier(&token) {
					if after_on_keyword {
						after_on_keyword = false;
					} else {
						count += 1;
					}
				}
				token.clear();
				in_string = true;
			}
			'{' => {
				// Flush token — the identifier before '{' is a field with sub-selection
				if depth > 0 && is_field_identifier(&token) {
					if after_on_keyword {
						after_on_keyword = false;
					} else {
						count += 1;
					}
				}
				token.clear();
				depth += 1;
			}
			'}' => {
				// Flush token before closing brace
				if depth > 0 && is_field_identifier(&token) {
					if after_on_keyword {
						after_on_keyword = false;
					} else {
						count += 1;
					}
				}
				token.clear();
				depth = depth.saturating_sub(1);
			}
			'(' => {
				// Flush token — the identifier before '(' is a field with arguments
				if depth > 0 && is_field_identifier(&token) {
					if after_on_keyword {
						after_on_keyword = false;
					} else {
						count += 1;
					}
				}
				token.clear();
			}
			c if c.is_ascii_whitespace() || c == ',' => {
				// Token delimiter: evaluate accumulated token
				if depth > 0 && is_field_identifier(&token) {
					if after_on_keyword {
						after_on_keyword = false;
					} else {
						// Set after_on_keyword when flushing the `on` keyword itself
						if token == "on" {
							after_on_keyword = true;
						}
						count += 1;
					}
				} else if token == "on" {
					// `on` is in GRAPHQL_KEYWORDS so is_field_identifier returns false,
					// but we still need to track it for inline fragment detection
					after_on_keyword = true;
				}
				token.clear();
			}
			')' | ':' | '!' | '@' | '$' | '=' | '|' | '&' => {
				// Punctuation that terminates a token but is not a field delimiter
				token.clear();
			}
			_ => {
				token.push(ch);
			}
		}
		i += 1;
	}

	// Flush final token (query may not end with newline)
	if depth > 0 && !in_string && is_field_identifier(&token) {
		if after_on_keyword {
			// Type condition at end of query — do not count
		} else {
			count += 1;
		}
	}

	count
}

/// Validate input for creating a user.
///
/// Enforces:
/// - Name is non-empty and within length limits
/// - Name contains only valid characters
/// - Email is non-empty and within length limits
/// - Email has a basic valid format
fn validate_create_user_input(input: &CreateUserInput) -> GqlResult<()> {
	// Validate name
	let name = input.name.trim();
	if name.is_empty() {
		return Err(async_graphql::Error::new("Name cannot be empty"));
	}
	if exceeds_max_chars(name, MAX_NAME_LENGTH) {
		return Err(async_graphql::Error::new(format!(
			"Name exceeds maximum length of {} characters",
			MAX_NAME_LENGTH
		)));
	}
	if !name
		.chars()
		.all(|c| c.is_alphanumeric() || c == '_' || c == '-' || c == ' ' || c == '.')
	{
		return Err(async_graphql::Error::new(
			"Name contains invalid characters (allowed: alphanumeric, spaces, underscores, hyphens, dots)",
		));
	}

	// Validate email
	let email = input.email.trim();
	if email.is_empty() {
		return Err(async_graphql::Error::new("Email cannot be empty"));
	}
	if exceeds_max_chars(email, MAX_EMAIL_LENGTH) {
		return Err(async_graphql::Error::new(format!(
			"Email exceeds maximum length of {} characters",
			MAX_EMAIL_LENGTH
		)));
	}
	// Basic email format validation: must contain exactly one @ with parts on both sides
	let at_count = email.chars().filter(|c| *c == '@').count();
	if at_count != 1 {
		return Err(async_graphql::Error::new("Invalid email format"));
	}
	let parts: Vec<&str> = email.splitn(2, '@').collect();
	if parts.len() != 2 || parts[0].is_empty() || parts[1].is_empty() || !parts[1].contains('.') {
		return Err(async_graphql::Error::new("Invalid email format"));
	}

	Ok(())
}

/// Example: User type for GraphQL
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct User {
	/// Unique identifier for the user.
	pub id: ID,
	/// Display name of the user.
	pub name: String,
	/// Email address of the user.
	pub email: String,
	/// Whether the user account is active.
	pub active: bool,
}

#[Object]
impl User {
	async fn id(&self) -> &ID {
		&self.id
	}

	async fn name(&self) -> &str {
		&self.name
	}

	async fn email(&self) -> &str {
		&self.email
	}

	async fn active(&self) -> bool {
		self.active
	}
}

/// User storage (in-memory for example)
#[derive(Clone)]
pub struct UserStorage {
	users: Arc<RwLock<HashMap<String, User>>>,
}

impl UserStorage {
	/// Create a new user storage
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_graphql::schema::UserStorage;
	///
	/// let storage = UserStorage::new();
	/// // Creates a new storage instance with defaults
	/// ```
	pub fn new() -> Self {
		Self {
			users: Arc::new(RwLock::new(HashMap::new())),
		}
	}
	/// Add a user to storage
	///
	pub async fn add_user(&self, user: User) {
		self.users.write().await.insert(user.id.to_string(), user);
	}
	/// Get a user by ID
	///
	/// # Examples
	///
	/// ```no_run
	/// # fn main() {
	/// # tokio::runtime::Runtime::new().unwrap().block_on(async {
	/// use reinhardt_graphql::schema::UserStorage;
	/// let storage = UserStorage::new();
	/// // Retrieve user
	/// let user = storage.get_user("user-1").await;
	/// # });
	/// # }
	/// ```
	pub async fn get_user(&self, id: &str) -> Option<User> {
		self.users.read().await.get(id).cloned()
	}
	/// List all users
	///
	/// # Examples
	///
	/// ```no_run
	/// # fn main() {
	/// # tokio::runtime::Runtime::new().unwrap().block_on(async {
	/// use reinhardt_graphql::schema::UserStorage;
	/// let storage = UserStorage::new();
	/// // List all users
	/// let users = storage.list_users().await;
	/// # });
	/// # }
	/// ```
	pub async fn list_users(&self) -> Vec<User> {
		self.users.read().await.values().cloned().collect()
	}
}

impl Default for UserStorage {
	fn default() -> Self {
		Self::new()
	}
}

/// GraphQL Query root
pub struct Query;

#[Object]
impl Query {
	async fn user(&self, ctx: &Context<'_>, id: ID) -> GqlResult<Option<User>> {
		let storage = ctx.data::<UserStorage>()?;
		Ok(storage.get_user(id.as_ref()).await)
	}

	/// List users with pagination support.
	///
	/// # Arguments
	///
	/// * `first` - Maximum number of users to return (default: 20, max: 100)
	/// * `offset` - Number of users to skip (default: 0)
	async fn users(
		&self,
		ctx: &Context<'_>,
		first: Option<usize>,
		offset: Option<usize>,
	) -> GqlResult<Vec<User>> {
		let storage = ctx.data::<UserStorage>()?;
		let limit = first
			.unwrap_or(DEFAULT_PAGE_SIZE)
			.min(DEFAULT_MAX_PAGE_SIZE);
		let skip = offset.unwrap_or(0);
		let all_users = storage.list_users().await;
		Ok(all_users.into_iter().skip(skip).take(limit).collect())
	}

	async fn hello(&self, name: Option<String>) -> String {
		format!("Hello, {}!", name.unwrap_or_else(|| "World".to_string()))
	}
}

/// Input type for creating users
#[derive(async_graphql::InputObject)]
pub struct CreateUserInput {
	/// Name of the user to create.
	pub name: String,
	/// Email address of the user to create.
	pub email: String,
}

/// GraphQL Mutation root
pub struct Mutation;

#[Object]
impl Mutation {
	async fn create_user(&self, ctx: &Context<'_>, input: CreateUserInput) -> GqlResult<User> {
		// Validate input before processing
		validate_create_user_input(&input)?;

		let storage = ctx.data::<UserStorage>()?;

		let user = User {
			id: ID::from(uuid::Uuid::now_v7().to_string()),
			name: input.name.trim().to_string(),
			email: input.email.trim().to_string(),
			active: true,
		};

		storage.add_user(user.clone()).await;
		Ok(user)
	}

	async fn update_user_status(
		&self,
		ctx: &Context<'_>,
		id: ID,
		active: bool,
	) -> GqlResult<Option<User>> {
		let storage = ctx.data::<UserStorage>()?;

		if let Some(mut user) = storage.get_user(id.as_ref()).await {
			user.active = active;
			storage.add_user(user.clone()).await;
			Ok(Some(user))
		} else {
			Ok(None)
		}
	}
}

/// Create GraphQL schema
pub type AppSchema = Schema<Query, Mutation, EmptySubscription>;

/// Create a GraphQL schema with default query protection limits.
///
/// Applies default depth and complexity limits to prevent
/// resource exhaustion from malicious queries.
pub fn create_schema(storage: UserStorage) -> AppSchema {
	create_schema_with_limits(storage, QueryLimits::default())
}

/// Create a GraphQL schema with custom query protection limits.
///
/// Configures depth limit, complexity limit, and the `Analyzer` extension
/// for query cost analysis.
///
/// # Arguments
///
/// * `storage` - User data storage
/// * `limits` - Query protection limits configuration
pub fn create_schema_with_limits(storage: UserStorage, limits: QueryLimits) -> AppSchema {
	Schema::build(Query, Mutation, EmptySubscription)
		.data(storage)
		.limit_depth(limits.max_depth)
		.limit_complexity(limits.max_complexity)
		.extension(Analyzer)
		.finish()
}

#[cfg(test)]
mod tests {
	use super::*;

	#[tokio::test]
	async fn test_query_hello() {
		let storage = UserStorage::new();
		let schema = create_schema(storage);

		let query = r#"
            {
                hello(name: "GraphQL")
            }
        "#;

		let result = schema.execute(query).await;
		let data = result.data.into_json().unwrap();
		assert_eq!(data["hello"], "Hello, GraphQL!");
	}

	#[tokio::test]
	async fn test_mutation_create_user() {
		let storage = UserStorage::new();
		let schema = create_schema(storage);

		let query = r#"
            mutation {
                createUser(input: { name: "Alice", email: "alice@example.com" }) {
                    name
                    email
                    active
                }
            }
        "#;

		let result = schema.execute(query).await;
		let data = result.data.into_json().unwrap();
		assert_eq!(data["createUser"]["name"], "Alice");
		assert!(data["createUser"]["active"].as_bool().unwrap());
	}

	#[tokio::test]
	async fn test_query_user() {
		let storage = UserStorage::new();
		let user = User {
			id: ID::from("test-id-123"),
			name: "Bob".to_string(),
			email: "bob@example.com".to_string(),
			active: true,
		};
		storage.add_user(user).await;

		let schema = create_schema(storage);

		let query = r#"
            {
                user(id: "test-id-123") {
                    id
                    name
                    email
                    active
                }
            }
        "#;

		let result = schema.execute(query).await;
		let data = result.data.into_json().unwrap();
		assert_eq!(data["user"]["id"], "test-id-123");
		assert_eq!(data["user"]["name"], "Bob");
		assert_eq!(data["user"]["email"], "bob@example.com");
		assert!(data["user"]["active"].as_bool().unwrap());
	}

	#[tokio::test]
	async fn test_query_user_not_found() {
		let storage = UserStorage::new();
		let schema = create_schema(storage);

		let query = r#"
            {
                user(id: "nonexistent-id") {
                    id
                    name
                }
            }
        "#;

		let result = schema.execute(query).await;
		let data = result.data.into_json().unwrap();
		assert!(data["user"].is_null());
	}

	#[tokio::test]
	async fn test_query_users_empty() {
		let storage = UserStorage::new();
		let schema = create_schema(storage);

		let query = r#"
            {
                users {
                    id
                    name
                }
            }
        "#;

		let result = schema.execute(query).await;
		let data = result.data.into_json().unwrap();
		assert!(data["users"].is_array());
		assert_eq!(data["users"].as_array().unwrap().len(), 0);
	}

	#[tokio::test]
	async fn test_query_users_multiple() {
		let storage = UserStorage::new();

		let user1 = User {
			id: ID::from("1"),
			name: "Alice".to_string(),
			email: "alice@example.com".to_string(),
			active: true,
		};
		let user2 = User {
			id: ID::from("2"),
			name: "Bob".to_string(),
			email: "bob@example.com".to_string(),
			active: false,
		};
		let user3 = User {
			id: ID::from("3"),
			name: "Charlie".to_string(),
			email: "charlie@example.com".to_string(),
			active: true,
		};

		storage.add_user(user1).await;
		storage.add_user(user2).await;
		storage.add_user(user3).await;

		let schema = create_schema(storage);

		let query = r#"
            {
                users {
                    id
                    name
                    email
                    active
                }
            }
        "#;

		let result = schema.execute(query).await;
		let data = result.data.into_json().unwrap();
		let users = data["users"].as_array().unwrap();
		assert_eq!(users.len(), 3);

		// Verify that all users are present
		let names: Vec<&str> = users.iter().map(|u| u["name"].as_str().unwrap()).collect();
		assert!(names.contains(&"Alice"));
		assert!(names.contains(&"Bob"));
		assert!(names.contains(&"Charlie"));
	}

	#[tokio::test]
	async fn test_query_users_pagination_with_first() {
		// Arrange
		let storage = UserStorage::new();
		for i in 0..10 {
			storage
				.add_user(User {
					id: ID::from(format!("user-{}", i)),
					name: format!("User{}", i),
					email: format!("user{}@example.com", i),
					active: true,
				})
				.await;
		}
		let schema = create_schema(storage);

		// Act: request only 3 users
		let query = r#"{ users(first: 3) { id } }"#;
		let result = schema.execute(query).await;

		// Assert
		assert!(result.errors.is_empty());
		let data = result.data.into_json().unwrap();
		let users = data["users"].as_array().unwrap();
		assert_eq!(users.len(), 3);
	}

	#[tokio::test]
	async fn test_query_users_pagination_with_offset() {
		// Arrange
		let storage = UserStorage::new();
		for i in 0..5 {
			storage
				.add_user(User {
					id: ID::from(format!("user-{}", i)),
					name: format!("User{}", i),
					email: format!("user{}@example.com", i),
					active: true,
				})
				.await;
		}
		let schema = create_schema(storage);

		// Act: skip 3, take 10 -> should get 2
		let query = r#"{ users(first: 10, offset: 3) { id } }"#;
		let result = schema.execute(query).await;

		// Assert
		assert!(result.errors.is_empty());
		let data = result.data.into_json().unwrap();
		let users = data["users"].as_array().unwrap();
		assert_eq!(users.len(), 2);
	}

	#[tokio::test]
	async fn test_query_users_enforces_max_page_size() {
		// Arrange
		let storage = UserStorage::new();
		for i in 0..150 {
			storage
				.add_user(User {
					id: ID::from(format!("user-{}", i)),
					name: format!("User{}", i),
					email: format!("user{}@example.com", i),
					active: true,
				})
				.await;
		}
		let schema = create_schema(storage);

		// Act: request 500 users but max is 100
		let query = r#"{ users(first: 500) { id } }"#;
		let result = schema.execute(query).await;

		// Assert: clamped to max page size
		assert!(result.errors.is_empty());
		let data = result.data.into_json().unwrap();
		let users = data["users"].as_array().unwrap();
		assert_eq!(users.len(), DEFAULT_MAX_PAGE_SIZE);
	}

	#[tokio::test]
	async fn test_create_user_validates_empty_name() {
		// Arrange
		let storage = UserStorage::new();
		let schema = create_schema(storage);

		// Act
		let query = r#"
			mutation {
				createUser(input: { name: "   ", email: "test@example.com" }) {
					id
				}
			}
		"#;
		let result = schema.execute(query).await;

		// Assert
		assert!(
			!result.errors.is_empty(),
			"expected validation error for empty name"
		);
	}

	#[tokio::test]
	async fn test_create_user_validates_invalid_email() {
		// Arrange
		let storage = UserStorage::new();
		let schema = create_schema(storage);

		// Act
		let query = r#"
			mutation {
				createUser(input: { name: "Alice", email: "not-an-email" }) {
					id
				}
			}
		"#;
		let result = schema.execute(query).await;

		// Assert
		assert!(
			!result.errors.is_empty(),
			"expected validation error for invalid email"
		);
	}

	#[tokio::test]
	async fn test_validate_query_rejects_oversized_query() {
		// Arrange
		let limits = QueryLimits::full(10, 100, 100, 200); // 100 byte limit

		// Act
		let long_query = "{ ".to_string() + &"a ".repeat(100) + "}";
		let result = validate_query(&long_query, &limits);

		// Assert
		assert!(result.is_err());
		assert!(result.unwrap_err().contains("exceeds maximum"));
	}

	#[tokio::test]
	async fn test_validate_query_accepts_normal_query() {
		// Arrange
		let limits = QueryLimits::default();

		// Act
		let result = validate_query("{ users { id name } }", &limits);

		// Assert
		assert!(result.is_ok());
	}

	#[tokio::test]
	async fn test_mutation_update_user_status() {
		let storage = UserStorage::new();
		let user = User {
			id: ID::from("update-test-id"),
			name: "David".to_string(),
			email: "david@example.com".to_string(),
			active: true,
		};
		storage.add_user(user).await;

		let schema = create_schema(storage);

		let query = r#"
            mutation {
                updateUserStatus(id: "update-test-id", active: false) {
                    id
                    name
                    active
                }
            }
        "#;

		let result = schema.execute(query).await;
		let data = result.data.into_json().unwrap();
		assert_eq!(data["updateUserStatus"]["id"], "update-test-id");
		assert!(!data["updateUserStatus"]["active"].as_bool().unwrap());
	}

	#[tokio::test]
	async fn test_mutation_update_nonexistent_user() {
		let storage = UserStorage::new();
		let schema = create_schema(storage);

		let query = r#"
            mutation {
                updateUserStatus(id: "does-not-exist", active: false) {
                    id
                    name
                }
            }
        "#;

		let result = schema.execute(query).await;
		let data = result.data.into_json().unwrap();
		assert!(data["updateUserStatus"].is_null());
	}

	#[tokio::test]
	async fn test_user_object_fields() {
		let user = User {
			id: ID::from("field-test-id"),
			name: "Eve".to_string(),
			email: "eve@example.com".to_string(),
			active: false,
		};

		// Test direct field access
		assert_eq!(user.id.to_string(), "field-test-id");
		assert_eq!(user.name, "Eve");
		assert_eq!(user.email, "eve@example.com");
		assert!(!user.active);
	}

	#[tokio::test]
	async fn test_user_storage_add_get() {
		let storage = UserStorage::new();

		let user = User {
			id: ID::from("storage-test-1"),
			name: "Frank".to_string(),
			email: "frank@example.com".to_string(),
			active: true,
		};

		storage.add_user(user.clone()).await;

		let retrieved = storage.get_user("storage-test-1").await;
		let retrieved = retrieved.unwrap();
		assert_eq!(retrieved.id.to_string(), "storage-test-1");
		assert_eq!(retrieved.name, "Frank");
		assert_eq!(retrieved.email, "frank@example.com");
		assert!(retrieved.active);
	}

	#[tokio::test]
	async fn test_user_storage_list() {
		let storage = UserStorage::new();

		// Initially empty
		let users = storage.list_users().await;
		assert_eq!(users.len(), 0);

		// Add users
		storage
			.add_user(User {
				id: ID::from("list-1"),
				name: "User1".to_string(),
				email: "user1@example.com".to_string(),
				active: true,
			})
			.await;

		storage
			.add_user(User {
				id: ID::from("list-2"),
				name: "User2".to_string(),
				email: "user2@example.com".to_string(),
				active: false,
			})
			.await;

		let users = storage.list_users().await;
		assert_eq!(users.len(), 2);
	}

	#[tokio::test]
	async fn test_create_schema_with_data() {
		let storage = UserStorage::new();
		storage
			.add_user(User {
				id: ID::from("pre-existing"),
				name: "PreExisting".to_string(),
				email: "preexisting@example.com".to_string(),
				active: true,
			})
			.await;

		let schema = create_schema(storage);

		// Verify schema can query pre-existing data
		let query = r#"
            {
                user(id: "pre-existing") {
                    name
                }
            }
        "#;

		let result = schema.execute(query).await;
		let data = result.data.into_json().unwrap();
		assert_eq!(data["user"]["name"], "PreExisting");
	}

	#[tokio::test]
	async fn test_graphql_error_types() {
		let err1 = GraphQLError::Schema("test schema error".to_string());
		assert!(err1.to_string().contains("Schema error"));

		let err2 = GraphQLError::Resolver("test resolver error".to_string());
		assert!(err2.to_string().contains("Resolver error"));

		let err3 = GraphQLError::NotFound("test item".to_string());
		assert!(err3.to_string().contains("Not found"));
	}

	#[tokio::test]
	async fn test_query_depth_limit_rejects_deep_query() {
		// Arrange: depth limit of 1 only allows top-level fields
		let storage = UserStorage::new();
		let limits = QueryLimits::new(1, 1000);
		let schema = create_schema_with_limits(storage, limits);

		// Act: query with nested selection exceeds depth limit of 1
		let query = r#"
			{
				users {
					name
				}
			}
		"#;
		let result = schema.execute(query).await;

		// Assert: should produce a depth-limit error
		assert!(
			!result.errors.is_empty(),
			"expected depth limit error but query succeeded"
		);
		let error_message = &result.errors[0].message;
		assert!(
			error_message.to_lowercase().contains("too deep"),
			"expected depth-limit message, got: {error_message}"
		);
	}

	#[tokio::test]
	async fn test_query_depth_limit_allows_shallow_query() {
		// Arrange
		let storage = UserStorage::new();
		let limits = QueryLimits::new(10, 1000);
		let schema = create_schema_with_limits(storage, limits);

		// Act
		let query = r#"{ hello(name: "Test") }"#;
		let result = schema.execute(query).await;

		// Assert
		assert!(
			result.errors.is_empty(),
			"expected no errors for shallow query"
		);
		let data = result.data.into_json().unwrap();
		assert_eq!(data["hello"], "Hello, Test!");
	}

	#[tokio::test]
	async fn test_query_complexity_limit_rejects_complex_query() {
		// Arrange: very low complexity limit
		let storage = UserStorage::new();
		let limits = QueryLimits::new(100, 1);
		let schema = create_schema_with_limits(storage, limits);

		// Act: query with multiple fields exceeds complexity of 1
		let query = r#"
			{
				users {
					id
					name
					email
					active
				}
			}
		"#;
		let result = schema.execute(query).await;

		// Assert: should produce a complexity-limit error
		assert!(
			!result.errors.is_empty(),
			"expected complexity limit error but query succeeded"
		);
		let error_message = &result.errors[0].message;
		assert!(
			error_message.to_lowercase().contains("complex"),
			"expected complexity-limit message, got: {error_message}"
		);
	}

	#[tokio::test]
	async fn test_query_limits_default_values() {
		// Arrange / Act
		let limits = QueryLimits::default();

		// Assert
		assert_eq!(limits.max_depth, DEFAULT_MAX_QUERY_DEPTH);
		assert_eq!(limits.max_complexity, DEFAULT_MAX_QUERY_COMPLEXITY);
	}

	#[tokio::test]
	async fn test_create_schema_with_custom_limits() {
		// Arrange
		let storage = UserStorage::new();
		let limits = QueryLimits::new(20, 500);
		let schema = create_schema_with_limits(storage, limits);

		// Act: simple query within limits
		let query = r#"{ hello }"#;
		let result = schema.execute(query).await;

		// Assert
		assert!(result.errors.is_empty());
		let data = result.data.into_json().unwrap();
		assert_eq!(data["hello"], "Hello, World!");
	}

	#[tokio::test]
	async fn test_analyzer_extension_present() {
		// Arrange
		let storage = UserStorage::new();
		let schema = create_schema(storage);

		// Act: execute query and check for complexity/depth in extensions
		let query = r#"{ hello(name: "Analyzer") }"#;
		let result = schema.execute(query).await;

		// Assert: Analyzer extension adds complexity/depth to response extensions
		assert!(result.errors.is_empty());
		assert!(
			!result.extensions.is_empty(),
			"expected Analyzer extension data in response"
		);
	}

	#[rstest::rstest]
	#[case(
		"{\n  user(name: \"hello \\\"world\\\"\") {\n    id\n  }\n}",
		2,
		"escaped quotes inside string should not affect field count"
	)]
	#[case(
		"{\n  user(name: \"hello \\\\\\\"end\") {\n    id\n    name\n  }\n}",
		3,
		"escaped backslash before quote should correctly toggle string state"
	)]
	#[case(
		"{\n  user(name: \"no escapes\") {\n    id\n  }\n}",
		2,
		"string without escapes should count fields normally"
	)]
	#[case(
		"{\n  user(name: \"a\\\"b\\\"c\") {\n    id\n    name\n    email\n  }\n}",
		4,
		"multiple escaped quotes in a single string literal"
	)]
	fn test_count_query_fields_with_escaped_strings(
		#[case] query: &str,
		#[case] expected: usize,
		#[case] description: &str,
	) {
		// Arrange — query and expected count provided by rstest parametrization

		// Act
		let count = count_query_fields(query);

		// Assert
		assert_eq!(count, expected, "{}", description);
	}

	#[rstest::rstest]
	#[case(
		"{ users { id name email } }",
		4,
		"parent field plus multiple fields on same line within sub-selection"
	)]
	#[case(
		"{ users { id } }",
		2,
		"parent field plus single field on same line within sub-selection"
	)]
	#[case(
		"{ users { id name } posts { title body } }",
		6,
		"two parent fields plus their sub-selection fields on same line"
	)]
	fn test_count_query_fields_same_line(
		#[case] query: &str,
		#[case] expected: usize,
		#[case] description: &str,
	) {
		// Arrange — query and expected count provided by rstest parametrization

		// Act
		let count = count_query_fields(query);

		// Assert
		assert_eq!(count, expected, "{}", description);
	}

	#[rstest::rstest]
	#[case(
		"{ ... on User { id name } }",
		2,
		"inline fragment type condition should not be counted as a field"
	)]
	#[case(
		"{ users { ... on Admin { role } ... on Member { level } } }",
		3,
		"multiple inline fragments: users + role + level, type names excluded"
	)]
	fn test_count_query_fields_inline_fragments(
		#[case] query: &str,
		#[case] expected: usize,
		#[case] description: &str,
	) {
		// Arrange — query and expected count provided by rstest parametrization

		// Act
		let count = count_query_fields(query);

		// Assert
		assert_eq!(count, expected, "{}", description);
	}

	#[rstest::rstest]
	#[case(
		"{ user(bio: \"\"\"multi\nline\"\"\") { id } }",
		2,
		"block string argument content should not be counted as fields"
	)]
	#[case(
		"{ user(desc: \"\"\"has identifier inside\"\"\") { id name } }",
		3,
		"block string with identifier-like content should not affect field count"
	)]
	fn test_count_query_fields_block_strings(
		#[case] query: &str,
		#[case] expected: usize,
		#[case] description: &str,
	) {
		// Arrange — query and expected count provided by rstest parametrization

		// Act
		let count = count_query_fields(query);

		// Assert
		assert_eq!(count, expected, "{}", description);
	}

	#[tokio::test]
	async fn test_exceeds_max_chars_short_circuits() {
		// Arrange / Act / Assert
		assert!(!exceeds_max_chars("hello", 5)); // exactly at limit
		assert!(exceeds_max_chars("hello!", 5)); // one over
		assert!(!exceeds_max_chars("", 0)); // empty at zero limit
		assert!(exceeds_max_chars("a", 0)); // single char over zero limit
	}

	#[tokio::test]
	async fn test_create_user_accepts_multibyte_name_within_limit() {
		// Arrange: CJK characters are multi-byte in UTF-8 but each is 1 char
		let storage = UserStorage::new();
		let schema = create_schema(storage);

		// 4 CJK characters = 4 chars (well under MAX_NAME_LENGTH of 100)
		let query = r#"
			mutation {
				createUser(input: { name: "田中太郎", email: "tanaka@example.com" }) {
					name
				}
			}
		"#;

		// Act
		let result = schema.execute(query).await;

		// Assert: should succeed because character count is within limit
		assert!(
			result.errors.is_empty(),
			"expected success for multi-byte name within limit, got: {:?}",
			result.errors
		);
		let data = result.data.into_json().unwrap();
		assert_eq!(data["createUser"]["name"], "田中太郎");
	}

	#[tokio::test]
	async fn test_create_user_rejects_multibyte_name_over_limit() {
		// Arrange: build a name with exactly MAX_NAME_LENGTH + 1 CJK characters
		let storage = UserStorage::new();
		let schema = create_schema(storage);

		let long_name: String = "".repeat(MAX_NAME_LENGTH + 1);
		let query = format!(
			r#"mutation {{ createUser(input: {{ name: "{}", email: "test@example.com" }}) {{ id }} }}"#,
			long_name
		);

		// Act
		let result = schema.execute(&query).await;

		// Assert: should reject because character count exceeds limit
		assert!(
			!result.errors.is_empty(),
			"expected validation error for name exceeding {} characters",
			MAX_NAME_LENGTH
		);
	}

	#[tokio::test]
	async fn test_create_user_accepts_emoji_name_at_limit() {
		// Arrange: emoji are multi-byte in UTF-8 but each is 1 char count
		let storage = UserStorage::new();
		let schema = create_schema(storage);

		// Exactly MAX_NAME_LENGTH emoji characters
		// Note: name validation only allows alphanumeric, spaces, underscores,
		// hyphens, and dots, so emoji will be rejected by the character check,
		// not the length check. We test length via CJK instead.
		// Here we verify that a name at exactly the limit passes length validation.
		let name_at_limit: String = "a".repeat(MAX_NAME_LENGTH);
		let query = format!(
			r#"mutation {{ createUser(input: {{ name: "{}", email: "test@example.com" }}) {{ id }} }}"#,
			name_at_limit
		);

		// Act
		let result = schema.execute(&query).await;

		// Assert: should succeed (exactly at limit)
		assert!(
			result.errors.is_empty(),
			"expected success for name at exactly the limit, got: {:?}",
			result.errors
		);
	}
}