hive-router 0.3.0

GraphQL router for Federation, part of the Hive platform
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
use std::collections::HashMap;
use std::fmt;

use crate::query_planner::state::supergraph_state::TypeNode;
use sonic_rs::{JsonNumberTrait, Value, ValueRef};

use crate::executor::introspection::schema::{InputObjectInfo, SchemaMetadata};

/// A request error raised while coercing an operation's variable values
/// (spec: "Coercing Variable Values"). Messages follow graphql-js.
#[derive(Debug, Clone, PartialEq, thiserror::Error)]
pub enum VariableCoercionError {
    #[error("Variable \"${name}\" has invalid value{path}: {reason}")]
    InvalidValue {
        name: String,
        path: ValuePath,
        reason: Box<InvalidValueReason>,
    },

    #[error("Variable \"${name}\" has invalid default value{path}: {reason}")]
    InvalidDefaultValue {
        name: String,
        path: ValuePath,
        reason: Box<InvalidValueReason>,
    },
}

/// Why a value cannot be coerced to its expected type.
#[derive(Debug, Clone, PartialEq, thiserror::Error)]
pub enum InvalidValueReason {
    #[error("Expected a value of non-null type \"{type_name}\" to be provided.")]
    MissingNonNullValue { type_name: String },

    #[error("Expected value of non-null type \"{type_name}\" not to be null.")]
    UnexpectedNull { type_name: String },

    #[error("Value \"{value}\" does not exist in \"{type_name}\" enum.")]
    InvalidEnumValue { value: String, type_name: String },

    #[error("Enum \"{type_name}\" cannot represent non-string value: {value}.")]
    ExpectedEnumString { type_name: String, value: String },

    #[error("String cannot represent a non string value: {value}")]
    ExpectedString { value: String },

    #[error("ID cannot represent value: {value}")]
    ExpectedId { value: String },

    #[error("Int cannot represent non-integer value: {value}")]
    ExpectedInteger { value: String },

    #[error("Int cannot represent non 32-bit signed integer value: {value}")]
    IntegerOutOfRange { value: String },

    #[error("Float cannot represent non numeric value: {value}")]
    ExpectedFloat { value: String },

    #[error("Boolean cannot represent a non boolean value: {value}")]
    ExpectedBoolean { value: String },

    #[error("Expected value of type \"{type_name}\" to be an object, found: {value}.")]
    ExpectedObject { type_name: String, value: String },

    #[error(
        "Expected value of type \"{type_name}\" to include required field \"{field_name}\", found: {value}."
    )]
    MissingRequiredField {
        type_name: String,
        field_name: String,
        value: String,
    },

    #[error(
        "Within OneOf Input Object type \"{type_name}\", exactly one field must be specified, and the value for that field must be non-null."
    )]
    InvalidOneOf { type_name: String },

    #[error(
        "Expected value of type \"{type_name}\" not to include unknown field \"{field_name}\", found: {value}."
    )]
    UnknownField {
        type_name: String,
        field_name: String,
        value: String,
    },
}

/// A step into a variable's value: a list index or an input object field.
#[derive(Debug, Clone, PartialEq)]
pub enum PathSegment {
    Index(usize),
    Field(String),
}

/// Where the invalid value sits inside a variable's value, from the outside in.
/// Displayed like graphql-js: empty for the variable itself, otherwise e.g. ` at .a[0].b`.
#[derive(Debug, Clone, PartialEq, Default)]
pub struct ValuePath(Vec<PathSegment>);

impl fmt::Display for ValuePath {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.0.is_empty() {
            return Ok(());
        }
        f.write_str(" at ")?;
        for segment in &self.0 {
            match segment {
                PathSegment::Index(index) => write!(f, "[{index}]")?,
                PathSegment::Field(name) => write!(f, ".{name}")?,
            }
        }
        Ok(())
    }
}

/// An invalid value found by `validate_runtime_value`. The path is collected while the error
/// travels up, so it holds the innermost segment first.
#[derive(Debug, PartialEq)]
struct InvalidValue {
    reversed_path: Vec<PathSegment>,
    reason: InvalidValueReason,
}

impl InvalidValue {
    fn new(reason: InvalidValueReason) -> Self {
        InvalidValue {
            reversed_path: Vec::new(),
            reason,
        }
    }

    fn path(mut self) -> (ValuePath, Box<InvalidValueReason>) {
        self.reversed_path.reverse();
        (ValuePath(self.reversed_path), Box::new(self.reason))
    }
}

#[inline]
pub fn collect_variables(
    operation: &crate::query_planner::ast::operation::OperationDefinition,
    variables_map: &mut HashMap<String, Value>,
    schema_metadata: &SchemaMetadata,
) -> Result<Option<HashMap<String, Value>>, VariableCoercionError> {
    let Some(variable_definitions) = operation.variable_definitions.as_ref() else {
        return Ok(None);
    };

    let mut variable_values: HashMap<String, Value> =
        HashMap::with_capacity(variable_definitions.len());

    for variable_definition in variable_definitions {
        let variable_name = variable_definition.name.as_str();
        let variable_type = &variable_definition.variable_type;

        if let Some(variable_value) = variables_map.remove(variable_name) {
            validate_runtime_value(variable_value.as_ref(), variable_type, schema_metadata)
                .map_err(|invalid| {
                    let (path, reason) = invalid.path();
                    VariableCoercionError::InvalidValue {
                        name: variable_name.to_string(),
                        path,
                        reason,
                    }
                })?;
            variable_values.insert(variable_name.to_string(), variable_value);
            continue;
        }

        if let Some(default_value) = &variable_definition.default_value {
            let default_value_coerced: Value = default_value.into();
            validate_runtime_value(
                default_value_coerced.as_ref(),
                variable_type,
                schema_metadata,
            )
            .map_err(|invalid| {
                let (path, reason) = invalid.path();
                VariableCoercionError::InvalidDefaultValue {
                    name: variable_name.to_string(),
                    path,
                    reason,
                }
            })?;
            variable_values.insert(variable_name.to_string(), default_value_coerced);
            continue;
        }

        if variable_type.is_non_null() {
            return Err(VariableCoercionError::InvalidValue {
                name: variable_name.to_string(),
                path: ValuePath::default(),
                reason: Box::new(InvalidValueReason::MissingNonNullValue {
                    type_name: variable_type.to_string(),
                }),
            });
        }
    }

    if variable_values.is_empty() {
        Ok(None)
    } else {
        Ok(Some(variable_values))
    }
}

#[inline]
fn validate_runtime_value(
    value: ValueRef,
    type_node: &TypeNode,
    schema_metadata: &SchemaMetadata,
) -> Result<(), InvalidValue> {
    if let ValueRef::Null = value {
        return if type_node.is_non_null() {
            Err(InvalidValue::new(InvalidValueReason::UnexpectedNull {
                type_name: type_node.to_string(),
            }))
        } else {
            Ok(())
        };
    }
    match type_node {
        TypeNode::Named(name) => {
            if let Some(enum_values) = schema_metadata.enum_values.get(name) {
                if let ValueRef::String(s) = value {
                    if !enum_values.contains(s) {
                        return Err(InvalidValue::new(InvalidValueReason::InvalidEnumValue {
                            value: s.to_string(),
                            type_name: name.clone(),
                        }));
                    }
                } else {
                    return Err(InvalidValue::new(InvalidValueReason::ExpectedEnumString {
                        type_name: name.clone(),
                        value: inspect(value),
                    }));
                }
            } else if let Some(input_object) = schema_metadata.input_objects.get(name) {
                validate_input_object(value, name, input_object, schema_metadata)?;
            } else {
                validate_scalar(name, value).map_err(InvalidValue::new)?;
            }
        }
        TypeNode::NonNull(inner_type) => {
            // The null check is now handled above, so we can just recurse.
            validate_runtime_value(value, inner_type, schema_metadata)?;
        }
        TypeNode::List(inner_type) => {
            if let ValueRef::Array(arr) = value {
                for (index, item) in arr.iter().enumerate() {
                    validate_runtime_value(item.as_ref(), inner_type, schema_metadata).map_err(
                        |mut invalid| {
                            invalid.reversed_path.push(PathSegment::Index(index));
                            invalid
                        },
                    )?;
                }
            } else {
                validate_runtime_value(value, inner_type, schema_metadata)?;
            }
        }
    }
    Ok(())
}

/// Checks an input object value (spec: "Input Objects", input coercion), in graphql-js's order:
/// the defined fields in schema order, then the keys the type doesn't define. For a OneOf type,
/// the number of keys is checked first, as the spec's "OneOf Input Objects" coercion does.
fn validate_input_object(
    value: ValueRef,
    type_name: &str,
    input_object: &InputObjectInfo,
    schema_metadata: &SchemaMetadata,
) -> Result<(), InvalidValue> {
    let ValueRef::Object(object) = value else {
        return Err(InvalidValue::new(InvalidValueReason::ExpectedObject {
            type_name: type_name.to_string(),
            value: inspect(value),
        }));
    };
    let fields = &input_object.fields;

    if input_object.is_one_of {
        let mut entries = object.iter();
        let invalid_one_of = || InvalidValueReason::InvalidOneOf {
            type_name: type_name.to_string(),
        };
        match (entries.next(), entries.next()) {
            (Some((key, entry_value)), None) => {
                if matches!(entry_value.as_ref(), ValueRef::Null) {
                    let mut invalid = InvalidValue::new(invalid_one_of());
                    invalid
                        .reversed_path
                        .push(PathSegment::Field(key.to_string()));
                    return Err(invalid);
                }
            }
            _ => return Err(InvalidValue::new(invalid_one_of())),
        }
    }

    for field in fields {
        match object.get(&field.name) {
            Some(field_value) => {
                validate_runtime_value(field_value.as_ref(), &field.field_type, schema_metadata)
                    .map_err(|mut invalid| {
                        invalid
                            .reversed_path
                            .push(PathSegment::Field(field.name.clone()));
                        invalid
                    })?
            }
            // A field is required only when it is non-null and has no default.
            None if field.field_type.is_non_null() && !field.has_default => {
                return Err(InvalidValue::new(
                    InvalidValueReason::MissingRequiredField {
                        type_name: type_name.to_string(),
                        field_name: field.name.clone(),
                        value: inspect(value),
                    },
                ));
            }
            None => {}
        }
    }

    if let Some((unknown_field, _)) = object
        .iter()
        .find(|(key, _)| !fields.iter().any(|field| field.name == *key))
    {
        return Err(InvalidValue::new(InvalidValueReason::UnknownField {
            type_name: type_name.to_string(),
            field_name: unknown_field.to_string(),
            value: inspect(value),
        }));
    }

    Ok(())
}

/// Checks a value against a built-in scalar. Like Apollo Router, Int and ID only accept JSON
/// numbers written as integers: `1.0` or `1e3` are floats, even with no fractional part.
#[inline]
fn validate_scalar(name: &str, value: ValueRef) -> Result<(), InvalidValueReason> {
    let is_integer = |num: &sonic_rs::Number| num.is_i64() || num.is_u64();
    match (name, &value) {
        ("String", ValueRef::String(_))
        | ("Boolean", ValueRef::Bool(_))
        // The JSON parser only produces finite numbers.
        | ("Float", ValueRef::Number(_)) => Ok(()),
        ("ID", ValueRef::String(_)) => Ok(()),
        ("ID", ValueRef::Number(num)) if is_integer(num) => Ok(()),
        ("Int", ValueRef::Number(num)) if is_integer(num) => {
            if num.as_i64().is_some_and(|n| i32::try_from(n).is_ok()) {
                Ok(())
            } else {
                Err(InvalidValueReason::IntegerOutOfRange {
                    value: inspect(value),
                })
            }
        }
        ("String", _) => Err(InvalidValueReason::ExpectedString {
            value: inspect(value),
        }),
        ("ID", _) => Err(InvalidValueReason::ExpectedId {
            value: inspect(value),
        }),
        ("Int", _) => Err(InvalidValueReason::ExpectedInteger {
            value: inspect(value),
        }),
        ("Float", _) => Err(InvalidValueReason::ExpectedFloat {
            value: inspect(value),
        }),
        ("Boolean", _) => Err(InvalidValueReason::ExpectedBoolean {
            value: inspect(value),
        }),
        // Custom scalars and input objects
        _ => Ok(()),
    }
}

/// How graphql-js prints a value in error messages (its `inspect` utility).
fn inspect(value: ValueRef) -> String {
    let mut output = String::new();
    write_inspected(&mut output, value, 0);
    output
}

/// graphql-js prints at most this many list items, and replaces lists and objects nested
/// deeper than `MAX_INSPECT_DEPTH` with `[Array]` / `[Object]`.
const MAX_INSPECT_LIST_ITEMS: usize = 10;
const MAX_INSPECT_DEPTH: usize = 2;

fn write_inspected(output: &mut String, value: ValueRef, depth: usize) {
    match value {
        ValueRef::Null => output.push_str("null"),
        ValueRef::Bool(b) => output.push_str(if b { "true" } else { "false" }),
        ValueRef::Number(num) => match (num.as_i64(), num.as_u64(), num.as_f64()) {
            (Some(n), _, _) => output.push_str(&n.to_string()),
            (_, Some(n), _) => output.push_str(&n.to_string()),
            // Unlike graphql-js, a float with no fractional part keeps its `.0`: Int and ID
            // reject it, and `Int cannot represent non-integer value: 1` would be confusing.
            (_, _, Some(n)) if n.fract() == 0.0 => output.push_str(&format!("{n}.0")),
            (_, _, Some(n)) => output.push_str(&n.to_string()),
            _ => output.push_str(&num.to_string()),
        },
        ValueRef::String(s) => {
            output.push_str(&sonic_rs::to_string(s).unwrap_or_else(|_| format!("{s:?}")))
        }
        ValueRef::Array(arr) => {
            if arr.is_empty() {
                output.push_str("[]");
            } else if depth >= MAX_INSPECT_DEPTH {
                output.push_str("[Array]");
            } else {
                output.push('[');
                for (index, item) in arr.iter().take(MAX_INSPECT_LIST_ITEMS).enumerate() {
                    if index > 0 {
                        output.push_str(", ");
                    }
                    write_inspected(output, item.as_ref(), depth + 1);
                }
                match arr.len().saturating_sub(MAX_INSPECT_LIST_ITEMS) {
                    0 => {}
                    1 => output.push_str(", ... 1 more item"),
                    remaining => output.push_str(&format!(", ... {remaining} more items")),
                }
                output.push(']');
            }
        }
        ValueRef::Object(obj) => {
            if obj.is_empty() {
                output.push_str("{}");
            } else if depth >= MAX_INSPECT_DEPTH {
                output.push_str("[Object]");
            } else {
                output.push_str("{ ");
                for (index, (key, item)) in obj.iter().enumerate() {
                    if index > 0 {
                        output.push_str(", ");
                    }
                    output.push_str(key);
                    output.push_str(": ");
                    write_inspected(output, item.as_ref(), depth + 1);
                }
                output.push_str(" }");
            }
        }
    }
}

#[cfg(test)]
mod tests;