File: operation_names_are_valid_spec.rb

package info (click to toggle)
ruby-graphql 2.5.19-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 13,868 kB
  • sloc: ruby: 80,420; ansic: 1,808; yacc: 845; javascript: 480; makefile: 6
file content (130 lines) | stat: -rw-r--r-- 3,154 bytes parent folder | download | duplicates (3)
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
# frozen_string_literal: true
require "spec_helper"

describe GraphQL::StaticValidation::OperationNamesAreValid do
  include StaticValidationHelpers

  describe "when there are multiple operations" do
    let(:query_string) { <<-GRAPHQL
    query getCheese {
      cheese(id: 1) { flavor }
    }

    {
      cheese(id: 2) { flavor }
    }

    {
      cheese(id: 3) { flavor }
    }
    GRAPHQL
    }

    it "must have operation names" do
      assert_equal 1, errors.length
      requires_name_error = {
        "message"=>"Operation name is required when multiple operations are present",
        "locations"=>[{"line"=>5, "column"=>5}, {"line"=>9, "column"=>5}],
        "path"=>[],
        "extensions"=>{"code"=>"uniquelyNamedOperations"}
      }
      assert_includes(errors, requires_name_error)
    end
  end

  describe "when there are only unnamed operations" do
    let(:query_string) { <<-GRAPHQL
    {
      cheese(id: 2) { flavor }
    }

    {
      cheese(id: 3) { flavor }
    }
    GRAPHQL
    }

    it "requires names" do
      assert_equal 1, errors.length
      requires_name_error = {
        "message"=>"Operation name is required when multiple operations are present",
        "locations"=>[{"line"=>1, "column"=>5}, {"line"=>5, "column"=>5}],
        "path"=>[],
        "extensions"=>{"code"=>"uniquelyNamedOperations"}
      }
      assert_includes(errors, requires_name_error)
    end
  end

  describe "when multiple operations have names" do
    let(:query_string) { <<-GRAPHQL
    query getCheese {
      cheese(id: 1) { flavor }
    }

    query getCheese {
      cheese(id: 2) { flavor }
    }
    GRAPHQL
    }

    it "must be unique" do
      assert_equal 1, errors.length
      name_uniqueness_error = {
        "message"=>'Operation name "getCheese" must be unique',
        "locations"=>[{"line"=>1, "column"=>5}, {"line"=>5, "column"=>5}],
        "path"=>[],
        "extensions"=>{"code"=>"uniquelyNamedOperations", "operationName"=>"getCheese"}
      }
      assert_includes(errors, name_uniqueness_error)
    end
  end

  describe "with error limiting" do
    let(:query_string) { <<-GRAPHQL
    query getCheese {
      cheese(id: 1) { flavor }
    }
    query getCheese {
      cheese(id: 2) { flavor }
    }
    query getCheeses{
      searchDairy(product: [{ source: COW }]) {
        __typename
      }
    }
    query getCheeses{
      searchDairy(product: [{ source: COW }]) {
        __typename
      }
    }
    GRAPHQL
    }
    describe("disabled") do
      let(:args) {
        { max_errors: nil }
      }

      it "does not limit the number of errors" do
        assert_equal(error_messages.length, 2)
        assert_equal(error_messages, [
          "Operation name \"getCheese\" must be unique",
          "Operation name \"getCheeses\" 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.length, 1)
        assert_equal(error_messages, [
          "Operation name \"getCheese\" must be unique",
        ])
      end
    end
  end
end