File: fragments_are_finite_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 (149 lines) | stat: -rw-r--r-- 3,574 bytes parent folder | download | duplicates (2)
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
# frozen_string_literal: true
require "spec_helper"

describe GraphQL::StaticValidation::FragmentsAreFinite do
  include StaticValidationHelpers

  let(:query_string) {%|
    query getCheese {
      cheese(id: 1) {
        ... idField
        ... sourceField
        similarCheese(source: SHEEP) {
          ... flavorField
        }
      }
    }

    fragment sourceField on Cheese {
      source,
      ... flavorField
      ... idField
    }
    fragment flavorField on Cheese {
      flavor,
      similarCheese(source: SHEEP) {
        ... on Cheese {
          ... sourceField
        }
      }
    }
    fragment idField on Cheese {
      id
    }
  |}

  it "doesnt allow infinite loops" do
    expected = [
      {
        "message"=>"Fragment sourceField contains an infinite loop",
        "locations"=>[{"line"=>12, "column"=>5}],
        "path"=>["fragment sourceField"],
        "extensions"=>{"code"=>"infiniteLoop", "fragmentName"=>"sourceField"}
      },
      {
        "message"=>"Fragment flavorField contains an infinite loop",
        "locations"=>[{"line"=>17, "column"=>5}],
        "path"=>["fragment flavorField"],
        "extensions"=>{"code"=>"infiniteLoop", "fragmentName"=>"flavorField"}
      }
    ]
    assert_equal(expected, errors)
  end

  describe "undefined spreads inside fragments" do
    let(:query_string) {%|
      {
        cheese(id: 1) { ... frag1 }
      }
      fragment frag1 on Cheese { id, ...frag2 }
    |}

    it "doesn't blow up" do
      assert_equal("Fragment frag2 was used, but not defined", errors.first["message"])
    end
  end

  describe "a duplicate fragment name with a loop" do
    let(:query_string) {%|
      {
        cheese(id: 1) { ... frag1 }
      }
      fragment frag1 on Cheese { id }
      fragment frag1 on Cheese { ...frag1 }
    |}

    it "detects the uniqueness problem" do
      assert_equal 1, errors.length
      assert_equal("Fragment name \"frag1\" must be unique", errors[0]["message"])
    end
  end

  describe "a duplicate operation name with a loop" do
    let(:query_string) {%|
      fragment frag1 on Cheese { ...frag1 }

      query frag1 {
        cheese(id: 1) {
          ... frag1
        }
      }
    |}

    it "detects the loop" do
      assert_equal 1, errors.length
      assert_equal("Fragment frag1 contains an infinite loop", errors[0]["message"])
    end
  end

  describe "several duplicate operation names with a loop" do
    let(:query_string) {%|
      query frag1 {
        cheese(id: 1) {
          id
        }
      }

      fragment frag1 on Cheese { ...frag1 }

      query frag1 {
        cheese(id: 1) {
          ... frag1
        }
      }
    |}

    it "detects the loop" do
      assert_equal 2, errors.length
      assert_equal("Fragment frag1 contains an infinite loop", errors[0]["message"])
      assert_equal("Operation name \"frag1\" must be unique", errors[1]["message"])
    end

    describe "with error limiting" do
      describe("disabled") do
        let(:args) {
          { max_errors: nil }
        }

        it "does not limit the number of errors" do
          assert_equal(error_messages, [
            "Fragment frag1 contains an infinite loop",
            "Operation name \"frag1\" must be unique"
          ])
        end
      end

      describe("enabled") do
        let(:args) {
          { max_errors: 1 }
        }

        it "does limit the number of errors" do
          assert_equal(error_messages, [
            "Fragment frag1 contains an infinite loop",
          ])
        end
      end
    end
  end
end