summaryrefslogtreecommitdiff
path: root/spec/graphql/types/global_id_type_spec.rb
blob: 8df92c818fc276b696f090cc78b8d901eb1bb7a9 (plain)
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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Types::GlobalIDType do
  include GraphqlHelpers
  include GlobalIDDeprecationHelpers

  let_it_be(:project) { create(:project) }

  let(:gid) { project.to_global_id }

  it 'is has the correct name' do
    expect(described_class.graphql_name).to eq('GlobalID')
  end

  describe '.coerce_result' do
    it 'can coerce results' do
      expect(described_class.coerce_isolated_result(gid)).to eq(gid.to_s)
    end

    it 'rejects integer IDs' do
      expect { described_class.coerce_isolated_result(project.id) }
        .to raise_error(ArgumentError)
    end

    it 'rejects strings' do
      expect { described_class.coerce_isolated_result('not a GID') }
        .to raise_error(ArgumentError)
    end
  end

  describe '.coerce_input' do
    it 'can coerce valid input' do
      coerced = described_class.coerce_isolated_input(gid.to_s)

      expect(coerced).to eq(gid)
    end

    it 'handles all valid application GIDs' do
      expect { described_class.coerce_isolated_input(build_stubbed(:user).to_global_id.to_s) }
        .not_to raise_error
    end

    it 'rejects invalid input' do
      expect { described_class.coerce_isolated_input('not valid') }
        .to raise_error(GraphQL::CoercionError, /not a valid Global ID/)
    end

    it 'rejects nil' do
      expect(described_class.coerce_isolated_input(nil)).to be_nil
    end

    it 'rejects GIDs from different apps' do
      invalid_gid = GlobalID.new(::URI::GID.build(app: 'otherapp', model_name: 'Project', model_id: project.id, params: nil))

      expect { described_class.coerce_isolated_input(invalid_gid) }
        .to raise_error(GraphQL::CoercionError, /is not a Gitlab Global ID/)
    end
  end

  describe 'a parameterized type' do
    let(:type) { ::Types::GlobalIDType[::Project] }

    it 'is has the correct name' do
      expect(type.graphql_name).to eq('ProjectID')
    end

    context 'the GID is appropriate' do
      it 'can coerce results' do
        expect(type.coerce_isolated_result(gid)).to eq(gid.to_s)
      end

      it 'can coerce IDs to a GlobalIDType' do
        expect(type.coerce_isolated_result(project.id)).to eq(gid.to_s)
      end

      it 'can coerce valid input' do
        expect(type.coerce_isolated_input(gid.to_s)).to eq(gid)
      end
    end

    context 'the GID is not for an appropriate type' do
      let(:gid) { build_stubbed(:user).to_global_id }

      it 'raises errors when coercing results' do
        expect { type.coerce_isolated_result(gid) }
          .to raise_error(GraphQL::CoercionError, /Expected a Project ID/)
      end

      it 'will not coerce invalid input, even if its a valid GID' do
        expect { type.coerce_isolated_input(gid.to_s) }
          .to raise_error(GraphQL::CoercionError, /does not represent an instance of Project/)
      end
    end

    it 'handles GIDs for invalid resource names gracefully' do
      invalid_gid = GlobalID.new(::URI::GID.build(app: GlobalID.app, model_name: 'invalid', model_id: 1, params: nil))

      expect { type.coerce_isolated_input(invalid_gid) }
        .to raise_error(GraphQL::CoercionError, /does not represent an instance of Project/)
    end

    context 'with a deprecation' do
      around do |example|
        # Unset all previously memoized GlobalIDTypes to allow us to define one
        # that will use the constants stubbed in the `before` block.
        previous_id_types = Types::GlobalIDType.instance_variable_get(:@id_types)
        Types::GlobalIDType.instance_variable_set(:@id_types, {})

        example.run
      ensure
        Types::GlobalIDType.instance_variable_set(:@id_types, previous_id_types)
      end

      before do
        deprecation = Gitlab::GlobalId::Deprecations::Deprecation.new(old_model_name: 'OldIssue', new_model_name: 'Issue', milestone: '10.0')

        stub_global_id_deprecations(deprecation)
      end

      let_it_be(:issue) { create(:issue) }

      let!(:type) { ::Types::GlobalIDType[::Issue] }
      let(:deprecated_gid) { Gitlab::GlobalId.build(model_name: 'OldIssue', id: issue.id) }
      let(:deprecating_gid) { Gitlab::GlobalId.build(model_name: 'Issue', id: issue.id) }

      it 'appends the description with a deprecation notice for the old Global ID' do
        expect(type.description).to include('The older format `"gid://gitlab/OldIssue/1"` was deprecated in 10.0')
      end

      describe 'coercing input against the type (parsing the Global ID string when supplied as an argument)' do
        subject(:result) { type.coerce_isolated_input(gid.to_s) }

        context 'when passed the deprecated Global ID' do
          let(:gid) { deprecated_gid }

          it 'changes the model_name to the new model name' do
            expect(result.model_name).to eq('Issue')
          end

          it 'changes the model_class to the new model class' do
            expect(result.model_class).to eq(Issue)
          end

          it 'can find the correct resource' do
            expect(result.find).to eq(issue)
          end

          it 'can find the correct resource loaded through GitlabSchema' do
            expect(force(GitlabSchema.object_from_id(result, expected_class: Issue))).to eq(issue)
          end
        end

        context 'when passed the Global ID that is deprecating another' do
          let(:gid) { deprecating_gid }

          it 'works as normal' do
            expect(result).to have_attributes(
              model_class: Issue,
              model_name: 'Issue',
              find: issue,
              to_s: gid.to_s
            )
          end
        end
      end

      describe 'coercing the result against the type (producing the Global ID string when used in a field)' do
        context 'when passed the deprecated Global ID' do
          let(:gid) { deprecated_gid }

          it 'works, but does not result in matching the new Global ID', :aggregate_failures do
            # Note, this would normally never happen in real life as the object being parsed
            # by the field would not produce the GlobalID of the deprecated model. This test
            # proves that it is technically possible for the deprecated GlobalID to be
            # considered parsable for the type, as opposed to raising a `GraphQL::CoercionError`.
            expect(type.coerce_isolated_result(gid)).not_to eq(issue.to_global_id.to_s)
            expect(type.coerce_isolated_result(gid)).to eq(gid.to_s)
          end
        end

        context 'when passed the Global ID that is deprecating another' do
          let(:gid) { deprecating_gid }

          it 'works as normal' do
            expect(type.coerce_isolated_result(gid)).to eq(issue.to_global_id.to_s)
          end
        end
      end

      describe 'executing against the schema' do
        let(:query_result) do
          context = { current_user: issue.project.first_owner }
          variables = { 'id' => gid }

          run_with_clean_state(query, context: context, variables: variables).to_h
        end

        shared_examples 'a query that works with old and new GIDs' do
          let(:query) do
            <<-GQL
            query($id: #{argument_name}!) {
              issue(id: $id) {
                id
              }
            }
            GQL
          end

          subject { query_result.dig('data', 'issue', 'id') }

          context 'when the argument value is the new GID' do
            let(:gid) { Gitlab::GlobalId.build(model_name: 'Issue', id: issue.id) }

            it { is_expected.to be_present }
          end

          context 'when the argument value is the old GID' do
            let(:gid) { Gitlab::GlobalId.build(model_name: 'OldIssue', id: issue.id) }

            it { is_expected.to be_present }
          end
        end

        context 'when the query signature includes the old type name' do
          let(:argument_name) { 'OldIssueID' }

          it_behaves_like 'a query that works with old and new GIDs'
        end

        context 'when the query signature includes the new type name' do
          let(:argument_name) { 'IssueID' }

          it_behaves_like 'a query that works with old and new GIDs'
        end
      end
    end
  end

  describe 'a parameterized type with a namespace' do
    let(:type) { ::Types::GlobalIDType[::Ci::Build] }

    it 'is has a valid GraphQL identifier for a name' do
      expect(type.graphql_name).to eq('CiBuildID')
    end
  end

  describe 'compatibility' do
    def query(doc, vars)
      GraphQL::Query.new(schema, document: doc, context: {}, variables: vars)
    end

    def run_query(gql_query, vars)
      query(GraphQL.parse(gql_query), vars).result
    end

    all_types = [::GraphQL::Types::ID, ::Types::GlobalIDType, ::Types::GlobalIDType[::Project]]

    shared_examples 'a working query' do
      # Simplified schema to test compatibility
      let!(:schema) do
        # capture values so they can be closed over
        arg_type = argument_type
        res_type = result_type

        project = Class.new(GraphQL::Schema::Object) do
          graphql_name 'Project'
          field :name, String, null: false
          field :id, res_type, null: false, resolver_method: :global_id

          def global_id
            object.to_global_id
          end
        end

        Class.new(GraphQL::Schema) do
          query(Class.new(GraphQL::Schema::Object) do
            graphql_name 'Query'

            field :project_by_id, project, null: true do
              argument :id, arg_type, required: true
            end

            # This is needed so that all types are always registered as input types
            field :echo, String, null: true do
              argument :id, ::GraphQL::Types::ID, required: false
              argument :gid, ::Types::GlobalIDType, required: false
              argument :pid, ::Types::GlobalIDType[::Project], required: false
            end

            def project_by_id(id:)
              gid = ::Types::GlobalIDType[::Project].coerce_isolated_input(id)
              gid.model_class.find(gid.model_id)
            end

            def echo(id: nil, gid: nil, pid: nil)
              "id: #{id}, gid: #{gid}, pid: #{pid}"
            end
          end)
        end
      end

      it 'works' do
        res = run_query(document, 'projectId' => project.to_global_id.to_s)

        expect(res['errors']).to be_blank
        expect(res.dig('data', 'project', 'name')).to eq(project.name)
        expect(res.dig('data', 'project', 'id')).to eq(project.to_global_id.to_s)
      end
    end

    context 'when the client declares the argument as ID the actual argument can be any type' do
      let(:document) do
        <<-GRAPHQL
        query($projectId: ID!){
          project: projectById(id: $projectId) {
            name, id
          }
        }
        GRAPHQL
      end

      where(:result_type, :argument_type) do
        all_types.flat_map { |arg_type| all_types.zip([arg_type].cycle) }
      end

      with_them do
        it_behaves_like 'a working query'
      end
    end

    context 'when the client passes the argument as GlobalID' do
      let(:document) do
        <<-GRAPHQL
        query($projectId: GlobalID!) {
          project: projectById(id: $projectId) {
            name, id
          }
        }
        GRAPHQL
      end

      let(:argument_type) { ::Types::GlobalIDType }

      where(:result_type) { all_types }

      with_them do
        it_behaves_like 'a working query'
      end
    end

    context 'when the client passes the argument as ProjectID' do
      let(:document) do
        <<-GRAPHQL
        query($projectId: ProjectID!) {
          project: projectById(id: $projectId) {
            name, id
          }
        }
        GRAPHQL
      end

      let(:argument_type) { ::Types::GlobalIDType[::Project] }

      where(:result_type) { all_types }

      with_them do
        it_behaves_like 'a working query'
      end
    end
  end

  describe '.model_name_to_graphql_name' do
    it 'returns a graphql name for the given model name' do
      expect(described_class.model_name_to_graphql_name('DesignManagement::Design')).to eq('DesignManagementDesignID')
    end
  end

  describe '.[]' do
    it 'returns a custom class for work items' do
      expect(described_class[::WorkItem]).to eq(::Types::WorkItemIdType)
    end
  end
end