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
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
use std::sync::Arc;
use std::sync::Mutex;

use futures::stream::StreamExt;
use http::HeaderMap;
use http::HeaderValue;
use http::Method;
use http::Request;
use http::Uri;
use http::header::CONTENT_TYPE;
use http::header::VARY;
use mime::APPLICATION_JSON;
use serde_json_bytes::json;
use tower::ServiceExt;
use tower_service::Service;

use crate::Context;
use crate::graphql;
use crate::metrics::FutureMetricsExt;
use crate::services::MULTIPART_DEFER_CONTENT_TYPE;
use crate::services::SupergraphRequest;
use crate::services::SupergraphResponse;
use crate::services::router;
use crate::services::router::body::get_body_bytes;
use crate::services::router::service::from_supergraph_mock_callback;
use crate::services::router::service::process_vary_header;
use crate::services::subgraph;
use crate::services::supergraph;
use crate::test_harness::make_fake_batch;

// Test Vary processing

#[test]
fn it_adds_default_with_value_origin_if_no_vary_header() {
    let mut default_headers = HeaderMap::new();
    process_vary_header(&mut default_headers);
    let vary_opt = default_headers.get(VARY);
    assert!(vary_opt.is_some());
    let vary = vary_opt.expect("has a value");
    assert_eq!(vary, "origin");
}

#[test]
fn it_leaves_vary_alone_if_set() {
    let mut default_headers = HeaderMap::new();
    default_headers.insert(VARY, HeaderValue::from_static("*"));
    process_vary_header(&mut default_headers);
    let vary_opt = default_headers.get(VARY);
    assert!(vary_opt.is_some());
    let vary = vary_opt.expect("has a value");
    assert_eq!(vary, "*");
}

#[test]
fn it_leaves_varys_alone_if_there_are_more_than_one() {
    let mut default_headers = HeaderMap::new();
    default_headers.insert(VARY, HeaderValue::from_static("one"));
    default_headers.append(VARY, HeaderValue::from_static("two"));
    process_vary_header(&mut default_headers);
    let vary = default_headers.get_all(VARY);
    assert_eq!(vary.iter().count(), 2);
    for value in vary {
        assert!(value == "one" || value == "two");
    }
}

#[tokio::test]
async fn it_extracts_query_and_operation_name() {
    let query = "query";
    let expected_query = query;
    let operation_name = "operationName";
    let expected_operation_name = operation_name;

    let expected_response = graphql::Response::builder()
        .data(json!({"response": "yay"}))
        .build();

    let mut router_service = from_supergraph_mock_callback(move |req| {
        let example_response = expected_response.clone();

        assert_eq!(
            req.supergraph_request.body().query.as_deref().unwrap(),
            expected_query
        );
        assert_eq!(
            req.supergraph_request
                .body()
                .operation_name
                .as_deref()
                .unwrap(),
            expected_operation_name
        );

        Ok(SupergraphResponse::new_from_graphql_response(
            example_response,
            req.context,
        ))
    })
    .await;

    // get request
    let get_request = supergraph::Request::builder()
        .query(query)
        .operation_name(operation_name)
        .header(CONTENT_TYPE, APPLICATION_JSON.essence_str())
        .uri(Uri::from_static("/"))
        .method(Method::GET)
        .context(Context::new())
        .build()
        .unwrap()
        .try_into()
        .unwrap();

    router_service.call(get_request).await.unwrap();

    // post request
    let post_request = supergraph::Request::builder()
        .query(query)
        .operation_name(operation_name)
        .header(CONTENT_TYPE, APPLICATION_JSON.essence_str())
        .uri(Uri::from_static("/"))
        .method(Method::POST)
        .context(Context::new())
        .build()
        .unwrap();

    router_service
        .call(post_request.try_into().unwrap())
        .await
        .unwrap();
}

#[tokio::test]
async fn it_fails_on_empty_query() {
    let expected_error = "Must provide query string.";

    let router_service = from_supergraph_mock_callback(move |_req| unreachable!()).await;

    let request = SupergraphRequest::fake_builder()
        .query("".to_string())
        .build()
        .expect("expecting valid request")
        .try_into()
        .unwrap();

    let response = router_service
        .oneshot(request)
        .await
        .unwrap()
        .into_graphql_response_stream()
        .await
        .next()
        .await
        .unwrap()
        .unwrap();
    let actual_error = response.errors[0].message.clone();

    assert_eq!(expected_error, actual_error);
    assert!(response.errors[0].extensions.contains_key("code"));
}

#[tokio::test]
async fn it_fails_on_no_query() {
    let expected_error = "Must provide query string.";

    let router_service = from_supergraph_mock_callback(move |_req| unreachable!()).await;

    let request = SupergraphRequest::fake_builder()
        .build()
        .expect("expecting valid request")
        .try_into()
        .unwrap();

    let response = router_service
        .oneshot(request)
        .await
        .unwrap()
        .into_graphql_response_stream()
        .await
        .next()
        .await
        .unwrap()
        .unwrap();
    let actual_error = response.errors[0].message.clone();
    assert_eq!(expected_error, actual_error);
    assert!(response.errors[0].extensions.contains_key("code"));
}

#[tokio::test]
async fn test_http_max_request_bytes() {
    /// Size of the JSON serialization of the request created by `fn canned_new`
    /// in `apollo-router/src/services/supergraph.rs`
    const CANNED_REQUEST_LEN: usize = 391;

    async fn with_config(http_max_request_bytes: usize) -> router::Response {
        let http_request = supergraph::Request::canned_builder()
            .build()
            .unwrap()
            .supergraph_request
            .map(|body| {
                let json_bytes = serde_json::to_vec(&body).unwrap();
                assert_eq!(
                    json_bytes.len(),
                    CANNED_REQUEST_LEN,
                    "The request generated by `fn canned_new` \
                     in `apollo-router/src/services/supergraph.rs` has changed. \
                     Please update `CANNED_REQUEST_LEN` accordingly."
                );
                hyper::Body::from(json_bytes)
            });
        let config = serde_json::json!({
            "limits": {
                "http_max_request_bytes": http_max_request_bytes
            }
        });
        crate::TestHarness::builder()
            .configuration_json(config)
            .unwrap()
            .build_router()
            .await
            .unwrap()
            .oneshot(router::Request::from(http_request))
            .await
            .unwrap()
    }
    // Send a request just at (under) the limit
    let response = with_config(CANNED_REQUEST_LEN).await.response;
    assert_eq!(response.status(), http::StatusCode::OK);

    // Send a request just over the limit
    let response = with_config(CANNED_REQUEST_LEN - 1).await.response;
    assert_eq!(response.status(), http::StatusCode::PAYLOAD_TOO_LARGE);
}

#[tokio::test]
async fn it_only_accepts_batch_http_link_mode_for_query_batch() {
    let expected_response: serde_json::Value = serde_json::from_str(include_str!(
        "../query_batching/testdata/batching_not_enabled_response.json"
    ))
    .unwrap();

    async fn with_config() -> router::Response {
        let http_request = make_fake_batch(
            supergraph::Request::canned_builder()
                .build()
                .unwrap()
                .supergraph_request,
            None,
        );
        let config = serde_json::json!({});
        crate::TestHarness::builder()
            .configuration_json(config)
            .unwrap()
            .build_router()
            .await
            .unwrap()
            .oneshot(router::Request::from(http_request))
            .await
            .unwrap()
    }
    // Send a request
    let response = with_config().await.response;
    assert_eq!(response.status(), http::StatusCode::BAD_REQUEST);
    let data: serde_json::Value =
        serde_json::from_slice(&get_body_bytes(response.into_body()).await.unwrap()).unwrap();
    assert_eq!(expected_response, data);
}

#[tokio::test]
async fn it_processes_a_valid_query_batch() {
    let expected_response: serde_json::Value = serde_json::from_str(include_str!(
        "../query_batching/testdata/expected_good_response.json"
    ))
    .unwrap();

    async fn with_config() -> router::Response {
        let http_request = batch_with_three_unique_queries();
        let config = serde_json::json!({
            "batching": {
                "enabled": true,
                "mode" : "batch_http_link"
            }
        });
        crate::TestHarness::builder()
            .configuration_json(config)
            .unwrap()
            .build_router()
            .await
            .unwrap()
            .oneshot(router::Request::from(http_request))
            .await
            .unwrap()
    }

    async move {
        // Send a request
        let response = with_config().await.response;
        assert_eq!(response.status(), http::StatusCode::OK);
        let data: serde_json::Value =
            serde_json::from_slice(&get_body_bytes(response.into_body()).await.unwrap()).unwrap();
        assert_eq!(expected_response, data);
        assert_histogram_sum!(
            "apollo.router.operations.batching.size",
            3,
            "mode" = "batch_http_link"
        );
    }
    .with_metrics()
    .await;
}

#[tokio::test]
async fn it_will_not_process_a_query_batch_without_enablement() {
    let expected_response: serde_json::Value = serde_json::from_str(include_str!(
        "../query_batching/testdata/batching_not_enabled_response.json"
    ))
    .unwrap();

    async fn with_config() -> router::Response {
        let http_request = make_fake_batch(
            supergraph::Request::canned_builder()
                .build()
                .unwrap()
                .supergraph_request,
            None,
        );
        let config = serde_json::json!({});
        crate::TestHarness::builder()
            .configuration_json(config)
            .unwrap()
            .build_router()
            .await
            .unwrap()
            .oneshot(router::Request::from(http_request))
            .await
            .unwrap()
    }
    // Send a request
    let response = with_config().await.response;
    assert_eq!(response.status(), http::StatusCode::BAD_REQUEST);
    let data: serde_json::Value =
        serde_json::from_slice(&get_body_bytes(response.into_body()).await.unwrap()).unwrap();
    assert_eq!(expected_response, data);
}

#[tokio::test]
async fn it_will_not_process_a_poorly_formatted_query_batch() {
    let expected_response: serde_json::Value = serde_json::from_str(include_str!(
        "../query_batching/testdata/badly_formatted_batch_response.json"
    ))
    .unwrap();

    async fn with_config() -> router::Response {
        let http_request = supergraph::Request::canned_builder()
            .build()
            .unwrap()
            .supergraph_request
            .map(|req: graphql::Request| {
                // Modify the request so that it is an invalid array of requests.
                let mut json_bytes = serde_json::to_vec(&req).unwrap();
                let mut result = vec![b'['];
                result.append(&mut json_bytes.clone());
                result.push(b',');
                result.append(&mut json_bytes);
                // Deliberately omit the required trailing ]
                hyper::Body::from(result)
            });
        let config = serde_json::json!({
            "batching": {
                "enabled": true,
                "mode" : "batch_http_link"
            }
        });
        crate::TestHarness::builder()
            .configuration_json(config)
            .unwrap()
            .build_router()
            .await
            .unwrap()
            .oneshot(router::Request::from(http_request))
            .await
            .unwrap()
    }
    // Send a request
    let response = with_config().await.response;
    assert_eq!(response.status(), http::StatusCode::BAD_REQUEST);
    let data: serde_json::Value =
        serde_json::from_slice(&get_body_bytes(response.into_body()).await.unwrap()).unwrap();
    assert_eq!(expected_response, data);
}

#[tokio::test]
async fn it_will_process_a_non_batched_defered_query() {
    let expected_response = "\r\n--graphql\r\ncontent-type: application/json\r\n\r\n{\"data\":{\"topProducts\":[{\"upc\":\"1\",\"name\":\"Table\",\"reviews\":[{\"product\":{\"name\":\"Table\"},\"author\":{\"id\":\"1\",\"name\":\"Ada Lovelace\"}},{\"product\":{\"name\":\"Table\"},\"author\":{\"id\":\"2\",\"name\":\"Alan Turing\"}}]},{\"upc\":\"2\",\"name\":\"Couch\",\"reviews\":[{\"product\":{\"name\":\"Couch\"},\"author\":{\"id\":\"1\",\"name\":\"Ada Lovelace\"}}]}]},\"hasNext\":true}\r\n--graphql\r\ncontent-type: application/json\r\n\r\n{\"hasNext\":false,\"incremental\":[{\"data\":{\"id\":\"1\"},\"path\":[\"topProducts\",0,\"reviews\",0]},{\"data\":{\"id\":\"4\"},\"path\":[\"topProducts\",0,\"reviews\",1]},{\"data\":{\"id\":\"2\"},\"path\":[\"topProducts\",1,\"reviews\",0]}]}\r\n--graphql--\r\n";
    async fn with_config() -> router::Response {
        let query = "
            query TopProducts($first: Int) {
                topProducts(first: $first) {
                    upc
                    name
                    reviews {
                        ... @defer {
                        id
                        }
                        product { name }
                        author { id name }
                    }
                }
            }
        ";
        let http_request = supergraph::Request::canned_builder()
            .header(http::header::ACCEPT, MULTIPART_DEFER_CONTENT_TYPE)
            .query(query)
            .build()
            .unwrap()
            .supergraph_request
            .map(|req: graphql::Request| {
                let bytes = serde_json::to_vec(&req).unwrap();
                hyper::Body::from(bytes)
            });
        let config = serde_json::json!({
            "batching": {
                "enabled": true,
                "mode" : "batch_http_link"
            }
        });
        crate::TestHarness::builder()
            .configuration_json(config)
            .unwrap()
            .build_router()
            .await
            .unwrap()
            .oneshot(router::Request::from(http_request))
            .await
            .unwrap()
    }
    // Send a request
    let response = with_config().await.response;
    assert_eq!(response.status(), http::StatusCode::OK);
    let bytes = get_body_bytes(response.into_body()).await.unwrap();
    let data = String::from_utf8_lossy(&bytes);
    assert_eq!(expected_response, data);
}

#[tokio::test]
async fn it_will_not_process_a_batched_deferred_query() {
    let expected_response = "[\r\n--graphql\r\ncontent-type: application/json\r\n\r\n{\"errors\":[{\"message\":\"Deferred responses and subscriptions aren't supported in batches\",\"extensions\":{\"code\":\"BATCHING_DEFER_UNSUPPORTED\"}}]}\r\n--graphql--\r\n, \r\n--graphql\r\ncontent-type: application/json\r\n\r\n{\"errors\":[{\"message\":\"Deferred responses and subscriptions aren't supported in batches\",\"extensions\":{\"code\":\"BATCHING_DEFER_UNSUPPORTED\"}}]}\r\n--graphql--\r\n]";

    async fn with_config() -> router::Response {
        let query = "
            query TopProducts($first: Int) {
                topProducts(first: $first) {
                    upc
                    name
                    reviews {
                        ... @defer {
                        id
                        }
                        product { name }
                        author { id name }
                    }
                }
            }
        ";
        let http_request = make_fake_batch(
            supergraph::Request::canned_builder()
                .header(http::header::ACCEPT, MULTIPART_DEFER_CONTENT_TYPE)
                .query(query)
                .build()
                .unwrap()
                .supergraph_request,
            None,
        );
        let config = serde_json::json!({
            "batching": {
                "enabled": true,
                "mode" : "batch_http_link"
            }
        });
        crate::TestHarness::builder()
            .configuration_json(config)
            .unwrap()
            .build_router()
            .await
            .unwrap()
            .oneshot(router::Request::from(http_request))
            .await
            .unwrap()
    }
    // Send a request
    let response = with_config().await.response;
    assert_eq!(response.status(), http::StatusCode::NOT_ACCEPTABLE);
    let bytes = get_body_bytes(response.into_body()).await.unwrap();
    let data = String::from_utf8_lossy(&bytes);
    assert_eq!(expected_response, data);
}

/// <https://github.com/apollographql/router/issues/3541>
#[tokio::test]
async fn escaped_quotes_in_string_literal() {
    let query = r#"
        query TopProducts($first: Int) {
            topProducts(first: $first) {
                name
                reviewsForAuthor(authorID: "\"1\"") {
                    body
                }
            }
        }
    "#;
    let request = supergraph::Request::fake_builder()
        .query(query)
        .variable("first", 2)
        .build()
        .unwrap();
    let config = serde_json::json!({
        "include_subgraph_errors": {"all": true},
    });
    let subgraph_query_log = Arc::new(Mutex::new(Vec::new()));
    let subgraph_query_log_2 = subgraph_query_log.clone();
    let mut response = crate::TestHarness::builder()
        .configuration_json(config)
        .unwrap()
        .subgraph_hook(move |subgraph_name, service| {
            let is_reviews = subgraph_name == "reviews";
            let subgraph_name = subgraph_name.to_owned();
            let subgraph_query_log_3 = subgraph_query_log_2.clone();
            service
                .map_request(move |request: subgraph::Request| {
                    subgraph_query_log_3.lock().unwrap().push((
                        subgraph_name.clone(),
                        request.subgraph_request.body().query.clone(),
                    ));
                    request
                })
                .map_response(move |mut response| {
                    if is_reviews {
                        // Replace "couldn't find mock for query" error with empty data
                        let graphql_response = response.response.body_mut();
                        graphql_response.errors.clear();
                        graphql_response.data = Some(serde_json_bytes::json!({
                            "_entities": {"reviews": []},
                        }));
                    }
                    response
                })
                .boxed()
        })
        .build_supergraph()
        .await
        .unwrap()
        .oneshot(request)
        .await
        .unwrap();
    let graphql_response = response.next_response().await.unwrap();
    let subgraph_query_log = subgraph_query_log.lock().unwrap();
    insta::assert_debug_snapshot!((graphql_response, &subgraph_query_log));
    let subgraph_query = subgraph_query_log[1].1.as_ref().unwrap();

    // The string literal made it through unchanged:
    assert!(subgraph_query.contains(r#"reviewsForAuthor(authorID: "\"1\"")"#));
}

#[tokio::test]
async fn it_processes_a_valid_query_batch_with_maximum_size() {
    let expected_response: serde_json::Value = serde_json::from_str(include_str!(
        "../query_batching/testdata/expected_good_response.json"
    ))
    .unwrap();

    let http_request = batch_with_three_unique_queries();
    let config = serde_json::json!({
        "batching": {
            "enabled": true,
            "mode" : "batch_http_link",
            "maximum_size": 3
        }
    });

    // Send a request
    let response = oneshot_request(http_request, config).await.response;
    assert_eq!(response.status(), http::StatusCode::OK);

    let data: serde_json::Value =
        serde_json::from_slice(&get_body_bytes(response.into_body()).await.unwrap()).unwrap();
    assert_eq!(expected_response, data);
}

#[tokio::test]
async fn it_will_not_process_a_batch_that_exceeds_the_maximum_size() {
    let expected_response: serde_json::Value = serde_json::from_str(include_str!(
        "../query_batching/testdata/batch_exceeds_maximum_size_response.json"
    ))
    .unwrap();

    // NB: make_fake_batch creates a request with a batch size of 2
    let http_request = make_fake_batch(
        supergraph::Request::canned_builder()
            .build()
            .unwrap()
            .supergraph_request,
        None,
    );
    let config = serde_json::json!({
        "batching": {
            "enabled": true,
            "mode" : "batch_http_link",
            "maximum_size": 1
        }
    });

    // Send a request
    let response = oneshot_request(http_request, config).await.response;
    assert_eq!(response.status(), http::StatusCode::UNPROCESSABLE_ENTITY);

    let data: serde_json::Value =
        serde_json::from_slice(&get_body_bytes(response.into_body()).await.unwrap()).unwrap();
    assert_eq!(expected_response, data);
}

async fn oneshot_request(
    http_request: Request<hyper::Body>,
    config: serde_json::Value,
) -> router::Response {
    crate::TestHarness::builder()
        .configuration_json(config)
        .unwrap()
        .build_router()
        .await
        .unwrap()
        .oneshot(router::Request::from(http_request))
        .await
        .unwrap()
}

fn batch_with_three_unique_queries() -> Request<hyper::Body> {
    supergraph::Request::canned_builder()
        .build()
        .unwrap()
        .supergraph_request
        .map(|req_2: graphql::Request| {
            // Create clones of our standard query and update it to have 3 unique queries
            let mut req_1 = req_2.clone();
            let mut req_3 = req_2.clone();
            req_1.query = req_2.query.clone().map(|x| x.replace("upc\n", ""));
            req_3.query = req_2.query.clone().map(|x| x.replace("id name", "name"));

            // Modify the request so that it is a valid array of 3 requests.
            let mut json_bytes_1 = serde_json::to_vec(&req_1).unwrap();
            let mut json_bytes_2 = serde_json::to_vec(&req_2).unwrap();
            let mut json_bytes_3 = serde_json::to_vec(&req_3).unwrap();
            let mut result = vec![b'['];
            result.append(&mut json_bytes_1);
            result.push(b',');
            result.append(&mut json_bytes_2);
            result.push(b',');
            result.append(&mut json_bytes_3);
            result.push(b']');
            hyper::Body::from(result)
        })
}