apollo-router 1.61.13

A configurable, high-performance routing runtime for 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
//! Wrapper types for [`http::Request`] and [`http::Response`] from the http crate.
//!
//! To improve their usability.

use std::cmp::PartialEq;
use std::hash::Hash;
use std::ops::Deref;
use std::ops::DerefMut;

use axum::body::boxed;
use axum::response::IntoResponse;
use bytes::Bytes;
use http::HeaderValue;
use http::header;
use http::header::HeaderName;
use multimap::MultiMap;

use crate::graphql;
use crate::services::APPLICATION_JSON_HEADER_VALUE;

/// Delayed-fallibility wrapper for conversion to [`http::header::HeaderName`].
///
/// `buildstructor` builders allow doing implict conversions for convenience,
/// but only infallible ones.
/// `HeaderName` can be converted from various types but the conversions is often fallible,
/// with `TryFrom` or `TryInto` instead of `From` or `Into`.
/// This types splits conversion in two steps:
/// it implements infallible conversion from various types like `&str` (that builders can rely on)
/// and fallible conversion to `HeaderName` (called later where we can handle errors).
///
/// See for example [`supergraph::Request::builder`][crate::services::supergraph::Request::builder]
/// which can be used like this:
///
/// ```
/// # fn main() -> Result<(), tower::BoxError> {
/// use apollo_router::services::supergraph;
/// let request = supergraph::Request::fake_builder()
///     .header("accept-encoding", "gzip")
///     // Other parameters
///     .build()?;
/// # Ok(()) }
/// ```
pub struct TryIntoHeaderName {
    /// The fallible conversion result
    result: Result<header::HeaderName, header::InvalidHeaderName>,
}

/// Delayed-fallibility wrapper for conversion to [`http::header::HeaderValue`].
///
/// `buildstructor` builders allow doing implict conversions for convenience,
/// but only infallible ones.
/// `HeaderValue` can be converted from various types but the conversions is often fallible,
/// with `TryFrom` or `TryInto` instead of `From` or `Into`.
/// This types splits conversion in two steps:
/// it implements infallible conversion from various types like `&str` (that builders can rely on)
/// and fallible conversion to `HeaderValue` (called later where we can handle errors).
///
/// See for example [`supergraph::Request::builder`][crate::services::supergraph::Request::builder]
/// which can be used like this:
///
/// ```
/// # fn main() -> Result<(), tower::BoxError> {
/// use apollo_router::services::supergraph;
/// let request = supergraph::Request::fake_builder()
///     .header("accept-encoding", "gzip")
///     // Other parameters
///     .build()?;
/// # Ok(()) }
/// ```
pub struct TryIntoHeaderValue {
    /// The fallible conversion result
    result: Result<header::HeaderValue, header::InvalidHeaderValue>,
}

impl TryFrom<TryIntoHeaderName> for header::HeaderName {
    type Error = header::InvalidHeaderName;

    fn try_from(value: TryIntoHeaderName) -> Result<Self, Self::Error> {
        value.result
    }
}

impl TryFrom<TryIntoHeaderValue> for header::HeaderValue {
    type Error = header::InvalidHeaderValue;

    fn try_from(value: TryIntoHeaderValue) -> Result<Self, Self::Error> {
        value.result
    }
}

impl From<header::HeaderName> for TryIntoHeaderName {
    fn from(value: header::HeaderName) -> Self {
        Self { result: Ok(value) }
    }
}

impl From<&'_ header::HeaderName> for TryIntoHeaderName {
    fn from(value: &'_ header::HeaderName) -> Self {
        Self {
            result: Ok(value.clone()),
        }
    }
}

impl From<&'_ [u8]> for TryIntoHeaderName {
    fn from(value: &'_ [u8]) -> Self {
        Self {
            result: value.try_into(),
        }
    }
}

impl From<&'_ str> for TryIntoHeaderName {
    fn from(value: &'_ str) -> Self {
        Self {
            result: value.try_into(),
        }
    }
}

impl From<Vec<u8>> for TryIntoHeaderName {
    fn from(value: Vec<u8>) -> Self {
        Self {
            result: value.try_into(),
        }
    }
}

impl From<String> for TryIntoHeaderName {
    fn from(value: String) -> Self {
        Self {
            result: value.try_into(),
        }
    }
}

impl From<header::HeaderValue> for TryIntoHeaderValue {
    fn from(value: header::HeaderValue) -> Self {
        Self { result: Ok(value) }
    }
}

impl From<&'_ header::HeaderValue> for TryIntoHeaderValue {
    fn from(value: &'_ header::HeaderValue) -> Self {
        Self {
            result: Ok(value.clone()),
        }
    }
}

impl From<&'_ [u8]> for TryIntoHeaderValue {
    fn from(value: &'_ [u8]) -> Self {
        Self {
            result: value.try_into(),
        }
    }
}

impl From<&'_ str> for TryIntoHeaderValue {
    fn from(value: &'_ str) -> Self {
        Self {
            result: value.try_into(),
        }
    }
}

impl From<Vec<u8>> for TryIntoHeaderValue {
    fn from(value: Vec<u8>) -> Self {
        Self {
            result: value.try_into(),
        }
    }
}

impl From<String> for TryIntoHeaderValue {
    fn from(value: String) -> Self {
        Self {
            result: value.try_into(),
        }
    }
}

impl Eq for TryIntoHeaderName {}

impl PartialEq for TryIntoHeaderName {
    fn eq(&self, other: &Self) -> bool {
        match (&self.result, &other.result) {
            (Ok(a), Ok(b)) => a == b,
            (Err(_), Err(_)) => true,
            _ => false,
        }
    }
}

impl Hash for TryIntoHeaderName {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        if let Ok(value) = &self.result {
            value.hash(state)
        }
    }
}

pub(crate) fn header_map(
    from: MultiMap<TryIntoHeaderName, TryIntoHeaderValue>,
) -> Result<http::HeaderMap<http::HeaderValue>, http::Error> {
    let mut http = http::HeaderMap::new();
    for (key, values) in from {
        let name = key.result?;
        let mut values = values.into_iter();
        if let Some(last) = values.next_back() {
            for value in values {
                http.append(name.clone(), value.result?);
            }
            http.append(name, last.result?);
        }
    }
    Ok(http)
}

/// Ignores `http::Extensions`
pub(crate) fn clone_http_request<B: Clone>(request: &http::Request<B>) -> http::Request<B> {
    let mut new = http::Request::builder()
        .method(request.method().clone())
        .uri(request.uri().clone())
        .version(request.version())
        .body(request.body().clone())
        .unwrap();
    *new.headers_mut() = request.headers().clone();
    new
}

/// Ignores `http::Extensions`
pub(crate) fn clone_http_response<B: Clone>(response: &http::Response<B>) -> http::Response<B> {
    let mut new = http::Response::builder()
        .status(response.status())
        .version(response.version())
        .body(response.body().clone())
        .unwrap();
    *new.headers_mut() = response.headers().clone();
    new
}

/// Wrap an http Request.
#[derive(Debug)]
pub(crate) struct Request<T> {
    pub(crate) inner: http::Request<T>,
}

// Most of the required functionality is provided by our Deref and DerefMut implementations.
#[buildstructor::buildstructor]
impl<T> Request<T> {
    /// This is the constructor (or builder) to use when constructing a real Request.
    ///
    /// Required parameters are required in non-testing code to create a Request.
    #[builder]
    pub(crate) fn new(
        headers: MultiMap<TryIntoHeaderName, TryIntoHeaderValue>,
        uri: http::Uri,
        method: http::Method,
        body: T,
    ) -> http::Result<Request<T>> {
        let mut builder = http::request::Builder::new().method(method).uri(uri);
        for (key, values) in headers {
            let header_name: HeaderName = key.try_into()?;
            for value in values {
                let header_value: HeaderValue = value.try_into()?;
                builder = builder.header(header_name.clone(), header_value);
            }
        }
        let req = builder.body(body)?;
        Ok(Self { inner: req })
    }
}

#[cfg(test)]
#[buildstructor::buildstructor]
impl<T> Request<T> {
    /// This is the constructor (or builder) to use when constructing a "fake" Request.
    ///
    /// This does not enforce the provision of the uri and method that is required for a fully functional
    /// Request. It's usually enough for testing, when a fully constructed Request is
    /// difficult to construct and not required for the purposes of the test.
    ///
    /// In addition, fake requests are expected to be valid, and will panic if given invalid values.
    #[builder]
    pub(crate) fn fake_new(
        headers: MultiMap<TryIntoHeaderName, TryIntoHeaderValue>,
        uri: Option<http::Uri>,
        method: Option<http::Method>,
        body: T,
    ) -> http::Result<Request<T>> {
        Self::new(
            headers,
            uri.unwrap_or_default(),
            method.unwrap_or(http::Method::GET),
            body,
        )
    }
}

impl<T> Deref for Request<T> {
    type Target = http::Request<T>;

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

impl<T> DerefMut for Request<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.inner
    }
}

impl<T> From<http::Request<T>> for Request<T> {
    fn from(inner: http::Request<T>) -> Self {
        Request { inner }
    }
}

impl<T: Clone> From<&'_ http::Request<T>> for Request<T> {
    fn from(req: &'_ http::Request<T>) -> Self {
        Request {
            inner: clone_http_request(req),
        }
    }
}

impl<T> From<Request<T>> for http::Request<T> {
    fn from(request: Request<T>) -> http::Request<T> {
        request.inner
    }
}

impl<T: Clone> Clone for Request<T> {
    fn clone(&self) -> Self {
        Self {
            inner: clone_http_request(&self.inner),
        }
    }
}

impl<T: Hash> Hash for Request<T> {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.inner.method().hash(state);
        self.inner.version().hash(state);
        self.inner.uri().hash(state);
        // this assumes headers are in the same order
        for (name, value) in self.inner.headers() {
            name.hash(state);
            value.hash(state);
        }
        self.inner.body().hash(state);
    }
}

impl<T: PartialEq> PartialEq for Request<T> {
    fn eq(&self, other: &Self) -> bool {
        let mut res = self.inner.method().eq(other.inner.method())
            && self.inner.version().eq(&other.inner.version())
            && self.inner.uri().eq(other.inner.uri());

        if !res {
            return false;
        }
        if self.inner.headers().len() != other.inner.headers().len() {
            return false;
        }

        // this assumes headers are in the same order
        for ((name, value), (other_name, other_value)) in self
            .inner
            .headers()
            .iter()
            .zip(other.inner.headers().iter())
        {
            res = name.eq(other_name) && value.eq(other_value);
            if !res {
                return false;
            }
        }

        self.inner.body().eq(other.inner.body())
    }
}

impl<T: Eq> Eq for Request<T> {}

/// Wrap an http Response.
#[derive(Debug, Default)]
pub(crate) struct Response<T> {
    pub(crate) inner: http::Response<T>,
}

impl<T> Deref for Response<T> {
    type Target = http::Response<T>;

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

impl<T> DerefMut for Response<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.inner
    }
}

impl<T> From<http::Response<T>> for Response<T> {
    fn from(inner: http::Response<T>) -> Self {
        Response { inner }
    }
}

impl<T: Clone> From<&'_ http::Response<T>> for Response<T> {
    fn from(req: &'_ http::Response<T>) -> Self {
        Response {
            inner: clone_http_response(req),
        }
    }
}

impl<T> From<Response<T>> for http::Response<T> {
    fn from(response: Response<T>) -> http::Response<T> {
        response.inner
    }
}

impl<T: Clone> Clone for Response<T> {
    fn clone(&self) -> Self {
        Self {
            inner: clone_http_response(&self.inner),
        }
    }
}

impl IntoResponse for Response<graphql::Response> {
    fn into_response(self) -> axum::response::Response {
        // todo: chunks?
        let (mut parts, body) = http::Response::from(self).into_parts();
        let json_body_bytes =
            Bytes::from(serde_json::to_vec(&body).expect("body should be serializable; qed"));
        parts
            .headers
            .insert(header::CONTENT_TYPE, APPLICATION_JSON_HEADER_VALUE.clone());

        axum::response::Response::from_parts(parts, boxed(http_body::Full::new(json_body_bytes)))
    }
}

impl IntoResponse for Response<Bytes> {
    fn into_response(self) -> axum::response::Response {
        // todo: chunks?
        let (parts, body) = http::Response::from(self).into_parts();

        axum::response::Response::from_parts(parts, boxed(http_body::Full::new(body)))
    }
}

#[cfg(test)]
mod test {
    use http::HeaderValue;
    use http::Method;
    use http::Uri;

    use crate::http_ext::Request;

    #[test]
    fn builder() {
        let request = Request::builder()
            .header("a", "b")
            .header("a", "c")
            .uri(Uri::from_static("http://example.com"))
            .method(Method::POST)
            .body("test")
            .build()
            .unwrap();
        assert_eq!(
            request
                .headers()
                .get_all("a")
                .into_iter()
                .collect::<Vec<_>>(),
            vec![HeaderValue::from_static("b"), HeaderValue::from_static("c")]
        );
        assert_eq!(request.uri(), &Uri::from_static("http://example.com"));
        assert_eq!(request.method(), Method::POST);
        assert_eq!(request.body(), &"test");
    }
}