File: variables_are_used_and_defined_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 (143 lines) | stat: -rw-r--r-- 4,115 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
# frozen_string_literal: true
require "spec_helper"

describe GraphQL::StaticValidation::VariablesAreUsedAndDefined do
  include StaticValidationHelpers

  let(:query_string) {'
    query getCheese(
      $usedVar: Int!,
      $usedInnerVar: [DairyAnimal!]!,
      $usedInlineFragmentVar: Int!,
      $usedFragmentVar: Int!,
      $notUsedVar: Int!,
    ) {
      c1: cheese(id: $usedVar) {
        __typename
      }
      ... on Query {
        c2: cheese(id: $usedInlineFragmentVar) {
          similarCheese(source: $usedInnerVar) { __typename }
        }

      }

      c3: cheese(id: $undefinedVar) { __typename }

      ... outerCheeseFields
    }

    fragment outerCheeseFields on Query {
      ... innerCheeseFields
    }

    fragment innerCheeseFields on Query {
      c4: cheese(id: $undefinedFragmentVar) { __typename }
      c5: cheese(id: $usedFragmentVar) { __typename }
    }
  '}

  describe "variables which are used-but-not-defined or defined-but-not-used" do
    it "finds the variables" do
      expected = [
        {
          "message"=>"Variable $notUsedVar is declared by getCheese but not used",
          "locations"=>[{"line"=>2, "column"=>5}],
          "path"=>["query getCheese"],
          "extensions"=>{"code"=>"variableNotUsed", "variableName"=>"notUsedVar"}
        },
        {
          "message"=>"Variable $undefinedVar is used by getCheese but not declared",
          "locations"=>[{"line"=>19, "column"=>22}],
          "path"=>["query getCheese", "c3", "id"],
          "extensions"=>{"code"=>"variableNotDefined", "variableName"=>"undefinedVar"}
        },
        {
          "message"=>"Variable $undefinedFragmentVar is used by innerCheeseFields but not declared",
          "locations"=>[{"line"=>29, "column"=>22}],
          "path"=>["fragment innerCheeseFields", "c4", "id"],
          "extensions"=>{"code"=>"variableNotDefined", "variableName"=>"undefinedFragmentVar"}
        },
      ]

      assert_equal(expected, errors)
    end

    describe "with an anonymous query" do
      let(:query_string) do
        <<-GRAPHQL
        query($notUsedVar: Int!) {
          c1: cheese(id: $undeclared) {
            __typename
          }
        }
        GRAPHQL
      end

      it "shows 'anonymous query' in the message" do
        expected = [
          {
            "message"=>"Variable $notUsedVar is declared by anonymous query but not used",
            "locations"=>[{"line"=>1, "column"=>9}],
            "path"=>["query"],
            "extensions"=>{"code"=>"variableNotUsed", "variableName"=>"notUsedVar"}
          },
          {
            "message"=>"Variable $undeclared is used by anonymous query but not declared",
            "locations"=>[{"line"=>2, "column"=>26}],
            "path"=>["query", "c1", "id"],
            "extensions"=>{"code"=>"variableNotDefined", "variableName"=>"undeclared"}
          }
        ]
        assert_equal(expected, errors)
      end
    end
  end

  describe "usages in directives on fragment spreads" do
    let(:query_string) {
      <<-GRAPHQL
      query($f: Boolean!){
        ...F @include(if: $f)
      }
      fragment F on Query {
        __typename
      }
      GRAPHQL
    }

    it "finds usages" do
      assert_equal([], errors)
    end
  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.length, 3)
        assert_equal(error_messages, [
          "Variable $notUsedVar is declared by getCheese but not used",
          "Variable $undefinedVar is used by getCheese but not declared",
          "Variable $undefinedFragmentVar is used by innerCheeseFields but not declared"
        ])
      end
    end

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

      it "does limit the number of errors" do
        assert_equal(error_messages.length, 1)
        assert_equal(error_messages, [
          "Variable $notUsedVar is declared by getCheese but not used"
        ])
      end
    end
  end
end