apollo-router 2.13.1

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
use apollo_compiler::Schema;
use test_log::test;

use super::*;
use crate::Configuration;

fn assert_expected_signature(actual: &UsageReporting, expected_sig: &str) {
    assert_eq!(actual.get_stats_report_key(), expected_sig);
}

macro_rules! assert_extended_references {
    ($actual:expr) => {
        insta::with_settings!({sort_maps => true}, {
            insta::assert_yaml_snapshot!($actual, {
                // sort referenced enum value sets
                ".referenced_enums.*" => insta::sorted_redaction()
            });
        });
    };
}

macro_rules! assert_enums_from_response {
    ($actual:expr) => {
        insta::with_settings!({sort_maps => true}, {
            insta::assert_yaml_snapshot!($actual, {
                // sort referenced enum value sets
                ".*" => insta::sorted_redaction()
            });
        });
    };
}

// Generate usage reporting with the same signature and refs doc, and with enhanced normalization algorithm
fn generate_enhanced(
    doc: &ExecutableDocument,
    operation_name: &Option<String>,
    schema: &Valid<Schema>,
) -> UsageReporting {
    generate_usage_reporting(
        doc,
        doc,
        operation_name,
        schema,
        &ApolloSignatureNormalizationAlgorithm::Enhanced,
    )
}

// Generate extended references (input objects and enum values)
fn generate_extended_refs(
    doc: &Valid<ExecutableDocument>,
    operation_name: Option<String>,
    schema: &Valid<Schema>,
    variables: Option<&Object>,
) -> ExtendedReferenceStats {
    let default_vars = Object::new();
    generate_extended_references(
        Arc::new(doc.clone()),
        operation_name,
        schema,
        variables.unwrap_or(&default_vars),
    )
}

fn enums_from_response(
    query_str: &str,
    operation_name: Option<&str>,
    schema_str: &str,
    response_body_str: &str,
) -> ReferencedEnums {
    let config = Configuration::default();
    let schema = crate::spec::Schema::parse(schema_str, &config).unwrap();
    let query = Query::parse(query_str, operation_name, &schema, &config).unwrap();
    let response_body: Object = serde_json::from_str(response_body_str).unwrap();

    let mut result = ReferencedEnums::new();
    extract_enums_from_response(
        Arc::new(query),
        schema.supergraph_schema(),
        &response_body,
        &mut result,
    );
    result
}

#[test(tokio::test)]
async fn test_enhanced_uses_comma_always() {
    let schema_str = include_str!("testdata/schema_interop.graphql");
    let query_str = include_str!("testdata/enhanced_uses_comma_always_query.graphql");

    let schema = Schema::parse_and_validate(schema_str, "schema.graphql").unwrap();
    let doc = ExecutableDocument::parse(&schema, query_str, "query.graphql").unwrap();

    let generated = generate_enhanced(&doc, &Some("TestCommaEnhanced".into()), &schema);
    let expected_sig = "# TestCommaEnhanced\nquery TestCommaEnhanced($arg1:String,$arg2:String,$veryMuchUsuallyTooLongName1234567890:String){manyArgsQuery(arg1:$arg1,arg2:$arg2,arg3:\"\",arg4:$veryMuchUsuallyTooLongName1234567890){basicTypes{nullableId}enumResponse id}}";
    assert_expected_signature(&generated, expected_sig);
}

#[test(tokio::test)]
async fn test_enhanced_sorts_fragments() {
    let schema_str = include_str!("testdata/schema_interop.graphql");
    let query_str = include_str!("testdata/enhanced_sorts_fragments_query.graphql");

    let schema = Schema::parse_and_validate(schema_str, "schema.graphql").unwrap();
    let doc = ExecutableDocument::parse(&schema, query_str, "query.graphql").unwrap();

    let generated = generate_enhanced(&doc, &Some("EnhancedFragmentQuery".into()), &schema);
    let expected_sig = "# EnhancedFragmentQuery\nfragment ZZZFragment on EverythingResponse{listOfInterfaces{sharedField}}fragment aaaFragment on EverythingResponse{listOfInterfaces{sharedField}}fragment aaaInterfaceFragment on InterfaceImplementation1{sharedField}fragment bbbInterfaceFragment on InterfaceImplementation2{implementation2Field sharedField}fragment zzzFragment on EverythingResponse{listOfInterfaces{sharedField}}query EnhancedFragmentQuery{noInputQuery{interfaceResponse{...aaaInterfaceFragment...bbbInterfaceFragment...{...on InterfaceImplementation1{implementation1Field}}...{...on InterfaceImplementation2{sharedField}}...on InterfaceImplementation1{implementation1Field}...on InterfaceImplementation2{implementation2Field}}listOfBools unionResponse{...on UnionType1{unionType1Field}...on UnionType2{unionType2Field}}...ZZZFragment...aaaFragment...zzzFragment}}";
    assert_expected_signature(&generated, expected_sig);
}

#[test(tokio::test)]
async fn test_enhanced_sorts_directives() {
    let schema_str = include_str!("testdata/schema_interop.graphql");
    let query_str = include_str!("testdata/enhanced_sorts_directives_query.graphql");

    let schema = Schema::parse_and_validate(schema_str, "schema.graphql").unwrap();
    let doc = ExecutableDocument::parse(&schema, query_str, "query.graphql").unwrap();

    let generated = generate_enhanced(&doc, &Some("DirectiveQuery".into()), &schema);
    let expected_sig = "# DirectiveQuery\nfragment Fragment1 on InterfaceImplementation1{implementation1Field sharedField}fragment Fragment2 on InterfaceImplementation2@noArgs@withArgs(arg1:\"\",arg2:\"\",arg3:true,arg4:0,arg5:[]){implementation2Field sharedField}query DirectiveQuery@noArgs@withArgs(arg1:\"\",arg2:\"\"){noInputQuery{enumResponse@noArgs@withArgs(arg3:false,arg4:0,arg5:[])interfaceResponse{...Fragment1@noArgs@withArgs(arg1:\"\")...Fragment2}unionResponse{...on UnionType1@noArgs@withArgs(arg1:\"\",arg2:\"\"){unionType1Field}}}}";
    assert_expected_signature(&generated, expected_sig);
}

#[test(tokio::test)]
async fn test_enhanced_inline_input_object() {
    let schema_str = include_str!("testdata/schema_interop.graphql");
    let query_str: &str = include_str!("testdata/enhanced_inline_input_object_query.graphql");

    let schema = Schema::parse_and_validate(schema_str, "schema.graphql").unwrap();
    let doc = ExecutableDocument::parse(&schema, query_str, "query.graphql").unwrap();

    let generated = generate_enhanced(&doc, &Some("InputObjectTypeQuery".into()), &schema);
    #[allow(clippy::literal_string_with_formatting_args)]
    let expected_sig = "# InputObjectTypeQuery\nquery InputObjectTypeQuery{inputTypeQuery(input:{inputString:\"\",inputInt:0,inputBoolean:null,nestedType:{someFloat:0},enumInput:SOME_VALUE_1,nestedTypeList:[],listInput:[]}){enumResponse}}";
    assert_expected_signature(&generated, expected_sig);
}

#[test(tokio::test)]
async fn test_enhanced_alias_preservation() {
    let schema_str = include_str!("testdata/schema_interop.graphql");
    let query_str = include_str!("testdata/enhanced_alias_preservation_query.graphql");

    let schema = Schema::parse_and_validate(schema_str, "schema.graphql").unwrap();
    let doc = ExecutableDocument::parse(&schema, query_str, "query.graphql").unwrap();

    let generated = generate_enhanced(&doc, &Some("AliasQuery".into()), &schema);
    let expected_sig = "# AliasQuery\nquery AliasQuery{enumInputQuery(enumInput:SOME_VALUE_1){enumResponse nullableId aliasedId:id}ZZAlias:enumInputQuery(enumInput:SOME_VALUE_3){enumResponse}aaAlias:enumInputQuery(enumInput:SOME_VALUE_2){aliasedAgain:enumResponse}xxAlias:enumInputQuery(enumInput:SOME_VALUE_1){aliased:enumResponse}}";
    assert_expected_signature(&generated, expected_sig);
}

#[test(tokio::test)]
async fn test_extended_references_inline_enums() {
    let schema_str = include_str!("testdata/schema_interop.graphql");
    let query_str = include_str!("testdata/extended_references_inline_enums.graphql");

    let schema = Schema::parse_and_validate(schema_str, "schema.graphql").unwrap();
    let doc = ExecutableDocument::parse_and_validate(&schema, query_str, "query.graphql").unwrap();

    let generated = generate_extended_refs(&doc, Some("EnumInlineQuery".into()), &schema, None);
    assert_extended_references!(&generated);
}

#[test(tokio::test)]
async fn test_extended_references_var_enums() {
    let schema_str = include_str!("testdata/schema_interop.graphql");
    let query_str = include_str!("testdata/extended_references_var_enums.graphql");
    let query_vars_str = include_str!("testdata/extended_references_var_enums.json");

    let schema = Schema::parse_and_validate(schema_str, "schema.graphql").unwrap();
    let doc = ExecutableDocument::parse_and_validate(&schema, query_str, "query.graphql").unwrap();
    let vars: Object = serde_json::from_str(query_vars_str).unwrap();

    let generated = generate_extended_refs(&doc, Some("EnumVarQuery".into()), &schema, Some(&vars));
    assert_extended_references!(&generated);
}

#[test(tokio::test)]
async fn test_extended_references_fragment_inline_enums() {
    let schema_str = include_str!("testdata/schema_interop.graphql");
    let query_str = include_str!("testdata/extended_references_fragment_inline_enums.graphql");

    let schema = Schema::parse_and_validate(schema_str, "schema.graphql").unwrap();
    let doc = ExecutableDocument::parse_and_validate(&schema, query_str, "query.graphql").unwrap();

    let generated = generate_extended_refs(
        &doc,
        Some("EnumInlineQueryWithFragment".into()),
        &schema,
        None,
    );
    assert_extended_references!(&generated);
}

#[test(tokio::test)]
async fn test_extended_references_fragment_var_enums() {
    let schema_str = include_str!("testdata/schema_interop.graphql");
    let query_str = include_str!("testdata/extended_references_fragment_var_enums.graphql");
    let query_vars_str = include_str!("testdata/extended_references_fragment_var_enums.json");

    let schema = Schema::parse_and_validate(schema_str, "schema.graphql").unwrap();
    let doc = ExecutableDocument::parse_and_validate(&schema, query_str, "query.graphql").unwrap();
    let vars: Object = serde_json::from_str(query_vars_str).unwrap();

    let generated = generate_extended_refs(
        &doc,
        Some("EnumVarQueryWithFragment".into()),
        &schema,
        Some(&vars),
    );
    assert_extended_references!(&generated);
}

#[test(tokio::test)]
async fn test_extended_references_inline_type() {
    let schema_str = include_str!("testdata/schema_interop.graphql");
    let query_str = include_str!("testdata/extended_references_inline_type.graphql");

    let schema = Schema::parse_and_validate(schema_str, "schema.graphql").unwrap();
    let doc = ExecutableDocument::parse_and_validate(&schema, query_str, "query.graphql").unwrap();

    let generated =
        generate_extended_refs(&doc, Some("InputTypeInlineQuery".into()), &schema, None);
    assert_extended_references!(&generated);
}

#[test(tokio::test)]
async fn test_extended_references_var_type() {
    let schema_str = include_str!("testdata/schema_interop.graphql");
    let query_str = include_str!("testdata/extended_references_var_type.graphql");
    let query_vars_str = include_str!("testdata/extended_references_var_type.json");

    let schema = Schema::parse_and_validate(schema_str, "schema.graphql").unwrap();
    let doc = ExecutableDocument::parse_and_validate(&schema, query_str, "query.graphql").unwrap();
    let vars: Object = serde_json::from_str(query_vars_str).unwrap();

    let generated = generate_extended_refs(
        &doc,
        Some("InputTypeVariablesQuery".into()),
        &schema,
        Some(&vars),
    );
    assert_extended_references!(&generated);
}

#[test(tokio::test)]
async fn test_extended_references_inline_nested_type() {
    let schema_str = include_str!("testdata/schema_interop.graphql");
    let query_str = include_str!("testdata/extended_references_inline_nested_type.graphql");

    let schema = Schema::parse_and_validate(schema_str, "schema.graphql").unwrap();
    let doc = ExecutableDocument::parse_and_validate(&schema, query_str, "query.graphql").unwrap();

    let generated = generate_extended_refs(
        &doc,
        Some("NestedInputTypeInlineQuery".into()),
        &schema,
        None,
    );
    assert_extended_references!(&generated);
}

#[test(tokio::test)]
async fn test_extended_references_var_nested_type() {
    let schema_str = include_str!("testdata/schema_interop.graphql");
    let query_str = include_str!("testdata/extended_references_var_nested_type.graphql");
    let query_vars_str = include_str!("testdata/extended_references_var_nested_type.json");

    let schema = Schema::parse_and_validate(schema_str, "schema.graphql").unwrap();
    let doc = ExecutableDocument::parse_and_validate(&schema, query_str, "query.graphql").unwrap();
    let vars: Object = serde_json::from_str(query_vars_str).unwrap();

    let generated = generate_extended_refs(
        &doc,
        Some("NestedInputTypeVarsQuery".into()),
        &schema,
        Some(&vars),
    );
    assert_extended_references!(&generated);
}

#[test(tokio::test)]
async fn test_extended_references_nested_query() {
    let schema_str = include_str!("testdata/schema_interop.graphql");
    let query_str = include_str!("testdata/extended_references_var_nested_type.graphql");
    let query_vars_str = include_str!("testdata/extended_references_var_nested_type.json");

    let schema = Schema::parse_and_validate(schema_str, "schema.graphql").unwrap();
    let doc = ExecutableDocument::parse_and_validate(&schema, query_str, "query.graphql").unwrap();
    let vars: Object = serde_json::from_str(query_vars_str).unwrap();

    let generated = generate_extended_refs(
        &doc,
        Some("NestedInputTypeVarsQuery".into()),
        &schema,
        Some(&vars),
    );
    assert_extended_references!(&generated);
}

#[test(tokio::test)]
async fn test_enums_from_response_complex_response_type() {
    let schema_str = include_str!("testdata/schema_interop.graphql");
    let query_str = include_str!("testdata/enums_from_response_complex_response_type.graphql");
    let response_str =
        include_str!("testdata/enums_from_response_complex_response_type_response.json");
    let op_name = Some("EnumResponseQuery");

    let generated = enums_from_response(query_str, op_name, schema_str, response_str);
    assert_enums_from_response!(&generated);
}

#[test(tokio::test)]
async fn test_enums_from_response_fragments() {
    let schema_str = include_str!("testdata/schema_interop.graphql");
    let query_str = include_str!("testdata/enums_from_response_fragments.graphql");
    let response_str = include_str!("testdata/enums_from_response_fragments_response.json");
    let op_name = Some("EnumResponseQueryFragments");

    let generated = enums_from_response(query_str, op_name, schema_str, response_str);
    assert_enums_from_response!(&generated);
}

#[test]
fn apollo_operation_id_hash() {
    let usage_reporting = UsageReporting::Operation(UsageReportingOperationDetails {
        operation_name: Some("IgnitionMeQuery".to_string()),
        operation_signature: Some("query IgnitionMeQuery{me{id}}".to_string()),
        referenced_fields_by_type: HashMap::new(),
    });

    assert_eq!(
        "d1554552698157b05c2a462827fb4367a4548ee5",
        usage_reporting.get_operation_id()
    );
}

// The Apollo operation ID hash for these errors is based on a slightly different string. E.g. instead of hashing
// "## GraphQLValidationFailure\n" we should hash "# # GraphQLValidationFailure".
#[test]
fn apollo_error_operation_id_hash() {
    assert_eq!(
        "ea4f152696abedca148b016d72df48842b713697",
        UsageReporting::Error("GraphQLValidationFailure".into()).get_operation_id()
    );
    assert_eq!(
        "3f410834f13153f401ffe73f7e454aa500d10bf7",
        UsageReporting::Error("GraphQLParseFailure".into()).get_operation_id()
    );
    assert_eq!(
        "7486043da2085fed407d942508a572ef88dc8120",
        UsageReporting::Error("GraphQLUnknownOperationName".into()).get_operation_id()
    );
}

#[test]
fn test_get_stats_report_key_and_metadata() {
    let usage_reporting_for_errors = UsageReporting::Error("GraphQLParseFailure".into());
    assert_eq!(
        "## GraphQLParseFailure\n",
        usage_reporting_for_errors.get_stats_report_key()
    );
    assert_eq!(None, usage_reporting_for_errors.get_query_metadata());

    let usage_reporting_for_pq = UsageReporting::PersistedQuery {
        operation_details: UsageReportingOperationDetails {
            operation_name: Some("SomeQuery".into()),
            operation_signature: Some("query SomeQuery{thing{id}}".into()),
            referenced_fields_by_type: HashMap::new(),
        },
        persisted_query_id: "SomePqId".into(),
    };
    assert_eq!(
        "pq# ",
        usage_reporting_for_pq
            .get_stats_report_key()
            .chars()
            .take(4)
            .collect::<String>()
    );
    assert_eq!(
        Some(QueryMetadata {
            name: "SomeQuery".into(),
            signature: "query SomeQuery{thing{id}}".into(),
            pq_id: "SomePqId".into()
        }),
        usage_reporting_for_pq.get_query_metadata()
    );

    let usage_reporting_for_named_operation =
        UsageReporting::Operation(UsageReportingOperationDetails {
            operation_name: Some("SomeQuery".into()),
            operation_signature: Some("query SomeQuery{thing{id}}".into()),
            referenced_fields_by_type: HashMap::new(),
        });
    assert_eq!(
        "# SomeQuery\nquery SomeQuery{thing{id}}",
        usage_reporting_for_named_operation.get_stats_report_key()
    );
    assert_eq!(
        None,
        usage_reporting_for_named_operation.get_query_metadata()
    );

    let usage_reporting_for_unnamed_operation =
        UsageReporting::Operation(UsageReportingOperationDetails {
            operation_name: None,
            operation_signature: Some("query{thing{id}}".into()),
            referenced_fields_by_type: HashMap::new(),
        });
    assert_eq!(
        "# -\nquery{thing{id}}",
        usage_reporting_for_unnamed_operation.get_stats_report_key()
    );
    assert_eq!(
        None,
        usage_reporting_for_unnamed_operation.get_query_metadata()
    );
}

// The stats report key should be distinct per combination of operation name/signature and PQ ID. All of these
// details are stored in metadata, so it's not important what the actual stats report key is, it's only important
// that they are distinct for each combination, but identical for the same operation name/signature and PQ ID.
#[test]
fn test_get_stats_report_key_uses_distinct_keys_for_pq_operations() {
    let usage_reporting_op_1_pq_1 = UsageReporting::PersistedQuery {
        operation_details: UsageReportingOperationDetails {
            operation_name: Some("SomeQuery1".into()),
            operation_signature: Some("query SomeQuery1{thing{id}}".into()),
            referenced_fields_by_type: HashMap::new(),
        },
        persisted_query_id: "SomePqId1".into(),
    };
    let usage_reporting_op_1_pq_1_again = UsageReporting::PersistedQuery {
        operation_details: UsageReportingOperationDetails {
            operation_name: Some("SomeQuery1".into()),
            operation_signature: Some("query SomeQuery1{thing{id}}".into()),
            referenced_fields_by_type: HashMap::new(),
        },
        persisted_query_id: "SomePqId1".into(),
    };
    assert_eq!(
        usage_reporting_op_1_pq_1.get_stats_report_key(),
        usage_reporting_op_1_pq_1_again.get_stats_report_key()
    );

    let usage_reporting_op_1_pq_1_different_name = UsageReporting::PersistedQuery {
        operation_details: UsageReportingOperationDetails {
            operation_name: Some("DifferentName".into()),
            operation_signature: Some("query SomeQuery1{thing{id}}".into()),
            referenced_fields_by_type: HashMap::new(),
        },
        persisted_query_id: "SomePqId1".into(),
    };
    let usage_reporting_op_2_pq_1 = UsageReporting::PersistedQuery {
        operation_details: UsageReportingOperationDetails {
            operation_name: Some("SomeQuery2".into()),
            operation_signature: Some("query SomeQuery2{thing{id}}".into()),
            referenced_fields_by_type: HashMap::new(),
        },
        persisted_query_id: "SomePqId1".into(),
    };
    let usage_reporting_op_1_pq_2 = UsageReporting::PersistedQuery {
        operation_details: UsageReportingOperationDetails {
            operation_name: Some("SomeQuery1".into()),
            operation_signature: Some("query SomeQuery1{thing{id}}".into()),
            referenced_fields_by_type: HashMap::new(),
        },
        persisted_query_id: "SomePqId2".into(),
    };
    let usage_reporting_op_2_pq_2 = UsageReporting::PersistedQuery {
        operation_details: UsageReportingOperationDetails {
            operation_name: Some("SomeQuery2".into()),
            operation_signature: Some("query SomeQuery2{thing{id}}".into()),
            referenced_fields_by_type: HashMap::new(),
        },
        persisted_query_id: "SomePqId2".into(),
    };
    let usage_reporting_op_1_no_pq = UsageReporting::Operation(UsageReportingOperationDetails {
        operation_name: Some("SomeQuery1".into()),
        operation_signature: Some("query SomeQuery1{thing{id}}".into()),
        referenced_fields_by_type: HashMap::new(),
    });
    let usage_reporting_op_2_no_pq = UsageReporting::Operation(UsageReportingOperationDetails {
        operation_name: Some("SomeQuery2".into()),
        operation_signature: Some("query SomeQuery2{thing{id}}".into()),
        referenced_fields_by_type: HashMap::new(),
    });

    let stats_report_keys = [
        usage_reporting_op_1_pq_1,
        usage_reporting_op_1_pq_1_different_name,
        usage_reporting_op_2_pq_1,
        usage_reporting_op_1_pq_2,
        usage_reporting_op_2_pq_2,
        usage_reporting_op_1_no_pq,
        usage_reporting_op_2_no_pq,
    ]
    .map(|x| x.get_stats_report_key());

    // Check that all the stats report keys are distinct
    for i in 0..stats_report_keys.len() {
        for j in (i + 1)..stats_report_keys.len() {
            assert_ne!(
                stats_report_keys[i], stats_report_keys[j],
                "Stats report keys should be distinct: {} == {}",
                stats_report_keys[i], stats_report_keys[j]
            );
        }
    }
}

#[test]
fn test_get_operation_name() {
    let usage_reporting_for_errors = UsageReporting::Error("GraphQLParseFailure".into());
    assert_eq!(
        "# GraphQLParseFailure",
        usage_reporting_for_errors.get_operation_name()
    );

    let usage_reporting_for_named_operation =
        UsageReporting::Operation(UsageReportingOperationDetails {
            operation_name: Some("SomeQuery".into()),
            operation_signature: Some("query SomeQuery{thing{id}}".into()),
            referenced_fields_by_type: HashMap::new(),
        });
    assert_eq!(
        "SomeQuery",
        usage_reporting_for_named_operation.get_operation_name()
    );

    let usage_reporting_for_unnamed_operation =
        UsageReporting::Operation(UsageReportingOperationDetails {
            operation_name: None,
            operation_signature: Some("query{thing{id}}".into()),
            referenced_fields_by_type: HashMap::new(),
        });
    assert_eq!(
        "",
        usage_reporting_for_unnamed_operation.get_operation_name()
    );
}

#[test(tokio::test)]
async fn test_enums_with_nested_query_fragment() {
    let schema_str = include_str!("testdata/schema_interop.graphql");
    let query_str = include_str!("testdata/enums_from_response_with_nested_query_fragment.graphql");

    let schema = Schema::parse_and_validate(schema_str, "schema.graphql").unwrap();
    let doc = ExecutableDocument::parse_and_validate(&schema, query_str, "query.graphql").unwrap();

    let generated =
        generate_extended_refs(&doc, Some("QueryWithNestedQuery".into()), &schema, None);
    assert_extended_references!(&generated);
}

#[test(tokio::test)]
async fn test_enums_with_nested_query() {
    let schema_str = include_str!("testdata/schema_interop.graphql");
    let query_str = include_str!("testdata/enums_from_response_with_nested_query.graphql");

    let schema = Schema::parse_and_validate(schema_str, "schema.graphql").unwrap();
    let doc = ExecutableDocument::parse_and_validate(&schema, query_str, "query.graphql").unwrap();

    let generated =
        generate_extended_refs(&doc, Some("QueryWithNestedQuery".into()), &schema, None);
    assert_extended_references!(&generated);
}