apollo-router 2.14.0-rc.2

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
use futures::StreamExt;
use insta::with_settings;
use serde_json::Map;
use serde_json::Value;
use serde_json::json;
use tower::BoxError;

use super::*; // Import items from mod.rs
use crate::graphql;
use crate::plugins::test::PluginTestHarness;
use crate::services::supergraph; // Required for collect

const PRODUCT_ERROR_RESPONSE: &[&str] = &[
    r#"{"data":{"topProducts":null},"errors":[{"message":"Could not query products","path":[],"extensions":{"test":"value","code":"FETCH_ERROR", "apollo.private.subgraph.name": "products"}}]}"#,
];
const ACCOUNT_ERROR_RESPONSE: &[&str] = &[
    r#"{"data":null,"errors":[{"message":"Account service error","path":[],"extensions":{"code":"ACCOUNT_FAIL", "apollo.private.subgraph.name": "accounts"}}]}"#,
];
const VALID_RESPONSE: &[&str] = &[
    r#"{"data":{"topProducts":[{"upc":"1","name":"Table","reviews":[{"id":"1","product":{"name":"Table"},"author":{"id":"1","name":"Ada Lovelace"}},{"id":"4","product":{"name":"Table"},"author":{"id":"2","name":"Alan Turing"}}]},{"upc":"2","name":"Couch","reviews":[{"id":"2","product":{"name":"Couch"},"author":{"id":"1","name":"Ada Lovelace"}}]}]}}"#,
];
const NON_SUBGRAPH_ERROR: &[&str] = &[
    r#"{"data":{"topProducts":null},"errors":[{"message":"Authentication error","path":[],"extensions":{"test":"value","code":"AUTH_ERROR"}}]}"#,
];
const INCREMENTAL_RESPONSE: &[&str] = &[
    r#"{"data":{"topProducts":null},"errors":[{"message":"Main errors error","path":[],"extensions":{"test":"value","code":"MAIN_ERROR", "apollo.private.subgraph.name": "products"}}]}"#,
    r#"{"incremental":[{"data":{"topProducts":null},"errors":[{"message":"Incremental error","path":[],"extensions":{"test":"value","code":"INCREMENTAL_ERROR", "apollo.private.subgraph.name": "products"}}]}]}"#,
];

async fn build_harness(
    plugin_config: &Value,
) -> Result<PluginTestHarness<IncludeSubgraphErrors>, BoxError> {
    let mut config = Map::new();
    config.insert("include_subgraph_errors".to_string(), plugin_config.clone());
    let config = serde_yaml::to_string(&config).expect("config to yaml");
    PluginTestHarness::builder().config(&config).build().await
}

async fn run_test_case(
    config: &Value,
    mock_responses: &[&str], // The array of responses
    snapshot_suffix: &str,   // Suffix for the snapshot file name
) {
    let harness = build_harness(config).await.expect("plugin should load");

    let mock_response_elements = mock_responses
        .iter()
        .map(|response| {
            serde_json::from_str(response).expect("Failed to parse mock response bytes")
        })
        .collect::<Vec<_>>();

    let service = harness.supergraph_service(move |req| {
        let mock_response_elements = mock_response_elements.clone();
        async {
            supergraph::Response::fake_stream_builder()
                .responses(mock_response_elements)
                .context(req.context)
                .build()
        }
    });
    let mut response = service.call_default().await.unwrap();

    // Collect the actual response body (potentially modified by the plugin)
    let actual_responses: Vec<graphql::Response> = response.response.body_mut().collect().await;

    let config = serde_yaml::to_string(config).expect("config to yaml");
    let parsed_responses = mock_responses
        .iter()
        .map(|response| serde_json::from_str(response).expect("request"))
        .collect::<Vec<Value>>();
    let request = serde_json::to_string_pretty(&parsed_responses).expect("request to json");

    let description = format!("CONFIG:\n{config}\n\nREQUEST:\n{request}");
    with_settings!({
        description => description,
    }, {
        // Assert the collected body against a snapshot
        insta::assert_yaml_snapshot!(snapshot_suffix, actual_responses);
    });
}

#[tokio::test]
async fn it_returns_valid_response() {
    run_test_case(
        &json!({ "all": false }),
        VALID_RESPONSE,   // Mock stream input
        "valid_response", // Snapshot suffix
    )
    .await;
}

#[tokio::test]
async fn it_redacts_all_subgraphs_explicit_redact() {
    run_test_case(
        &json!({ "all": false }),
        PRODUCT_ERROR_RESPONSE, // Mock original error
        "redact_all_explicit",  // Snapshot suffix
    )
    .await;
}

#[tokio::test]
async fn it_redacts_all_subgraphs_implicit_redact() {
    run_test_case(
        &json!({}), // Default is all: false
        PRODUCT_ERROR_RESPONSE,
        "redact_all_implicit",
    )
    .await;
}

#[tokio::test]
async fn it_does_not_redact_all_subgraphs_explicit_allow() {
    run_test_case(
        &json!({ "all": true }),
        PRODUCT_ERROR_RESPONSE, // Mock original error
        "allow_all_explicit",   // Snapshot suffix
    )
    .await;
}

#[tokio::test]
async fn it_does_not_redact_all_implicit_redact_product_explicit_allow_for_product_query() {
    run_test_case(
        &json!({ "subgraphs": {"products": true }}), // Default all: false
        PRODUCT_ERROR_RESPONSE,
        "allow_product_override_implicit_redact",
    )
    .await;
}

#[tokio::test]
async fn it_does_redact_all_implicit_redact_product_explicit_allow_for_review_query() {
    run_test_case(
        &json!({ "subgraphs": {"reviews": true }}), // Allows reviews, defaults products to redact
        PRODUCT_ERROR_RESPONSE,                     // Mock original error for products
        "redact_product_when_review_allowed",
    )
    .await;
}

#[tokio::test]
async fn it_does_not_redact_all_explicit_allow_review_explicit_redact_for_product_query() {
    run_test_case(
        &json!({ "all": true, "subgraphs": {"reviews": false }}), // Global allow, reviews redact
        PRODUCT_ERROR_RESPONSE,                                   // Mock original
        "allow_product_when_review_redacted",
    )
    .await;
}

#[tokio::test]
async fn it_does_redact_all_explicit_allow_product_explicit_redact_for_product_query() {
    run_test_case(
        &json!({ "all": true, "subgraphs": {"products": false }}), // Global allow, products redact
        PRODUCT_ERROR_RESPONSE,                                    // Mock original
        "redact_product_override_explicit_allow",
    )
    .await;
}

#[tokio::test]
async fn it_does_not_redact_all_explicit_allow_account_explicit_redact_for_product_query() {
    run_test_case(
        &json!({ "all": true, "subgraphs": {"accounts": false }}), // Global allow, accounts redact
        PRODUCT_ERROR_RESPONSE,                                    // Mock original
        "allow_product_when_account_redacted",
    )
    .await;
}

#[tokio::test]
async fn it_does_redact_all_explicit_allow_account_explicit_redact_for_account_query() {
    run_test_case(
        &json!({ "all": true, "subgraphs": {"accounts": false }}), // Global allow, accounts redact
        ACCOUNT_ERROR_RESPONSE,                                    // Mock original account error
        "redact_account_override_explicit_allow",
    )
    .await;
}

#[tokio::test]
async fn it_does_not_allow_both_allow_and_deny_list_in_global_config() {
    let config_json = json!({
        "all": {
            "redact_message": false,
            "allow_extensions_keys": [],
            "deny_extensions_keys": []
        }
    });
    let result = build_harness(&config_json).await;
    assert_eq!(
        result.expect_err("expected error").to_string(),
        "Global config cannot have both allow_extensions_keys and deny_extensions_keys"
    );
}

#[tokio::test]
async fn it_does_not_allow_both_allow_and_deny_list_in_a_subgraph_config() {
    let config_json = json!({
        "all": { // Global must be object type if subgraph is object type
            "redact_message": false,
            "allow_extensions_keys": [],
        },
        "subgraphs": {
            "products": {
                "redact_message": false,
                "allow_extensions_keys": [],
                "deny_extensions_keys": []
            }
        }
    });
    let result = build_harness(&config_json).await;
    assert_eq!(
        result.expect_err("expected error").to_string(),
        "A subgraph config cannot have both allow_extensions_keys and deny_extensions_keys"
    );
}

#[tokio::test]
async fn it_does_not_allow_subgraph_config_with_object_when_global_is_boolean() {
    let config_json = json!({
        "all": false, // Global is boolean
        "subgraphs": {
            "products": { // Subgraph is object
                "redact_message": true
            }
        }
    });
    let result = build_harness(&config_json).await;
    assert_eq!(
        result.expect_err("expected error").to_string(),
        "Subgraph 'products' must use boolean config when global config is boolean"
    );
}

#[tokio::test]
async fn it_allows_subgraph_config_with_boolean_when_global_is_object() {
    let config_json = json!({
        "all": {
            "redact_message": true,
            "deny_extensions_keys": ["code"]
        },
        "subgraphs": {
            "products": true // Boolean subgraph config is allowed
        }
    });
    let result = build_harness(&config_json).await;
    assert!(result.is_ok()); // Check plugin creation succeeded
}

#[tokio::test]
async fn it_allows_any_subgraph_config_type_when_global_is_object() {
    let config_json = json!({
        "all": {
            "redact_message": true,
            "deny_extensions_keys": ["code"] // Global deny list
        },
        "subgraphs": {
            "products": {
                "allow_extensions_keys": ["code"]  // Subgraph allow overrides global deny
            },
            "reviews": {
                "deny_extensions_keys": ["reason"]  // Subgraph deny extends global deny
            },
            "inventory": {
                "exclude_global_keys": ["code"]  // CommonOnly inherits global deny, but excludes 'code'
            },
            "accounts": true  // Boolean overrides global object config
        }
    });
    let result = build_harness(&config_json).await;
    assert!(result.is_ok());
}

#[tokio::test]
async fn it_filters_extensions_based_on_global_allow_list_and_redacts_message() {
    run_test_case(
        &json!({
            "all": {
                "redact_message": true,
                "allow_extensions_keys": ["code", "service"] // Allow 'code' and 'service'
            }
        }),
        PRODUCT_ERROR_RESPONSE,
        "filter_global_allow_redact_msg",
    )
    .await;
}

#[tokio::test]
async fn it_filters_extensions_based_on_global_allow_list_keeps_message() {
    run_test_case(
        &json!({
            "all": {
                "redact_message": false,
                "allow_extensions_keys": ["code", "service"] // Allow 'code' and 'service'
            }
        }),
        PRODUCT_ERROR_RESPONSE,
        "filter_global_allow_keep_msg",
    )
    .await;
}

#[tokio::test]
async fn it_allows_subgraph_bool_override_global_deny_config() {
    run_test_case(
        &json!({
            "all": {
                "redact_message": true,
                "deny_extensions_keys": ["code"],
            },
            "subgraphs": { "products": true }
        }),
        PRODUCT_ERROR_RESPONSE,
        "subgraph_bool_true_override_global_deny",
    )
    .await;
}

#[tokio::test]
async fn it_allows_subgraph_bool_override_global_allow_config() {
    run_test_case(
        &json!({
            "all": {
                "redact_message": true,
                "allow_extensions_keys": ["code"],
            },
            "subgraphs": { "products": false }
        }),
        PRODUCT_ERROR_RESPONSE,
        "subgraph_bool_false_override_global_allow",
    )
    .await;
}

#[tokio::test]
async fn it_allows_subgraph_object_to_override_global_redaction() {
    run_test_case(
        &json!({
            "all": {
                "redact_message": false,
                "allow_extensions_keys": ["code", "service"],
            },
            "subgraphs": {
                "products": { "redact_message": true } // Override redaction
            }
        }),
        PRODUCT_ERROR_RESPONSE,
        "subgraph_obj_override_redaction",
    )
    .await;
}

#[tokio::test]
async fn it_allows_subgraph_to_exclude_key_from_global_allow_list() {
    run_test_case(
        &json!({
            "all": {
                "redact_message": false,
                "allow_extensions_keys": ["code", "test", "service"]
            },
            "subgraphs": {
                "products": { "exclude_global_keys": ["test"] } // Exclude 'test'
            }
        }),
        PRODUCT_ERROR_RESPONSE,
        "subgraph_exclude_global_allow",
    )
    .await;
}

#[tokio::test]
async fn it_allows_subgraph_deny_list_to_override_global_allow_list() {
    run_test_case(
        &json!({
            "all": {
                "redact_message": false,
                "allow_extensions_keys": ["code", "test", "service"]
            },
            "subgraphs": {
                "products": { "deny_extensions_keys": ["test", "service"] } // Deny overrides global allow
            }
        }),
        PRODUCT_ERROR_RESPONSE,
        "subgraph_deny_override_global_allow",
    )
    .await;
}

#[tokio::test]
async fn it_allows_subgraph_allow_list_to_override_global_deny_list() {
    run_test_case(
        &json!({
            "all": {
                "redact_message": false,
                "deny_extensions_keys": ["test", "service"]
            },
            "subgraphs": {
                "products": { "allow_extensions_keys": ["code", "test"] } // Allow overrides global deny for 'test'
            }
        }),
        PRODUCT_ERROR_RESPONSE,
        "subgraph_allow_override_global_deny",
    )
    .await;
}

#[tokio::test]
async fn it_allows_subgraph_deny_list_to_extend_global_deny_list() {
    run_test_case(
        &json!({
            "all": {
                "redact_message": true,
                "deny_extensions_keys": ["test"]
            },
            "subgraphs": {
                "products": { "deny_extensions_keys": ["code"] } // Extends global deny
            }
        }),
        PRODUCT_ERROR_RESPONSE,
        "subgraph_deny_extend_global_deny",
    )
    .await;
}

#[tokio::test]
async fn it_allows_subgraph_allow_list_to_extend_global_allow_list() {
    run_test_case(
        &json!({
            "all": {
                "redact_message": false,
                "allow_extensions_keys": ["test", "service"]
            },
            "subgraphs": {
                "products": { "allow_extensions_keys": ["code"] } // Extends global allow
            }
        }),
        PRODUCT_ERROR_RESPONSE,
        "subgraph_allow_extend_global_allow",
    )
    .await;
}

#[tokio::test]
async fn it_redacts_service_extension_if_denied() {
    run_test_case(
        &json!({
            "all": {
                "redact_message": false,
                "allow_extensions_keys": ["code", "test", "service"] // Allow globally initially
            },
            "subgraphs": {
                "products": { "deny_extensions_keys": ["service"] } // Deny service specifically
            }
        }),
        PRODUCT_ERROR_RESPONSE,
        "subgraph_deny_service",
    )
    .await;
}

#[tokio::test]
async fn it_includes_service_extension_if_allowed() {
    run_test_case(
        &json!({
            "all": {
                "redact_message": true,
                "deny_extensions_keys": ["code", "test"]
            },
            "subgraphs": {
                "products": { "allow_extensions_keys": ["service"] } // Allow service specifically
            }
        }),
        PRODUCT_ERROR_RESPONSE,
        "subgraph_allow_service",
    )
    .await;
}

#[tokio::test]
async fn it_does_not_add_service_extension_for_non_subgraph_errors() {
    run_test_case(
        &json!({
            "all": true,
        }),
        NON_SUBGRAPH_ERROR,
        "non_subgraph_error",
    )
    .await;
}

#[tokio::test]
async fn it_processes_incremental_responses() {
    run_test_case(
        &json!({
            "all": true,
        }),
        INCREMENTAL_RESPONSE,
        "incremental_response",
    )
    .await;
}