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
//! Pre-flight metric validation engine for AST tree complexity and selector depth.
//!
//! Provides [`MetricValidator`] to enforce configurable safety boundaries on query weight
//! and nesting levels prior to outbound network dispatch, protecting client applications
//! from remote HTTP `422 Unprocessable Entity` and `429 Too Many Requests` rejections.
//!
//! ### Metric Enforcement Pipeline
//!
//! ```text
//! MetricValidator::validate(&query)
//! │
//! ▼
//! ┌───────────────────────────────────────────────┐
//! │ 1. GqlValidatable::validate_args() │
//! │ (Preemptively checks min, max, regex, etc.)│
//! └───────────────────────┬───────────────────────┘
//! │
//! ▼
//! ┌───────────────────────────────────────────────┐
//! │ 2. GqlInspectable::complexity() │
//! │ (Root cost + Field costs <= max_complexity)│
//! └───────────────────────┬───────────────────────┘
//! │
//! ▼
//! ┌───────────────────────────────────────────────┐
//! │ 3. GqlInspectable::depth() │
//! │ (AST nesting depth <= max_depth) │
//! └───────────────────────────────────────────────┘
//! ```
//!
//! ### Quick Example
//!
//! ```rust
//! use moongraphql_builder::metrics::MetricValidator;
//! use moongraphql_builder::error::MoongqlError;
//! use moongraphql_builder::traits::{GqlInspectable, GqlValidatable};
//!
//! // Mock structure implementing inspectable and validatable contracts
//! struct MockQuery;
//! impl GqlValidatable for MockQuery {
//! fn validate_args(&self) -> Result<(), MoongqlError> { Ok(()) }
//! }
//! impl GqlInspectable for MockQuery {
//! fn complexity(&self) -> u32 { 15 }
//! fn depth(&self) -> u32 { 2 }
//! }
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Configure budget: max complexity 20, max depth 3
//! let validator = MetricValidator::new(20, 3);
//! validator.validate(&MockQuery)?;
//! println!("Query passed preemptive metric validation!");
//! # Ok(())
//! # }
//! ```
use crate;
use crate;
/// Preemptive metric and argument validator for any GraphQL query builder or composite document.
///
/// Evaluates theoretical AST complexity weights and hierarchy depth ceilings against
/// configured thresholds before network dispatch.