File: lookahead_spec.rb

package info (click to toggle)
ruby-graphql 2.2.17-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,584 kB
  • sloc: ruby: 67,505; ansic: 1,753; yacc: 831; javascript: 331; makefile: 6
file content (825 lines) | stat: -rw-r--r-- 26,140 bytes parent folder | download
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
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
# frozen_string_literal: true
require "spec_helper"

describe GraphQL::Execution::Lookahead do
  module LookaheadTest
    DATA = [
      OpenStruct.new(name: "Cardinal", is_waterfowl: false, similar_species_names: ["Scarlet Tanager"], genus: OpenStruct.new(latin_name: "Piranga")),
      OpenStruct.new(name: "Scarlet Tanager", is_waterfowl: false, similar_species_names: ["Cardinal"], genus: OpenStruct.new(latin_name: "Cardinalis")),
      OpenStruct.new(name: "Great Egret", is_waterfowl: false, similar_species_names: ["Great Blue Heron"], genus: OpenStruct.new(latin_name: "Ardea")),
      OpenStruct.new(name: "Great Blue Heron", is_waterfowl: true, similar_species_names: ["Great Egret"], genus: OpenStruct.new(latin_name: "Ardea")),
    ]

    def DATA.find_by_name(name)
      DATA.find { |b| b.name == name }
    end

    module Node
      include GraphQL::Schema::Interface
      field :id, ID, null: false
    end

    class BirdGenus < GraphQL::Schema::Object
      implements Node
      field :name, String, null: false
      field :latin_name, String, null: false
      field :id, ID, null: false, method: :latin_name
    end

    class BirdSpecies < GraphQL::Schema::Object
      implements Node
      field :name, String, null: false
      field :id, ID, null: false, method: :name
      field :is_waterfowl, Boolean, null: false
      field :similar_species, [BirdSpecies], null: false

      def similar_species
        object.similar_species_names.map { |n| DATA.find_by_name(n) }
      end

      field :genus, BirdGenus, null: false,
        extras: [:lookahead]

      def genus(lookahead:)
        if lookahead.selects?(:latin_name)
          context[:lookahead_latin_name] += 1
        end
        object.genus
      end
    end

    class PlantSpecies < GraphQL::Schema::Object
      implements Node
      field :name, String, null: false
      field :id, ID, null: false, method: :name
      field :is_edible, Boolean, null: false
    end

    class Species < GraphQL::Schema::Union
      possible_types BirdSpecies, PlantSpecies
    end

    class Query < GraphQL::Schema::Object
      field :find_bird_species, BirdSpecies do
        argument :by_name, String
      end

      def find_bird_species(by_name:)
        DATA.find_by_name(by_name)
      end

      field :node, Node do
        argument :id, ID
      end

      def node(id:)
        if (node = DATA.find_by_name(id))
          node
        else
          DATA.map { |d| d.genus }.select { |g| g.name == id }
        end
      end

      field :species, Species do
        argument :id, ID
      end

      def species(id:)
        DATA.find_by_name(id)
      end
    end

    module LookaheadInstrumenter
      def execute_query(query:)
        query.context[:root_lookahead_selections] = query.lookahead.selections
        super
      end
    end

    class Schema < GraphQL::Schema
      query(Query)
      trace_with LookaheadInstrumenter
    end

    class AlwaysVisibleSchema < Schema
      use GraphQL::Schema::AlwaysVisible
    end
  end

  describe "looking ahead" do
    let(:document) {
      GraphQL.parse <<-GRAPHQL
      query($name: String!){
        findBirdSpecies(byName: $name) {
          name
          similarSpecies {
            likesWater: isWaterfowl
          }
        }
        t: __typename
      }
      GRAPHQL
    }
    let(:schema) { LookaheadTest::Schema }
    let(:query) {
      GraphQL::Query.new(schema, document: document, variables: { name: "Cardinal" })
    }

    it "has a good test setup" do
      res = query.result
      assert_equal [false], res["data"]["findBirdSpecies"]["similarSpecies"].map { |s| s["likesWater"] }
    end

    it "can detect fields on objects with symbol or string" do
      lookahead = query.lookahead.selection("findBirdSpecies")
      assert_equal true, lookahead.selects?("similarSpecies")
      assert_equal true, lookahead.selects?(:similar_species)
      assert_equal false, lookahead.selects?("isWaterfowl")
      assert_equal false, lookahead.selects?(:is_waterfowl)
    end

    it "detects by name, not by alias" do
      assert_equal true, query.lookahead.selects?("__typename")
    end

    describe "with a NullWarden" do
      let(:schema) { LookaheadTest::AlwaysVisibleSchema }

      it "works" do
        lookahead = query.lookahead.selection("findBirdSpecies")
        assert_equal true, lookahead.selects?("similarSpecies")
        assert_equal true, lookahead.selects?(:similar_species)
        assert_equal false, lookahead.selects?("isWaterfowl")
        assert_equal false, lookahead.selects?(:is_waterfowl)
      end
    end

    describe "on unions" do
      let(:document) {
        GraphQL.parse <<-GRAPHQL
        {
          species(id: "Cardinal") {
            ... on BirdSpecies {
              name
              isWaterfowl
            }
            ... on PlantSpecies {
              name
              isEdible
            }
          }
        }
        GRAPHQL
      }

      it "works" do
        lookahead = query.lookahead.selection(:species)
        assert lookahead.selects?(:name)
        assert_equal [:name, :is_waterfowl, :name, :is_edible], lookahead.selections.map(&:name)
      end

      it "works with different selected types" do
        lookahead = query.lookahead.selection(:species)
        # Both have `name`
        assert lookahead.selects?(:name, selected_type: LookaheadTest::BirdSpecies)
        assert lookahead.selects?(:name, selected_type: LookaheadTest::PlantSpecies)
        # Only birds have `isWaterfowl`
        assert lookahead.selects?(:is_waterfowl, selected_type: LookaheadTest::BirdSpecies)
        refute lookahead.selects?(:is_waterfowl, selected_type: LookaheadTest::PlantSpecies)
        # Only plants have `isEdible`
        refute lookahead.selects?(:is_edible, selected_type: LookaheadTest::BirdSpecies)
        assert lookahead.selects?(:is_edible, selected_type: LookaheadTest::PlantSpecies)
      end
    end

    describe "fields on interfaces" do
      let(:document) {
        GraphQL.parse <<-GRAPHQL
        query {
          node(id: "Cardinal") {
            id
            ... on BirdSpecies {
              name
            }
            ...Other
          }
        }
        fragment Other on BirdGenus {
          latinName
        }
        GRAPHQL
      }

      it "finds fields on object types and interface types" do
        node_lookahead = query.lookahead.selection("node")
        assert_equal [:id, :name, :latin_name], node_lookahead.selections.map(&:name)
      end
    end

    describe "inspect" do
      it "works for root lookaheads" do
        assert_includes query.lookahead.inspect, "#<GraphQL::Execution::Lookahead @root_type="
      end

      it "works for field lookaheads" do
        assert_includes query.lookahead.selection(:find_bird_species).inspect, "#<GraphQL::Execution::Lookahead @field="
      end
    end

    describe "constraints by arguments" do
      let(:lookahead) do
        query.lookahead
      end

      it "is true without constraints" do
        assert_equal true, lookahead.selects?("findBirdSpecies")
      end

      it "is true when all given constraints are satisfied" do
        assert_equal true, lookahead.selects?(:find_bird_species, arguments: { by_name: "Cardinal" })
        assert_equal true, lookahead.selects?("findBirdSpecies", arguments: { "byName" => "Cardinal" })
      end

      it "is true when no constraints are given" do
        assert_equal true, lookahead.selects?(:find_bird_species, arguments: {})
        assert_equal true, lookahead.selects?("__typename", arguments: {})
      end

      it "is false when some given constraints aren't satisfied" do
        assert_equal false, lookahead.selects?(:find_bird_species, arguments: { by_name: "Chickadee" })
        assert_equal false, lookahead.selects?(:find_bird_species, arguments: { by_name: "Cardinal", other: "Nonsense" })
      end

      describe "with literal values" do
        let(:document) {
          GraphQL.parse <<-GRAPHQL
          {
            findBirdSpecies(byName: "Great Blue Heron") {
              isWaterfowl
            }
          }
          GRAPHQL
        }

        it "works" do
          assert_equal true, lookahead.selects?(:find_bird_species, arguments: { by_name: "Great Blue Heron" })
        end
      end
    end

    it "can do a chained lookahead" do
      next_lookahead = query.lookahead.selection(:find_bird_species, arguments: { by_name: "Cardinal" })
      assert_equal true, next_lookahead.selected?
      nested_selection = next_lookahead.selection(:similar_species).selection(:is_waterfowl, arguments: {})
      assert_equal true, nested_selection.selected?
      assert_equal false, next_lookahead.selection(:similar_species).selection(:name).selected?
    end

    it "can detect fields on lists with symbol or string" do
      assert_equal true, query.lookahead.selection(:find_bird_species).selection(:similar_species).selection(:is_waterfowl).selected?
      assert_equal true, query.lookahead.selection("findBirdSpecies").selection("similarSpecies").selection("isWaterfowl").selected?
    end

    describe "merging branches and fragments" do
      let(:document) {
        GraphQL.parse <<-GRAPHQL
        {
          findBirdSpecies(name: "Cardinal") {
            similarSpecies {
              __typename
            }
          }
          ...F
          ... {
            findBirdSpecies(name: "Cardinal") {
              similarSpecies {
                isWaterfowl
              }
            }
          }
        }

        fragment F on Query {
          findBirdSpecies(name: "Cardinal") {
            similarSpecies {
              name
            }
          }
        }
        GRAPHQL
      }

      it "finds selections using merging" do
        merged_lookahead = query.lookahead.selection(:find_bird_species).selection(:similar_species)
        assert merged_lookahead.selects?(:__typename)
        assert merged_lookahead.selects?(:is_waterfowl)
        assert merged_lookahead.selects?(:name)
      end
    end
  end

  describe "in queries" do
    it "can be an extra" do
      query_str = <<-GRAPHQL
      {
        cardinal: findBirdSpecies(byName: "Cardinal") {
          genus { __typename }
        }
        scarletTanager: findBirdSpecies(byName: "Scarlet Tanager") {
          genus { latinName }
        }
        greatBlueHeron: findBirdSpecies(byName: "Great Blue Heron") {
          genus { latinName }
        }
      }
      GRAPHQL
      context = {lookahead_latin_name: 0}
      res = LookaheadTest::Schema.execute(query_str, context: context)
      refute res.key?("errors")
      assert_equal 2, context[:lookahead_latin_name]
      assert_equal [:find_bird_species], context[:root_lookahead_selections].map(&:name).uniq
      assert_equal(
        [{ by_name: "Cardinal" }, { by_name: "Scarlet Tanager" }, { by_name: "Great Blue Heron" }],
        context[:root_lookahead_selections].map(&:arguments)
      )
    end

    it "works for invalid queries" do
      context = {lookahead_latin_name: 0}
      res = LookaheadTest::Schema.execute("{ doesNotExist }", context: context)
      assert res.key?("errors")
      assert_equal 0, context[:lookahead_latin_name]
    end
  end

  describe '#selections' do
    let(:document) {
      GraphQL.parse <<-GRAPHQL
        query {
          findBirdSpecies(byName: "Laughing Gull") {
            name
            similarSpecies {
              likesWater: isWaterfowl
            }
          }
        }
      GRAPHQL
    }

    def query(doc = document)
      GraphQL::Query.new(LookaheadTest::Schema, document: doc)
    end

    it "provides a list of all selections" do
      ast_node = document.definitions.first.selections.first
      field = LookaheadTest::Query.fields["findBirdSpecies"]
      lookahead = GraphQL::Execution::Lookahead.new(query: query, ast_nodes: [ast_node], field: field)
      assert_equal [:name, :similar_species], lookahead.selections.map(&:name)
    end

    it "filters outs selections which do not match arguments" do
      ast_node = document.definitions.first
      lookahead = GraphQL::Execution::Lookahead.new(query: query, ast_nodes: [ast_node], root_type: LookaheadTest::Query)
      arguments = { by_name: "Cardinal" }

      assert_equal lookahead.selections(arguments: arguments).map(&:name), []
    end

    it "includes selections which match arguments" do
      ast_node = document.definitions.first
      lookahead = GraphQL::Execution::Lookahead.new(query: query, ast_nodes: [ast_node], root_type: LookaheadTest::Query)
      arguments = { by_name: "Laughing Gull" }

      assert_equal lookahead.selections(arguments: arguments).map(&:name), [:find_bird_species]
    end

    it 'handles duplicate selections across fragments' do
      doc = GraphQL.parse <<-GRAPHQL
        query {
          ... on Query {
            ...MoreFields
          }
        }

        fragment MoreFields on Query {
          findBirdSpecies(byName: "Laughing Gull") {
            name
          }
          findBirdSpecies(byName: "Laughing Gull") {
            ...EvenMoreFields
          }
        }

        fragment EvenMoreFields on BirdSpecies {
          similarSpecies {
            likesWater: isWaterfowl
          }
        }
      GRAPHQL

      lookahead = query(doc).lookahead

      root_selections = lookahead.selections
      assert_equal [:find_bird_species], root_selections.map(&:name), "Selections are merged"
      assert_equal 2, root_selections.first.ast_nodes.size, "It represents both nodes"

      assert_equal [:name, :similar_species], root_selections.first.selections.map(&:name), "Subselections are merged"
    end

    it "avoids merging selections for same field name on distinct types" do
      query = GraphQL::Query.new(LookaheadTest::Schema, <<-GRAPHQL)
        query {
          node(id: "Cardinal") {
            ... on BirdSpecies {
              name
            }
            ... on BirdGenus {
              name
            }
            id
          }
        }
      GRAPHQL

      node_lookahead = query.lookahead.selection("node")
      assert_equal(
        [[LookaheadTest::Node, :id], [LookaheadTest::BirdSpecies, :name], [LookaheadTest::BirdGenus, :name]],
        node_lookahead.selections.map { |s| [s.owner_type, s.name] }
      )
    end

    it "works for missing selections" do
      ast_node = document.definitions.first.selections.first
      field = LookaheadTest::Query.fields["findBirdSpecies"]
      lookahead = GraphQL::Execution::Lookahead.new(query: query, ast_nodes: [ast_node], field: field)
      null_lookahead = lookahead.selection(:genus)
      # This is an implementation detail, but I want to make sure the test is set up right
      assert_instance_of GraphQL::Execution::Lookahead::NullLookahead, null_lookahead
      assert_equal [], null_lookahead.selections
    end

    it "excludes fields skipped by directives" do
      document = GraphQL.parse <<-GRAPHQL
        query($skipName: Boolean!, $includeGenus: Boolean!){
          findBirdSpecies(byName: "Cardinal") {
            id
            name @skip(if: $skipName)
            genus @include(if: $includeGenus)
          }
        }
      GRAPHQL
      query = GraphQL::Query.new(LookaheadTest::Schema, document: document,
        variables: { skipName: false, includeGenus: true })
      lookahead = query.lookahead.selection("findBirdSpecies")
      assert_equal [:id, :name, :genus], lookahead.selections.map(&:name)
      assert_equal true, lookahead.selects?(:name)

      query = GraphQL::Query.new(LookaheadTest::Schema, document: document,
        variables: { skipName: true, includeGenus: false })
      lookahead = query.lookahead.selection("findBirdSpecies")
      assert_equal [:id], lookahead.selections.map(&:name)
      assert_equal false, lookahead.selects?(:name)
    end
  end

  def assert_selection_exists(selection)
    assert GraphQL::Execution::Lookahead::NULL_LOOKAHEAD != selection
  end

  def assert_selection_is_null(selection)
    assert_equal GraphQL::Execution::Lookahead::NULL_LOOKAHEAD, selection
  end

  describe "#selection" do
    let(:document) {
      GraphQL.parse <<-GRAPHQL
        query {
          findBirdSpecies(byName: "Laughing Gull") {
            name
            similarSpecies {
              likesWater: isWaterfowl
            }
          }
        }
      GRAPHQL
    }

    def query(doc = document)
      GraphQL::Query.new(LookaheadTest::Schema, document: doc)
    end

    it "returns selection by field name" do
      ast_node = document.definitions.first.selections.first
      field = LookaheadTest::Query.fields["findBirdSpecies"]
      lookahead = GraphQL::Execution::Lookahead.new(query: query, ast_nodes: [ast_node], field: field)
      assert_selection_exists lookahead.selection("similarSpecies")
    end

    describe "when same field is selected twice" do
      let(:document) {
        GraphQL.parse <<-GRAPHQL
          query {
            gull: findBirdSpecies(byName: "Laughing Gull") {
              name
            }

            tanager: findBirdSpecies(byName: "Scarlet Tanager") {
              name
            }
          }
        GRAPHQL
      }

      let(:graphql_query) do
        GraphQL::Query.new(LookaheadTest::Schema, document: document)
      end

      it "returns lookahead with two ast_nodes" do
        assert_equal 2, graphql_query.lookahead.selection("findBirdSpecies").ast_nodes.length
      end
    end

    describe "when query has alias" do
      let(:document) {
        GraphQL.parse <<-GRAPHQL
          query {
            findBirdSpecies(byName: "Laughing Gull") {
              name
              similar: similarSpecies {
                likesWater: isWaterfowl
              }
            }
          }
        GRAPHQL
      }

      let(:graphql_query) do
        GraphQL::Query.new(LookaheadTest::Schema, document: document)
      end

      let(:species_lookahead) do
        graphql_query.lookahead.selection("findBirdSpecies")
      end

      it "returns selection when field name is passed" do
        assert_selection_exists species_lookahead.selection("similarSpecies")
      end

      it "returns null when alias name is passed" do
        assert_selection_is_null species_lookahead.selection("similar")
      end

      describe "when alias has arguments" do
        let(:document) {
          GraphQL.parse <<-GRAPHQL
            query {
              gull: findBirdSpecies(byName: "Laughing Gull") {
                name
              }
            }
          GRAPHQL
        }

        it "returns selection when field name is passed" do
          assert_selection_exists graphql_query.lookahead.selection("findBirdSpecies")
        end

        it "returns null when alias name is passed" do
          assert_selection_is_null graphql_query.lookahead.selection("gull")
        end

        describe "when same field is selected twice" do
          let(:document) {
            GraphQL.parse <<-GRAPHQL
              query {
                gull: findBirdSpecies(byName: "Laughing Gull") {
                  name
                }

                tanager: findBirdSpecies(byName: "Scarlet Tanager") {
                  name
                }
              }
            GRAPHQL
          }

          it "returns null when alias name is passed" do
            assert_selection_is_null graphql_query.lookahead.selection("gull")
            assert_selection_is_null graphql_query.lookahead.selection("tanager")
          end
        end
      end
    end
  end

  describe "#alias_selection" do
    let(:document) {
      GraphQL.parse <<-GRAPHQL
        query {
          findBirdSpecies(byName: "Laughing Gull") {
            name
            similar: similarSpecies {
              likesWater: isWaterfowl
            }
          }
        }
      GRAPHQL
    }

    def query(doc = document)
      GraphQL::Query.new(LookaheadTest::Schema, document: doc)
    end

    let(:graphql_query) do
      GraphQL::Query.new(LookaheadTest::Schema, document: document)
    end

    let(:species_lookahead) do
      graphql_query.lookahead.selection("findBirdSpecies")
    end

    describe "when alias name is passed" do
      it "returns selection" do
        assert_selection_exists species_lookahead.alias_selection("similar")
      end

      it "returns true from selects_alias?" do
        assert true, species_lookahead.selects_alias?("similar")
      end

      describe "when the aliased field is deeply nested" do
        it "not finds the deeply-nested alias" do
          assert_equal [:name, :similar_species], species_lookahead.selections.map(&:name)
          assert_equal false, species_lookahead.selects_alias?("likesWater")
        end
      end
    end

    describe "when the same field is executed with the same arguments but different aliases" do
      let(:document) {
        GraphQL.parse <<-GRAPHQL
          query {
            egret: findBirdSpecies(byName: "Great Egret") {
              isWaterfowl
            }
            otherEgret: findBirdSpecies(byName: "Great Egret") {
              name
            }
            findBirdSpecies(byName: "Great Egret") {
              __typename
            }
          }
        GRAPHQL
      }

      it "distinguishes between the aliased fields" do
        lookahead = query.lookahead
        assert_equal [:is_waterfowl], lookahead.alias_selection("egret").selections.map(&:name)
        assert_equal [:name], lookahead.alias_selection("otherEgret").selections.map(&:name)
        assert_equal [], lookahead.alias_selection("findBirdSpecies").selections.map(&:name)
      end

      it "filters aliased fields by arguments" do
        lookahead = query.lookahead
        # No `arguments:` performs no filtering
        assert_equal [:is_waterfowl], lookahead.alias_selection("egret").selections.map(&:name)
        # Matching arguments filters to the expected field:
        assert_equal [:is_waterfowl], lookahead.alias_selection("egret", arguments: {by_name: "Great Egret"}).selections.map(&:name)
        # Empty `arguments:` matches nothing:
        assert_equal [], lookahead.alias_selection("egret", arguments: {}).selections.map(&:name)
        # Mismatching `arguments:` filters to nothing:
        assert_equal [], lookahead.alias_selection("egret", arguments: {by_name: "Macaw"}).selections.map(&:name)
      end
    end

    describe "when field name is passed" do
      it "returns null_lookahead" do
        assert_selection_is_null species_lookahead.alias_selection("similarSpecies")
      end

      it "returns false from selects_alias?" do
        assert_equal false, species_lookahead.selects_alias?("similarSpecies")
      end
    end

    describe "when alias is inside fragment" do
      let(:document) {
        GraphQL.parse <<-GRAPHQL
          fragment BirdSpeciesFragment on BirdSpecies {
            name
            similar: similarSpecies {
              likesWater: isWaterfowl
            }
          }

          query {
            findBirdSpecies(byName: "Laughing Gull") {
              ...BirdSpeciesFragment
            }
          }
        GRAPHQL
      }

      it "returns selection" do
        assert_selection_exists species_lookahead.alias_selection("similar")
      end

      it "returns true from selects_alias?" do
        assert true, species_lookahead.selects_alias?("similar")
      end

      describe "when fragment name is wrong" do
        let(:document) {
          GraphQL.parse <<-GRAPHQL
            query {
              findBirdSpecies(byName: "Laughing Gull") {
                ...WrongFragment
              }
            }
          GRAPHQL
        }

        it "raises error" do
          assert_raises(RuntimeError) {
            species_lookahead.selects_alias?("similar")
          }
        end
      end
    end

    describe "when alias is inside inline fragment" do
      let(:document) {
        GraphQL.parse <<-GRAPHQL
          query {
            findBirdSpecies(byName: "Laughing Gull") {
              ...on BirdSpecies {
                name
                similar: similarSpecies {
                  likesWater: isWaterfowl
                }
              }
            }
          }
        GRAPHQL
      }

      it "returns selection" do
        assert_selection_exists species_lookahead.alias_selection("similar")
      end

      it "returns true from selects_alias?" do
        assert true, species_lookahead.selects_alias?("similar")
      end
    end

    describe "when alias has arguments" do
      let(:document) {
        GraphQL.parse <<-GRAPHQL
          query {
            gull: findBirdSpecies(byName: "Laughing Gull") {
              name
            }
          }
        GRAPHQL
      }

      it "returns selection" do
        assert_selection_exists graphql_query.lookahead.alias_selection("gull")
      end

      it "returns true from selects_alias?" do
        assert true, graphql_query.lookahead.selects_alias?("gull")
      end

      describe "when same field is selected twice" do
        let(:document) {
          GraphQL.parse <<-GRAPHQL
            query {
              gull: findBirdSpecies(byName: "Laughing Gull") {
                name
              }

              tanager: findBirdSpecies(byName: "Scarlet Tanager") {
                name
              }
            }
          GRAPHQL
        }

        it "returns selection when alias name is passed" do
          graphql_query.lookahead.alias_selection("gull", arguments: { by_name: "Laughing Gull" }).tap do |selection|
            assert_selection_exists selection
            assert_equal({ by_name: "Laughing Gull" }, selection.arguments)
            assert_equal 1, selection.ast_nodes.length
          end

          graphql_query.lookahead.alias_selection("tanager", arguments: { by_name: "Scarlet Tanager" }).tap do |selection|
            assert_selection_exists selection
            assert_equal({ by_name: "Scarlet Tanager" }, selection.arguments)
            assert_equal 1, selection.ast_nodes.length
          end
        end
      end
    end
  end
end