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
|
# frozen_string_literal: true
require 'spec_helper'
require 'fast_spec_helper'
require "test_prof/recipes/rspec/let_it_be"
RSpec.describe Gitlab::Graphql::Queries do
shared_examples 'a valid GraphQL query for the blog schema' do
it 'is valid' do
expect(subject.validate(schema).second).to be_empty
end
end
shared_examples 'an invalid GraphQL query for the blog schema' do
it 'is invalid' do
expect(subject.validate(schema).second).to match errors
end
end
# Toy schema to validate queries against
let_it_be(:schema) do
author = Class.new(GraphQL::Schema::Object) do
graphql_name 'Author'
field :name, GraphQL::Types::String, null: true
field :handle, GraphQL::Types::String, null: false
field :verified, GraphQL::Types::Boolean, null: false
end
post = Class.new(GraphQL::Schema::Object) do
graphql_name 'Post'
field :name, GraphQL::Types::String, null: false
field :title, GraphQL::Types::String, null: false
field :content, GraphQL::Types::String, null: true
field :author, author, null: false
end
author.field :posts, [post], null: false do
argument :blog_title, GraphQL::Types::String, required: false
end
blog = Class.new(GraphQL::Schema::Object) do
graphql_name 'Blog'
field :title, GraphQL::Types::String, null: false
field :description, GraphQL::Types::String, null: false
field :main_author, author, null: false
field :posts, [post], null: false
field :post, post, null: true do
argument :slug, GraphQL::Types::String, required: true
end
end
Class.new(GraphQL::Schema) do
query(Class.new(GraphQL::Schema::Object) do
graphql_name 'Query'
field :blog, blog, null: true do
argument :title, GraphQL::Types::String, required: true
end
field :post, post, null: true do
argument :slug, GraphQL::Types::String, required: true
end
end)
end
end
let(:root) do
Rails.root / 'fixtures/lib/gitlab/graphql/queries'
end
describe Gitlab::Graphql::Queries::Fragments do
subject { described_class.new(root) }
it 'has the right home' do
expect(subject.home).to eq (root / 'app/assets/javascripts').to_s
end
it 'has the right EE home' do
expect(subject.home_ee).to eq (root / 'ee/app/assets/javascripts').to_s
end
it 'caches query definitions' do
fragment = subject.get('foo')
expect(fragment).to be_a(::Gitlab::Graphql::Queries::Definition)
expect(subject.get('foo')).to be fragment
end
end
describe '.all' do
it 'is the combination of finding queries in CE and EE' do
expect(described_class)
.to receive(:find).with(Rails.root / 'app/assets/javascripts').and_return([:ce])
expect(described_class)
.to receive(:find).with(Rails.root / 'ee/app/assets/javascripts').and_return([:ee])
expect(described_class.all).to eq([:ce, :ee])
end
end
describe '.find' do
def definition_of(path)
be_a(::Gitlab::Graphql::Queries::Definition)
.and(have_attributes(file: path.to_s))
end
it 'find a single specific file' do
path = root / 'post_by_slug.graphql'
expect(described_class.find(path)).to contain_exactly(definition_of(path))
end
it 'ignores files that do not exist' do
path = root / 'not_there.graphql'
expect(described_class.find(path)).to be_empty
end
it 'ignores fragments' do
path = root / 'author.fragment.graphql'
expect(described_class.find(path)).to be_empty
end
it 'ignores typedefs' do
path = root / 'typedefs.graphql'
expect(described_class.find(path)).to be_empty
end
it 'ignores customer.query.graphql' do
path = root / 'plans.customer.query.graphql'
expect(described_class.find(path)).to be_empty
end
it 'ignores customer.mutation.graphql' do
path = root / 'plans.customer.mutation.graphql'
expect(described_class.find(path)).to be_empty
end
it 'finds all query definitions under a root directory' do
found = described_class.find(root)
expect(found).to include(
definition_of(root / 'post_by_slug.graphql'),
definition_of(root / 'post_by_slug.with_import.graphql'),
definition_of(root / 'post_by_slug.with_import.misspelled.graphql'),
definition_of(root / 'duplicate_imports.graphql'),
definition_of(root / 'deeply/nested/query.graphql')
)
expect(found).not_to include(
definition_of(root / 'typedefs.graphql'),
definition_of(root / 'author.fragment.graphql'),
definition_of(root / 'plans.customer.query.graphql'),
definition_of(root / 'plans.customer.mutation.graphql')
)
end
end
describe Gitlab::Graphql::Queries::Definition do
let(:fragments) { Gitlab::Graphql::Queries::Fragments.new(root, '.') }
subject { described_class.new(root / path, fragments) }
context 'a simple query' do
let(:path) { 'post_by_slug.graphql' }
it_behaves_like 'a valid GraphQL query for the blog schema'
it 'has a complexity' do
expect(subject.complexity(schema)).to be < 10
end
end
context 'a query with an import' do
let(:path) { 'post_by_slug.with_import.graphql' }
it_behaves_like 'a valid GraphQL query for the blog schema'
end
context 'a query with duplicate imports' do
let(:path) { 'duplicate_imports.graphql' }
it_behaves_like 'a valid GraphQL query for the blog schema'
end
context 'a query importing from ee_else_ce' do
let(:path) { 'ee_else_ce.import.graphql' }
it_behaves_like 'a valid GraphQL query for the blog schema'
it 'can resolve the ee fields' do
expect(subject.text(mode: :ce)).not_to include('verified')
expect(subject.text(mode: :ee)).to include('verified')
end
end
context 'a query refering to parent directories' do
let(:path) { 'deeply/nested/query.graphql' }
it_behaves_like 'a valid GraphQL query for the blog schema'
end
context 'a query refering to parent directories, incorrectly' do
let(:path) { 'deeply/nested/bad_import.graphql' }
it_behaves_like 'an invalid GraphQL query for the blog schema' do
let(:errors) do
contain_exactly(
be_a(::Gitlab::Graphql::Queries::FileNotFound)
.and(have_attributes(message: include('deeply/author.fragment.graphql')))
)
end
end
end
context 'a query with a broken import' do
let(:path) { 'post_by_slug.with_import.misspelled.graphql' }
it_behaves_like 'an invalid GraphQL query for the blog schema' do
let(:errors) do
contain_exactly(
be_a(::Gitlab::Graphql::Queries::FileNotFound)
.and(have_attributes(message: include('auther.fragment.graphql')))
)
end
end
end
context 'a query which imports a file with a broken import' do
let(:path) { 'transitive_bad_import.graphql' }
it_behaves_like 'an invalid GraphQL query for the blog schema' do
let(:errors) do
contain_exactly(
be_a(::Gitlab::Graphql::Queries::FileNotFound)
.and(have_attributes(message: include('does-not-exist.graphql')))
)
end
end
end
context 'a query containing a client directive' do
let(:path) { 'client.query.graphql' }
it_behaves_like 'a valid GraphQL query for the blog schema'
it 'is tagged as a client query' do
expect(subject.validate(schema).first).to eq :client_query
end
end
context 'a mixed client query, valid' do
let(:path) { 'mixed_client.query.graphql' }
it_behaves_like 'a valid GraphQL query for the blog schema'
it 'is not tagged as a client query' do
expect(subject.validate(schema).first).not_to eq :client_query
end
end
context 'a mixed client query, with skipped argument' do
let(:path) { 'mixed_client_skipped_argument.graphql' }
it_behaves_like 'a valid GraphQL query for the blog schema'
end
context 'a mixed client query, with unused fragment' do
let(:path) { 'mixed_client_unused_fragment.graphql' }
it_behaves_like 'a valid GraphQL query for the blog schema'
end
context 'a client query, with unused fragment' do
let(:path) { 'client_unused_fragment.graphql' }
it_behaves_like 'a valid GraphQL query for the blog schema'
it 'is tagged as a client query' do
expect(subject.validate(schema).first).to eq :client_query
end
end
context 'a mixed client query, invalid' do
let(:path) { 'mixed_client_invalid.query.graphql' }
it_behaves_like 'an invalid GraphQL query for the blog schema' do
let(:errors) do
contain_exactly(have_attributes(message: include('titlz')))
end
end
end
context 'a query containing a connection directive' do
let(:path) { 'connection.query.graphql' }
it_behaves_like 'a valid GraphQL query for the blog schema'
end
context 'a query which mentions an incorrect field' do
let(:path) { 'wrong_field.graphql' }
it_behaves_like 'an invalid GraphQL query for the blog schema' do
let(:errors) do
contain_exactly(
have_attributes(message: /'createdAt' doesn't exist/),
have_attributes(message: /'categories' doesn't exist/)
)
end
end
end
context 'a query which has a missing argument' do
let(:path) { 'missing_argument.graphql' }
it_behaves_like 'an invalid GraphQL query for the blog schema' do
let(:errors) do
contain_exactly(
have_attributes(message: include('blog'))
)
end
end
end
context 'a query which has a bad argument' do
let(:path) { 'bad_argument.graphql' }
it_behaves_like 'an invalid GraphQL query for the blog schema' do
let(:errors) do
contain_exactly(
have_attributes(message: include('Nullability mismatch on variable $bad'))
)
end
end
end
context 'a query which has a syntax error' do
let(:path) { 'syntax-error.graphql' }
it_behaves_like 'an invalid GraphQL query for the blog schema' do
let(:errors) do
contain_exactly(
have_attributes(message: include('Parse error'))
)
end
end
end
context 'a query which has an unused import' do
let(:path) { 'unused_import.graphql' }
it_behaves_like 'an invalid GraphQL query for the blog schema' do
let(:errors) do
contain_exactly(
have_attributes(message: include('AuthorF was defined, but not used'))
)
end
end
end
end
end
|