hive-router 0.3.2

GraphQL router for Federation, part of the Hive platform
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
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
//! A `@requires` reads `price(currency: "EUR")` while the client asks for
//! `price(currency: "GBP")` on the same objects, so one of them gets an internal alias. These
//! tests check what the plan does with it, under interfaces, type conditions and `@include`.
//!
//! All fixtures are composed from real subgraphs:
//! - `requires-alias-entity-interface`: `shop` has `interface Node { price(currency:) }` with
//!   `Cat` and `Dog`, and `pricing` has `Cat.eur` / `Dog.eur` requiring the EUR price.
//! - `requires-alias-interface-object`: the same `shop`, with `pricing` seeing `Node` as an
//!   `@interfaceObject` with `eur` on it. Composed with Apollo, the Guild composer gets this one
//!   wrong, see `composition-guild.md`.
//! - `requires-alias-two-requirements`: the same `shop` and `pricing`, plus `tax` with
//!   `Cat.gbp` requiring the GBP price. Composed with Apollo, for the same reason.
//! - `requires-alias-object-field`: `users` has `User.team(role:)`, `teams` has `Team.name`,
//!   and `perms` has `User.label` requiring `team(role: "admin") { name }`.
//! - `requires-alias-entity-calls`: `catalog` has `things: [Thing]`, `shop` has
//!   `Thing.price(currency:)`, `pricing` has `Thing.eur` requiring the EUR price and `tax` has
//!   `Thing.gbp` requiring the GBP one. `shop` isn't the root here, so every price comes from
//!   an entity call.

use std::error::Error;

use graphql_tools::{
    parser::query::{Definition, OperationDefinition, Selection, SelectionSet, TypeCondition},
    validation::{
        rules::OverlappingFieldsCanBeMerged,
        validate::{validate, ValidationPlan},
    },
};
use serde_json::Value;

use crate::query_planner::{
    tests::testkit::{build_query_plan_with_defaults, init_logger},
    utils::parsing::{parse_operation, parse_schema},
};

const INTERFACE_OBJECT: &str = "fixture/tests/requires-alias-interface-object.supergraph.graphql";
const ENTITY_INTERFACE: &str = "fixture/tests/requires-alias-entity-interface.supergraph.graphql";
const TWO_REQUIREMENTS: &str = "fixture/tests/requires-alias-two-requirements.supergraph.graphql";
const OBJECT_FIELD: &str = "fixture/tests/requires-alias-object-field.supergraph.graphql";
const ENTITY_CALLS: &str = "fixture/tests/requires-alias-entity-calls.supergraph.graphql";

/// What `shop` serves, without the federation parts. Enough to validate its root fetches.
const SHOP_SCHEMA: &str = r#"
  type Query { things: [Node!]! }
  interface Node { id: ID! price(currency: String!): Int! }
  type Cat implements Node { id: ID! price(currency: String!): Int! meow: String! }
  type Dog implements Node { id: ID! price(currency: String!): Int! bark: String! }
"#;

/// `eur` of `Node`s under `$x` and of `Cat`s under `$y`. Under `$x`, `shop` fetches the EUR
/// price as `_internal_qp_alias_0`, next to the client's GBP one.
///
/// The `Cat` representations are sent when `$y` is true, so they have to read a value that's
/// fetched when `$y` is true, not `_internal_qp_alias_0`, which only exists when `$x` is. With
/// `$x: false, $y: true`, `pricing` would get no price.
#[test]
fn requires_alias_is_read_only_where_it_is_fetched() -> Result<(), Box<dyn Error>> {
    init_logger();
    let plan = plan(
        INTERFACE_OBJECT,
        r#"
        query($x: Boolean!, $y: Boolean!) {
          things {
            ... on Node @include(if: $x) { price(currency: "GBP") eur }
            ... on Node @include(if: $y) { ... on Cat { eur } }
          }
        }
        "#,
    )?;

    let shop: Vec<_> = fetches(&plan)
        .into_iter()
        .filter(|f| f.service == "shop")
        .collect();
    let readers: Vec<_> = fetches(&plan)
        .into_iter()
        .filter(|f| f.service == "pricing")
        .collect();
    assert_eq!(
        readers.len(),
        2,
        "expected a pricing fetch under $x and one under $y"
    );

    // The one under `$x` reads the alias fetched under `$x`, which is right.
    let mut wrong = Vec::new();
    for reader in readers {
        let key = read_keys(&reader.requires, "price")
            .into_iter()
            .next()
            .expect("the representation reads a price")
            .1;
        // Any `shop` fetch sent whenever the reader is, with the EUR price under that key.
        let sent_with_reader =
            |conditions: &[String]| conditions.iter().all(|v| reader.conditions.contains(v));
        let fetched_when_sent = shop
            .iter()
            .filter(|fetch| sent_with_reader(&fetch.conditions))
            .flat_map(|fetch| selections_with_key(&fetch.operation, &key))
            .filter(|s| s.arguments == r#"currency: "EUR""#)
            .any(|s| sent_with_reader(&s.includes));
        if !fetched_when_sent {
            wrong.push(format!(
                "representations sent under {:?} read `{key}`, but `shop` fetches it under other conditions",
                reader.conditions
            ));
        }
    }
    let operations: Vec<_> = shop.iter().map(|fetch| fetch.operation.as_str()).collect();
    assert!(wrong.is_empty(), "{wrong:#?}\n{operations:#?}");

    Ok(())
}

/// `shop` gets `price(currency: "GBP")` under `... on Node` and `price(currency: "EUR")`
/// under `... on Cat`, with the same response key. `Node` is an interface, so those two can
/// meet on one object, and GraphQL rejects the operation.
#[test]
fn requires_alias_keeps_shop_operation_valid_across_interface_and_object(
) -> Result<(), Box<dyn Error>> {
    init_logger();
    for query in [
        r#"
        query($x: Boolean!) {
          things {
            ... on Node @include(if: $x) { price(currency: "GBP") eur }
            ... on Cat { eur }
          }
        }
        "#,
        r#"
        query($x: Boolean!, $y: Boolean!) {
          things {
            ... on Node @include(if: $x) { price(currency: "GBP") eur }
            ... on Node @include(if: $y) { ... on Cat { eur } }
          }
        }
        "#,
    ] {
        let plan = plan(INTERFACE_OBJECT, query)?;
        let shop = root_fetch(&plan, "shop");
        let errors = overlapping_fields_errors(SHOP_SCHEMA, &shop.operation);
        assert!(
            errors.is_empty(),
            "`shop` operation doesn't validate: {errors:?}\n{}",
            shop.operation
        );
    }

    Ok(())
}

/// `Cat.eur` and `Dog.eur` both need the EUR price, and the client asks for the GBP one on
/// every `Node`. The two `pricing` fetches get batched into one step for `Cat` and `Dog`, and
/// planning fails in the alias pass with "Expected single input type ... but found multi-type
/// input".
///
/// Both kinds of representations have to read the EUR value, not the client's GBP one.
#[test]
fn requires_alias_reaches_readers_batched_for_many_types() -> Result<(), Box<dyn Error>> {
    init_logger();
    let plan = plan(
        ENTITY_INTERFACE,
        r#"
        {
          things {
            price(currency: "GBP")
            ... on Cat { eur }
            ... on Dog { eur }
          }
        }
        "#,
    )?;

    let shop = root_fetch(&plan, "shop");
    let readers: Vec<_> = fetches(&plan)
        .into_iter()
        .filter(|f| f.service == "pricing")
        .collect();
    assert!(!readers.is_empty(), "expected pricing fetches");

    for reader in readers {
        for (type_name, key) in read_keys(&reader.requires, "price") {
            let values: Vec<_> = selections_with_key(&shop.operation, &key)
                .into_iter()
                .filter(|s| s.applies_to(&type_name))
                .map(|s| s.arguments)
                .collect();
            assert!(
                !values.is_empty() && values.iter().all(|a| a == r#"currency: "EUR""#),
                "`{type_name}` representations read `{key}`, which `shop` fetches as {values:?}:\n{}",
                shop.operation
            );
        }
    }

    Ok(())
}

/// `pricing` needs the EUR price of every `Node` and `tax` the GBP price of `Cat`s, next to
/// the client's own price. `shop` fetches them per type, under different aliases, like
/// `_internal_qp_alias_1: price(currency: "EUR")` for `Cat` and `_internal_qp_alias_2` for
/// `Dog`.
///
/// The `pricing` input is `... on Node { ... on Cat { price } ... on Dog { price } }`. The
/// alias pass has to look inside those fragments, or `price` stays plain and `pricing` gets
/// the client's price instead of the EUR one.
#[test]
fn requires_alias_reaches_readers_inside_type_fragments() -> Result<(), Box<dyn Error>> {
    init_logger();
    for query in [
        r#"{ things { price(currency: "USD") eur ... on Cat { gbp } } }"#,
        r#"{ things { ... on Cat { gbp } eur } }"#,
        r#"{ things { ... on Cat { price(currency: "USD") gbp } eur } }"#,
    ] {
        let plan = plan(TWO_REQUIREMENTS, query)?;
        let wrong = wrong_price_reads(&plan);
        assert!(wrong.is_empty(), "{query}\n{wrong:#?}");
    }

    Ok(())
}

/// Same fixture and readers as above, but under `... on Node @include(if: $x)`. `shop` needs
/// three prices per `Cat`, and they come from separate steps merged into the root one by one.
/// Each merge picks alias names with a fresh counter and only checks the fields right next to
/// it, so after the GBP one went into a fragment as `_internal_qp_alias_0`, the EUR one picks
/// `_internal_qp_alias_0` too. The conflict check catches it, but the merge is a real one by
/// then, so the whole plan fails.
#[test]
fn requires_alias_names_stay_unique_across_merges() -> Result<(), Box<dyn Error>> {
    init_logger();
    for query in [
        r#"query($x: Boolean!) { things { ... on Node @include(if: $x) { price(currency: "USD") eur ... on Cat { gbp } } } }"#,
        r#"query($x: Boolean!) { things { ... on Node @include(if: $x) { price(currency: "USD") ... on Cat { gbp } eur } } }"#,
        r#"query($x: Boolean!) { things { ... on Node @include(if: $x) { ... on Cat { gbp } price(currency: "USD") eur } } }"#,
    ] {
        let plan = plan(TWO_REQUIREMENTS, query).map_err(|e| format!("{query}\n{e}"))?;
        let wrong = wrong_price_reads(&plan);
        assert!(wrong.is_empty(), "{query}\n{wrong:#?}");
    }

    Ok(())
}

/// `perms` needs `team(role: "admin") { name }` and the client asks for
/// `team(role: "user") { name }` on the same `me`. When they end up in one fetch, the admin one
/// gets an alias. When they don't, the admin team still comes back under the plain `team` key,
/// lands on the same `me` as the client's team, and both `teams` fetches write `name` into
/// that one object. The client can get the admin team's name.
///
/// So no two fetches can write the same key at the same place with different arguments.
#[test]
fn requires_alias_keeps_response_keys_apart_across_fetches() -> Result<(), Box<dyn Error>> {
    init_logger();
    for query in [
        // Both in one fetch, the admin team is aliased. This one is right.
        r#"{ me { team(role: "user") { name } label } }"#,
        r#"query($x: Boolean!) { me { team(role: "user") { name } label @include(if: $x) } }"#,
        r#"query($x: Boolean!) { me { team(role: "user") @include(if: $x) { name } label } }"#,
        r#"
        query($x: Boolean!, $y: Boolean!) {
          me {
            ... on User @include(if: $x) { team(role: "user") { name } label }
            ... on User @include(if: $y) { label }
          }
        }
        "#,
    ] {
        let clashes = key_clashes(&plan(OBJECT_FIELD, query)?);
        assert!(clashes.is_empty(), "{query}\n{clashes:#?}");
    }

    Ok(())
}

/// Like the test above, but `me` is selected twice, once of them under `@include`. The admin
/// team is fetched for the `me` without the client's team, and the merge compares it with the
/// fields next to it, which don't include the other `me`. Both `me`s are the same object,
/// so the merge is refused and the admin team lands under the plain `team` key again.
#[test]
fn requires_alias_keeps_response_keys_apart_when_the_parent_repeats() -> Result<(), Box<dyn Error>>
{
    init_logger();
    for query in [
        r#"query($x: Boolean!) { me { team(role: "user") { name } } me @include(if: $x) { label } }"#,
        r#"query($x: Boolean!) { me @include(if: $x) { team(role: "user") { name } } me { label } }"#,
        r#"query($x: Boolean!, $y: Boolean!) { me @include(if: $x) { team(role: "user") { name } } me @include(if: $y) { label } }"#,
        r#"query($x: Boolean!, $y: Boolean!) { me @include(if: $x) { team(role: "user") { name } label } me @include(if: $y) { label } }"#,
        r#"query($x: Boolean!, $y: Boolean!) { ... @include(if: $x) { me { team(role: "user") { name } } } ... @include(if: $y) { me { label } } }"#,
    ] {
        let clashes = key_clashes(&plan(OBJECT_FIELD, query)?);
        assert!(clashes.is_empty(), "{query}\n{clashes:#?}");
    }

    Ok(())
}

/// Every price comes from a `shop` entity call, and entity calls under different conditions
/// are never merged. So when `eur` is under `@include(if: $x)`, its EUR price gets its own
/// call, and nothing compares it with the client's USD price in the other one. Both write
/// `price` into the same `things`, so the client can get the EUR price and `pricing` the USD
/// one. The same goes for two requirements, like `gbp` under `$x` next to `eur`.
#[test]
fn requires_alias_keeps_response_keys_apart_across_entity_calls() -> Result<(), Box<dyn Error>> {
    init_logger();
    for query in [
        // All in one call, EUR and GBP get aliases. This one is right.
        r#"{ things { price(currency: "USD") eur gbp } }"#,
        r#"query($x: Boolean!) { things { price(currency: "USD") eur @include(if: $x) } }"#,
        r#"query($x: Boolean!) { things { price(currency: "USD") @include(if: $x) eur } }"#,
        r#"query($x: Boolean!) { things { price(currency: "USD") ... @include(if: $x) { eur gbp } } }"#,
        r#"query($x: Boolean!) { things { ... @include(if: $x) { price(currency: "USD") } eur gbp } }"#,
        r#"query($x: Boolean!) { things { ... @include(if: $x) { gbp } eur } }"#,
    ] {
        let plan = plan(ENTITY_CALLS, query)?;
        let mut wrong = key_clashes(&plan);
        wrong.extend(wrong_price_reads(&plan));
        assert!(wrong.is_empty(), "{query}\n{wrong:#?}");
    }

    Ok(())
}

fn plan(fixture: &str, query: &str) -> Result<Value, Box<dyn Error>> {
    let plan = build_query_plan_with_defaults(fixture, parse_operation(query))?;
    Ok(serde_json::to_value(&plan)?)
}

/// Readers of `price` that don't get the one they need: `pricing` needs the EUR price, `tax`
/// the GBP one. A `Node` reader reads for `Cat`s and `Dog`s, so the key has to be there for
/// both.
fn wrong_price_reads(plan: &Value) -> Vec<String> {
    let all = fetches(plan);
    let shops: Vec<_> = all.iter().filter(|f| f.service == "shop").collect();
    let mut wrong = Vec::new();
    for reader in &all {
        let expected = match reader.service.as_str() {
            "pricing" => r#"currency: "EUR""#,
            "tax" => r#"currency: "GBP""#,
            _ => continue,
        };
        for (read_type, key) in read_keys(&reader.requires, "price") {
            let objects = match read_type.as_str() {
                "Node" => vec!["Cat".to_string(), "Dog".to_string()],
                _ => vec![read_type],
            };
            for type_name in objects {
                let values: Vec<_> = shops
                    .iter()
                    .flat_map(|shop| selections_with_key(&shop.operation, &key))
                    .filter(|s| s.applies_to(&type_name))
                    .map(|s| s.arguments)
                    .collect();
                if values.is_empty() || values.iter().any(|a| a != expected) {
                    wrong.push(format!(
                        "`{}` reads `{key}` of `{type_name}`, which `shop` fetches as {values:?}",
                        reader.service
                    ));
                }
            }
        }
    }
    wrong
}

/// Fetches that write the same key at the same place, with different arguments.
fn key_clashes(plan: &Value) -> Vec<String> {
    let writes: Vec<_> = fetches(plan).iter().flat_map(writes).collect();
    let mut clashes = Vec::new();
    for (i, a) in writes.iter().enumerate() {
        for b in &writes[i + 1..] {
            if a.location == b.location && a.key == b.key && a.arguments != b.arguments {
                clashes.push(format!(
                    "`{}` at `{}` is `{}({})` from {} and `{}({})` from {}",
                    a.key,
                    a.location.join("."),
                    a.name,
                    a.arguments,
                    a.service,
                    b.name,
                    b.arguments,
                    b.service
                ));
            }
        }
    }
    clashes
}

struct Fetch {
    service: String,
    operation: String,
    requires: Value,
    /// Variables of the `Condition` nodes around the fetch, `!x` for a skip.
    conditions: Vec<String>,
    /// Where the fetched objects go, as response keys, without lists and type conditions.
    paths: Vec<Vec<String>>,
    /// In a batch, the alias of this entity call, like `_e0`.
    entity_alias: Option<String>,
}

/// A `Flatten` or batch path, as response keys.
fn response_keys(path: &Value) -> Vec<String> {
    path.as_array()
        .into_iter()
        .flatten()
        .filter_map(|segment| segment.get("Field").and_then(Value::as_str))
        .map(str::to_string)
        .collect()
}

fn fetches(plan: &Value) -> Vec<Fetch> {
    fn walk(node: &Value, conditions: &[String], path: &[String], out: &mut Vec<Fetch>) {
        match node {
            Value::Object(map) => {
                if map.get("kind").and_then(Value::as_str) == Some("Fetch") {
                    out.push(Fetch {
                        service: map["serviceName"].as_str().unwrap_or_default().to_string(),
                        operation: map["operation"].as_str().unwrap_or_default().to_string(),
                        requires: map.get("requires").cloned().unwrap_or(Value::Null),
                        conditions: conditions.to_vec(),
                        paths: vec![path.to_vec()],
                        entity_alias: None,
                    });
                    return;
                }
                if map.get("kind").and_then(Value::as_str) == Some("Flatten") {
                    walk(&map["node"], conditions, &response_keys(&map["path"]), out);
                    return;
                }
                // Each entity call in a batch has its own representations.
                if map.get("kind").and_then(Value::as_str) == Some("BatchFetch") {
                    let aliases = map["entityBatch"]["aliases"].as_array();
                    for alias in aliases.into_iter().flatten() {
                        out.push(Fetch {
                            service: map["serviceName"].as_str().unwrap_or_default().to_string(),
                            operation: map["operation"].as_str().unwrap_or_default().to_string(),
                            requires: alias["requires"].clone(),
                            conditions: conditions.to_vec(),
                            paths: alias["paths"]
                                .as_array()
                                .into_iter()
                                .flatten()
                                .map(response_keys)
                                .collect(),
                            entity_alias: alias["alias"].as_str().map(str::to_string),
                        });
                    }
                    return;
                }
                if map.get("kind").and_then(Value::as_str) == Some("Condition") {
                    let variable = map["condition"].as_str().unwrap_or_default();
                    for (branch, prefix) in [("ifClause", ""), ("elseClause", "!")] {
                        if let Some(branch) = map.get(branch) {
                            let mut inner = conditions.to_vec();
                            inner.push(format!("{prefix}{variable}"));
                            walk(branch, &inner, path, out);
                        }
                    }
                    return;
                }
                for value in map.values() {
                    walk(value, conditions, path, out);
                }
            }
            Value::Array(items) => items
                .iter()
                .for_each(|item| walk(item, conditions, path, out)),
            _ => {}
        }
    }

    let mut out = Vec::new();
    walk(plan, &[], &[], &mut out);
    out
}

/// The first fetch to `service` that isn't an entity call.
fn root_fetch(plan: &Value, service: &str) -> Fetch {
    fetches(plan)
        .into_iter()
        .find(|f| f.service == service && !f.operation.contains("_entities"))
        .unwrap_or_else(|| panic!("expected a root fetch to {service}"))
}

/// For each type in a representation, the response key it reads `field` from.
/// Looks into nested fragments too, and takes the innermost type.
fn read_keys(requires: &Value, field: &str) -> Vec<(String, String)> {
    fn walk(selections: &Value, type_name: &str, field: &str, out: &mut Vec<(String, String)>) {
        for selection in selections.as_array().into_iter().flatten() {
            if selection["kind"].as_str() == Some("InlineFragment") {
                let inner = selection["typeCondition"].as_str().unwrap_or(type_name);
                walk(&selection["selections"], inner, field, out);
                continue;
            }
            let name = selection["name"].as_str().unwrap_or_default();
            let alias = selection.get("alias").and_then(Value::as_str);
            if alias.unwrap_or(name) == field {
                out.push((type_name.to_string(), name.to_string()));
            }
        }
    }

    let mut out = Vec::new();
    walk(requires, "", field, &mut out);
    out
}

struct Write {
    service: String,
    /// Where the object holding the field sits, as response keys.
    location: Vec<String>,
    key: String,
    name: String,
    arguments: String,
}

/// Every field a fetch puts into the response, and where.
fn writes(fetch: &Fetch) -> Vec<Write> {
    fn collect(
        set: &SelectionSet<'_, String>,
        location: &[String],
        fetch: &Fetch,
        out: &mut Vec<Write>,
    ) {
        for item in &set.items {
            match item {
                Selection::Field(field) => {
                    let key = field.alias.clone().unwrap_or_else(|| field.name.clone());
                    out.push(Write {
                        service: fetch.service.clone(),
                        location: location.to_vec(),
                        key: key.clone(),
                        name: field.name.clone(),
                        arguments: arguments_of(field),
                    });
                    let mut inner = location.to_vec();
                    inner.push(key);
                    collect(&field.selection_set, &inner, fetch, out);
                }
                Selection::InlineFragment(fragment) => {
                    collect(&fragment.selection_set, location, fetch, out)
                }
                Selection::FragmentSpread(_) => {}
            }
        }
    }

    let mut out = Vec::new();
    let document = parse_operation(&fetch.operation);
    for root in operation_selection_sets(&document) {
        let entities_key = fetch.entity_alias.as_deref().unwrap_or("_entities");
        let entities = root.items.iter().find_map(|item| match item {
            Selection::Field(field)
                if field.name == "_entities"
                    && field.alias.as_deref().unwrap_or(&field.name) == entities_key =>
            {
                Some(&field.selection_set)
            }
            _ => None,
        });
        match entities {
            Some(set) => {
                for path in &fetch.paths {
                    collect(set, path, fetch, &mut out);
                }
            }
            None => collect(root, &[], fetch, &mut out),
        }
    }
    out
}

fn arguments_of(field: &graphql_tools::parser::query::Field<'_, String>) -> String {
    field
        .arguments
        .iter()
        .map(|(name, value)| format!("{name}: {value}"))
        .collect::<Vec<_>>()
        .join(", ")
}

/// Operations without variables are printed as a bare `{ ... }`.
fn operation_selection_sets<'a>(
    document: &'a graphql_tools::parser::query::Document<'static, String>,
) -> Vec<&'a SelectionSet<'static, String>> {
    document
        .definitions
        .iter()
        .filter_map(|definition| match definition {
            Definition::Operation(OperationDefinition::Query(query)) => Some(&query.selection_set),
            Definition::Operation(OperationDefinition::SelectionSet(set)) => Some(set),
            _ => None,
        })
        .collect()
}

struct Selected {
    arguments: String,
    /// Type conditions on the way to the field, outermost first.
    types: Vec<String>,
    /// `@include(if:)` variables on the way to the field, including its own.
    includes: Vec<String>,
}

impl Selected {
    /// Whether objects of `type_name` get this field. An interface in the way counts, as the
    /// objects are all `Node`s here.
    fn applies_to(&self, type_name: &str) -> bool {
        self.types.iter().all(|t| t == type_name || t == "Node")
    }
}

/// Fields of the operation with the response key `key`.
fn selections_with_key(operation: &str, key: &str) -> Vec<Selected> {
    fn includes_of(
        directives: &[graphql_tools::parser::query::Directive<'_, String>],
    ) -> Vec<String> {
        directives
            .iter()
            .filter(|d| d.name == "include")
            .flat_map(|d| d.arguments.iter())
            .filter_map(|(_, value)| match value {
                graphql_tools::parser::query::Value::Variable(name) => Some(name.clone()),
                _ => None,
            })
            .collect()
    }

    fn walk(
        set: &SelectionSet<'_, String>,
        key: &str,
        types: &[String],
        includes: &[String],
        out: &mut Vec<Selected>,
    ) {
        for item in &set.items {
            match item {
                Selection::Field(field) => {
                    let mut includes = includes.to_vec();
                    includes.extend(includes_of(&field.directives));
                    if field.alias.as_deref().unwrap_or(&field.name) == key {
                        out.push(Selected {
                            arguments: field
                                .arguments
                                .iter()
                                .map(|(name, value)| format!("{name}: {value}"))
                                .collect::<Vec<_>>()
                                .join(", "),
                            types: types.to_vec(),
                            includes: includes.clone(),
                        });
                    }
                    walk(&field.selection_set, key, &[], &includes, out);
                }
                Selection::InlineFragment(fragment) => {
                    let mut types = types.to_vec();
                    if let Some(TypeCondition::On(name)) = &fragment.type_condition {
                        types.push(name.clone());
                    }
                    let mut includes = includes.to_vec();
                    includes.extend(includes_of(&fragment.directives));
                    walk(&fragment.selection_set, key, &types, &includes, out);
                }
                Selection::FragmentSpread(_) => {}
            }
        }
    }

    let document = parse_operation(operation);
    let mut out = Vec::new();
    for definition in &document.definitions {
        // Operations without variables are printed as a bare `{ ... }`.
        let selection_set = match definition {
            Definition::Operation(OperationDefinition::Query(query)) => &query.selection_set,
            Definition::Operation(OperationDefinition::SelectionSet(set)) => set,
            _ => continue,
        };
        walk(selection_set, key, &[], &[], &mut out);
    }
    out
}

fn overlapping_fields_errors(schema: &str, operation: &str) -> Vec<String> {
    let schema = parse_schema(schema);
    let operation = parse_operation(operation);
    let plan = ValidationPlan::from(vec![Box::new(OverlappingFieldsCanBeMerged::new())]);
    validate(&schema, &operation, &plan)
        .into_iter()
        .map(|error| error.message)
        .collect()
}