apollo-federation 2.15.0

Apollo Federation
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
//! Headers defined in connectors `@source` and `@connect` directives.

use std::ops::Deref;
#[cfg(test)]
use std::str::FromStr;

use apollo_compiler::collections::IndexMap;
use serde_json_bytes::Value;

use super::ApplyToError;
use crate::connectors::ConnectSpec;
use crate::connectors::string_template;
use crate::connectors::string_template::Part;
use crate::connectors::string_template::StringTemplate;

#[derive(Clone, Debug)]
pub struct HeaderValue(StringTemplate);

impl HeaderValue {
    pub fn parse_with_spec(s: &str, spec: ConnectSpec) -> Result<Self, string_template::Error> {
        let template = StringTemplate::parse_with_spec(s, spec)?;
        // Validate that any constant parts are valid header values.
        for part in &template.parts {
            let Part::Constant(constant) = part else {
                continue;
            };
            http::HeaderValue::from_str(&constant.value).map_err(|_| string_template::Error {
                message: format!("invalid value `{}`", constant.value),
                location: constant.location.clone(),
            })?;
        }
        Ok(Self(template))
    }

    /// Evaluate expressions in the header value.
    ///
    /// # Errors
    ///
    /// Returns an error any expression can't be evaluated, or evaluates to an unsupported type.
    pub fn interpolate(
        &self,
        vars: &IndexMap<String, Value>,
    ) -> Result<(http::HeaderValue, Vec<ApplyToError>), String> {
        let (interpolated, apply_to_errors) =
            self.0.interpolate(vars).map_err(|e| e.to_string())?;
        let result = http::HeaderValue::from_str(&interpolated).map_err(|e| e.to_string())?;
        Ok((result, apply_to_errors))
    }
}

impl Deref for HeaderValue {
    type Target = StringTemplate;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

#[cfg(test)]
impl FromStr for HeaderValue {
    type Err = string_template::Error;

    /// Parses a [`HeaderValue`] from a &str, using [`ConnectSpec::latest()`] as
    /// the parsing version. This trait implementation is only available in
    /// tests, and should be avoided outside tests because it runs the risk of
    /// ignoring the developer's chosen [`ConnectSpec`].
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::parse_with_spec(s, ConnectSpec::latest())
    }
}

#[cfg(test)]
mod test_header_value_parse {
    use insta::assert_debug_snapshot;

    use super::*;

    #[test]
    fn simple_constant() {
        assert_debug_snapshot!(
            HeaderValue::from_str("text"),
            @r###"
        Ok(
            HeaderValue(
                StringTemplate {
                    parts: [
                        Constant(
                            Constant {
                                value: "text",
                                location: 0..4,
                            },
                        ),
                    ],
                },
            ),
        )
        "###
        );
    }
    #[test]
    fn simple_expression() {
        assert_debug_snapshot!(
            HeaderValue::from_str("{$config.one}"),
            @r#"
        Ok(
            HeaderValue(
                StringTemplate {
                    parts: [
                        Expression(
                            Expression {
                                expression: JSONSelection {
                                    inner: Value(
                                        WithRange {
                                            node: Path(
                                                PathSelection {
                                                    path: WithRange {
                                                        node: Var(
                                                            WithRange {
                                                                node: $config,
                                                                range: Some(
                                                                    0..7,
                                                                ),
                                                            },
                                                            WithRange {
                                                                node: Key(
                                                                    WithRange {
                                                                        node: Field(
                                                                            "one",
                                                                        ),
                                                                        range: Some(
                                                                            8..11,
                                                                        ),
                                                                    },
                                                                    WithRange {
                                                                        node: Empty,
                                                                        range: Some(
                                                                            11..11,
                                                                        ),
                                                                    },
                                                                ),
                                                                range: Some(
                                                                    7..11,
                                                                ),
                                                            },
                                                        ),
                                                        range: Some(
                                                            0..11,
                                                        ),
                                                    },
                                                },
                                            ),
                                            range: Some(
                                                0..11,
                                            ),
                                        },
                                    ),
                                    spec: V0_3,
                                },
                                location: 1..12,
                            },
                        ),
                    ],
                },
            ),
        )
        "#
        );
    }
    #[test]
    fn mixed_constant_and_expression() {
        assert_debug_snapshot!(
            HeaderValue::from_str("text{$config.one}text"),
            @r#"
        Ok(
            HeaderValue(
                StringTemplate {
                    parts: [
                        Constant(
                            Constant {
                                value: "text",
                                location: 0..4,
                            },
                        ),
                        Expression(
                            Expression {
                                expression: JSONSelection {
                                    inner: Value(
                                        WithRange {
                                            node: Path(
                                                PathSelection {
                                                    path: WithRange {
                                                        node: Var(
                                                            WithRange {
                                                                node: $config,
                                                                range: Some(
                                                                    0..7,
                                                                ),
                                                            },
                                                            WithRange {
                                                                node: Key(
                                                                    WithRange {
                                                                        node: Field(
                                                                            "one",
                                                                        ),
                                                                        range: Some(
                                                                            8..11,
                                                                        ),
                                                                    },
                                                                    WithRange {
                                                                        node: Empty,
                                                                        range: Some(
                                                                            11..11,
                                                                        ),
                                                                    },
                                                                ),
                                                                range: Some(
                                                                    7..11,
                                                                ),
                                                            },
                                                        ),
                                                        range: Some(
                                                            0..11,
                                                        ),
                                                    },
                                                },
                                            ),
                                            range: Some(
                                                0..11,
                                            ),
                                        },
                                    ),
                                    spec: V0_3,
                                },
                                location: 5..16,
                            },
                        ),
                        Constant(
                            Constant {
                                value: "text",
                                location: 17..21,
                            },
                        ),
                    ],
                },
            ),
        )
        "#
        );
    }

    #[test]
    fn invalid_header_values() {
        assert_debug_snapshot!(
            HeaderValue::from_str("\x7f"),
            @r###"
        Err(
            Error {
                message: "invalid value `\u{7f}`",
                location: 0..1,
            },
        )
        "###
        )
    }

    #[test]
    fn expressions_with_nested_braces() {
        assert_debug_snapshot!(
            HeaderValue::from_str("const{$config.one { two { three } }}another-const"),
            @r#"
        Ok(
            HeaderValue(
                StringTemplate {
                    parts: [
                        Constant(
                            Constant {
                                value: "const",
                                location: 0..5,
                            },
                        ),
                        Expression(
                            Expression {
                                expression: JSONSelection {
                                    inner: Value(
                                        WithRange {
                                            node: Path(
                                                PathSelection {
                                                    path: WithRange {
                                                        node: Var(
                                                            WithRange {
                                                                node: $config,
                                                                range: Some(
                                                                    0..7,
                                                                ),
                                                            },
                                                            WithRange {
                                                                node: Key(
                                                                    WithRange {
                                                                        node: Field(
                                                                            "one",
                                                                        ),
                                                                        range: Some(
                                                                            8..11,
                                                                        ),
                                                                    },
                                                                    WithRange {
                                                                        node: Selection(
                                                                            SubSelection {
                                                                                selections: [
                                                                                    NamedSelection {
                                                                                        prefix: None,
                                                                                        path: WithRange {
                                                                                            node: Path(
                                                                                                PathSelection {
                                                                                                    path: WithRange {
                                                                                                        node: Key(
                                                                                                            WithRange {
                                                                                                                node: Field(
                                                                                                                    "two",
                                                                                                                ),
                                                                                                                range: Some(
                                                                                                                    14..17,
                                                                                                                ),
                                                                                                            },
                                                                                                            WithRange {
                                                                                                                node: Selection(
                                                                                                                    SubSelection {
                                                                                                                        selections: [
                                                                                                                            NamedSelection {
                                                                                                                                prefix: None,
                                                                                                                                path: WithRange {
                                                                                                                                    node: Path(
                                                                                                                                        PathSelection {
                                                                                                                                            path: WithRange {
                                                                                                                                                node: Key(
                                                                                                                                                    WithRange {
                                                                                                                                                        node: Field(
                                                                                                                                                            "three",
                                                                                                                                                        ),
                                                                                                                                                        range: Some(
                                                                                                                                                            20..25,
                                                                                                                                                        ),
                                                                                                                                                    },
                                                                                                                                                    WithRange {
                                                                                                                                                        node: Empty,
                                                                                                                                                        range: Some(
                                                                                                                                                            25..25,
                                                                                                                                                        ),
                                                                                                                                                    },
                                                                                                                                                ),
                                                                                                                                                range: Some(
                                                                                                                                                    20..25,
                                                                                                                                                ),
                                                                                                                                            },
                                                                                                                                        },
                                                                                                                                    ),
                                                                                                                                    range: Some(
                                                                                                                                        20..25,
                                                                                                                                    ),
                                                                                                                                },
                                                                                                                            },
                                                                                                                        ],
                                                                                                                        range: Some(
                                                                                                                            18..27,
                                                                                                                        ),
                                                                                                                    },
                                                                                                                ),
                                                                                                                range: Some(
                                                                                                                    18..27,
                                                                                                                ),
                                                                                                            },
                                                                                                        ),
                                                                                                        range: Some(
                                                                                                            14..27,
                                                                                                        ),
                                                                                                    },
                                                                                                },
                                                                                            ),
                                                                                            range: Some(
                                                                                                14..27,
                                                                                            ),
                                                                                        },
                                                                                    },
                                                                                ],
                                                                                range: Some(
                                                                                    12..29,
                                                                                ),
                                                                            },
                                                                        ),
                                                                        range: Some(
                                                                            12..29,
                                                                        ),
                                                                    },
                                                                ),
                                                                range: Some(
                                                                    7..29,
                                                                ),
                                                            },
                                                        ),
                                                        range: Some(
                                                            0..29,
                                                        ),
                                                    },
                                                },
                                            ),
                                            range: Some(
                                                0..29,
                                            ),
                                        },
                                    ),
                                    spec: V0_3,
                                },
                                location: 6..35,
                            },
                        ),
                        Constant(
                            Constant {
                                value: "another-const",
                                location: 36..49,
                            },
                        ),
                    ],
                },
            ),
        )
        "#
        );
    }

    #[test]
    fn missing_closing_braces() {
        assert_debug_snapshot!(
            HeaderValue::from_str("{$config.one"),
            @r###"
        Err(
            Error {
                message: "Invalid expression, missing closing }",
                location: 0..12,
            },
        )
        "###
        )
    }
}

#[cfg(test)]
mod test_interpolate {
    use insta::assert_debug_snapshot;
    use pretty_assertions::assert_eq;
    use serde_json_bytes::json;

    use super::*;
    #[test]
    fn test_interpolate() {
        let value = HeaderValue::from_str("before {$config.one} after").unwrap();
        let mut vars = IndexMap::default();
        vars.insert("$config".to_string(), json!({"one": "foo"}));
        assert_eq!(
            value.interpolate(&vars).unwrap().0,
            http::HeaderValue::from_static("before foo after")
        );
    }

    #[test]
    fn test_interpolate_missing_value() {
        let value = HeaderValue::from_str("{$config.one}").unwrap();
        let vars = IndexMap::default();
        assert_eq!(
            value.interpolate(&vars).unwrap().0,
            http::HeaderValue::from_static("")
        );
    }

    #[test]
    fn test_interpolate_value_array() {
        let header_value = HeaderValue::from_str("{$config.one}").unwrap();
        let mut vars = IndexMap::default();
        vars.insert("$config".to_string(), json!({"one": ["one", "two"]}));
        assert_eq!(
            header_value.interpolate(&vars),
            Err("Expression is not allowed to evaluate to arrays or objects.".to_string())
        );
    }

    #[test]
    fn test_interpolate_value_bool() {
        let header_value = HeaderValue::from_str("{$config.one}").unwrap();
        let mut vars = IndexMap::default();
        vars.insert("$config".to_string(), json!({"one": true}));
        assert_eq!(
            http::HeaderValue::from_static("true"),
            header_value.interpolate(&vars).unwrap().0
        );
    }

    #[test]
    fn test_interpolate_value_null() {
        let header_value = HeaderValue::from_str("{$config.one}").unwrap();
        let mut vars = IndexMap::default();
        vars.insert("$config".to_string(), json!({"one": null}));
        assert_eq!(
            http::HeaderValue::from_static(""),
            header_value.interpolate(&vars).unwrap().0
        );
    }

    #[test]
    fn test_interpolate_value_number() {
        let header_value = HeaderValue::from_str("{$config.one}").unwrap();
        let mut vars = IndexMap::default();
        vars.insert("$config".to_string(), json!({"one": 1}));
        assert_eq!(
            http::HeaderValue::from_static("1"),
            header_value.interpolate(&vars).unwrap().0
        );
    }

    #[test]
    fn test_interpolate_value_object() {
        let header_value = HeaderValue::from_str("{$config.one}").unwrap();
        let mut vars = IndexMap::default();
        vars.insert("$config".to_string(), json!({"one": {}}));
        assert_debug_snapshot!(
            header_value.interpolate(&vars),
            @r###"
        Err(
            "Expression is not allowed to evaluate to arrays or objects.",
        )
        "###
        );
    }

    #[test]
    fn test_interpolate_value_string() {
        let header_value = HeaderValue::from_str("{$config.one}").unwrap();
        let mut vars = IndexMap::default();
        vars.insert("$config".to_string(), json!({"one": "string"}));
        assert_eq!(
            http::HeaderValue::from_static("string"),
            header_value.interpolate(&vars).unwrap().0
        );
    }
}

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

    #[test]
    fn test_variable_references() {
        let value =
            HeaderValue::from_str("a {$this.a.b.c} b {$args.a.b.c} c {$config.a.b.c}").unwrap();
        let references: Vec<_> = value
            .expressions()
            .map(|e| e.expression.to_string())
            .collect();
        assert_eq!(
            references,
            vec!["$this.a.b.c", "$args.a.b.c", "$config.a.b.c"]
        );
    }
}