3
3
import { expect } from 'chai' ;
4
4
import { describe , it } from 'mocha' ;
5
5
6
- import {
7
- FieldsOnCorrectType ,
8
- undefinedFieldMessage ,
9
- } from '../rules/FieldsOnCorrectType' ;
6
+ import { parse } from '../../language/parser' ;
7
+
8
+ import { buildSchema } from '../../utilities/buildASTSchema' ;
9
+
10
+ import { validate } from '../validate' ;
11
+ import { FieldsOnCorrectType } from '../rules/FieldsOnCorrectType' ;
10
12
11
13
import { expectValidationErrors } from './harness' ;
12
14
@@ -18,25 +20,6 @@ function expectValid(queryStr) {
18
20
expectErrors ( queryStr ) . to . deep . equal ( [ ] ) ;
19
21
}
20
22
21
- function undefinedField (
22
- field ,
23
- type ,
24
- suggestedTypes ,
25
- suggestedFields ,
26
- line ,
27
- column ,
28
- ) {
29
- return {
30
- message : undefinedFieldMessage (
31
- field ,
32
- type ,
33
- suggestedTypes ,
34
- suggestedFields ,
35
- ) ,
36
- locations : [ { line, column } ] ,
37
- } ;
38
- }
39
-
40
23
describe ( 'Validate: Fields on correct type' , ( ) => {
41
24
it ( 'Object field selection' , ( ) => {
42
25
expectValid ( `
@@ -99,8 +82,14 @@ describe('Validate: Fields on correct type', () => {
99
82
}
100
83
}
101
84
` ) . to . deep . equal ( [
102
- undefinedField ( 'unknown_pet_field' , 'Pet' , [ ] , [ ] , 3 , 9 ) ,
103
- undefinedField ( 'unknown_cat_field' , 'Cat' , [ ] , [ ] , 5 , 13 ) ,
85
+ {
86
+ message : 'Cannot query field "unknown_pet_field" on type "Pet".' ,
87
+ locations : [ { line : 3 , column : 9 } ] ,
88
+ } ,
89
+ {
90
+ message : 'Cannot query field "unknown_cat_field" on type "Cat".' ,
91
+ locations : [ { line : 5 , column : 13 } ] ,
92
+ } ,
104
93
] ) ;
105
94
} ) ;
106
95
@@ -110,7 +99,11 @@ describe('Validate: Fields on correct type', () => {
110
99
meowVolume
111
100
}
112
101
` ) . to . deep . equal ( [
113
- undefinedField ( 'meowVolume' , 'Dog' , [ ] , [ 'barkVolume' ] , 3 , 9 ) ,
102
+ {
103
+ message :
104
+ 'Cannot query field "meowVolume" on type "Dog". Did you mean "barkVolume"?' ,
105
+ locations : [ { line : 3 , column : 9 } ] ,
106
+ } ,
114
107
] ) ;
115
108
} ) ;
116
109
@@ -121,7 +114,12 @@ describe('Validate: Fields on correct type', () => {
121
114
deeper_unknown_field
122
115
}
123
116
}
124
- ` ) . to . deep . equal ( [ undefinedField ( 'unknown_field' , 'Dog' , [ ] , [ ] , 3 , 9 ) ] ) ;
117
+ ` ) . to . deep . equal ( [
118
+ {
119
+ message : 'Cannot query field "unknown_field" on type "Dog".' ,
120
+ locations : [ { line : 3 , column : 9 } ] ,
121
+ } ,
122
+ ] ) ;
125
123
} ) ;
126
124
127
125
it ( 'Sub-field not defined' , ( ) => {
@@ -131,7 +129,12 @@ describe('Validate: Fields on correct type', () => {
131
129
unknown_field
132
130
}
133
131
}
134
- ` ) . to . deep . equal ( [ undefinedField ( 'unknown_field' , 'Pet' , [ ] , [ ] , 4 , 11 ) ] ) ;
132
+ ` ) . to . deep . equal ( [
133
+ {
134
+ message : 'Cannot query field "unknown_field" on type "Pet".' ,
135
+ locations : [ { line : 4 , column : 11 } ] ,
136
+ } ,
137
+ ] ) ;
135
138
} ) ;
136
139
137
140
it ( 'Field not defined on inline fragment' , ( ) => {
@@ -142,7 +145,11 @@ describe('Validate: Fields on correct type', () => {
142
145
}
143
146
}
144
147
` ) . to . deep . equal ( [
145
- undefinedField ( 'meowVolume' , 'Dog' , [ ] , [ 'barkVolume' ] , 4 , 11 ) ,
148
+ {
149
+ message :
150
+ 'Cannot query field "meowVolume" on type "Dog". Did you mean "barkVolume"?' ,
151
+ locations : [ { line : 4 , column : 11 } ] ,
152
+ } ,
146
153
] ) ;
147
154
} ) ;
148
155
@@ -152,7 +159,11 @@ describe('Validate: Fields on correct type', () => {
152
159
volume : mooVolume
153
160
}
154
161
` ) . to . deep . equal ( [
155
- undefinedField ( 'mooVolume' , 'Dog' , [ ] , [ 'barkVolume' ] , 3 , 9 ) ,
162
+ {
163
+ message :
164
+ 'Cannot query field "mooVolume" on type "Dog". Did you mean "barkVolume"?' ,
165
+ locations : [ { line : 3 , column : 9 } ] ,
166
+ } ,
156
167
] ) ;
157
168
} ) ;
158
169
@@ -162,7 +173,11 @@ describe('Validate: Fields on correct type', () => {
162
173
barkVolume : kawVolume
163
174
}
164
175
` ) . to . deep . equal ( [
165
- undefinedField ( 'kawVolume' , 'Dog' , [ ] , [ 'barkVolume' ] , 3 , 9 ) ,
176
+ {
177
+ message :
178
+ 'Cannot query field "kawVolume" on type "Dog". Did you mean "barkVolume"?' ,
179
+ locations : [ { line : 3 , column : 9 } ] ,
180
+ } ,
166
181
] ) ;
167
182
} ) ;
168
183
@@ -171,7 +186,12 @@ describe('Validate: Fields on correct type', () => {
171
186
fragment notDefinedOnInterface on Pet {
172
187
tailLength
173
188
}
174
- ` ) . to . deep . equal ( [ undefinedField ( 'tailLength' , 'Pet' , [ ] , [ ] , 3 , 9 ) ] ) ;
189
+ ` ) . to . deep . equal ( [
190
+ {
191
+ message : 'Cannot query field "tailLength" on type "Pet".' ,
192
+ locations : [ { line : 3 , column : 9 } ] ,
193
+ } ,
194
+ ] ) ;
175
195
} ) ;
176
196
177
197
it ( 'Defined on implementors but not on interface' , ( ) => {
@@ -180,7 +200,11 @@ describe('Validate: Fields on correct type', () => {
180
200
nickname
181
201
}
182
202
` ) . to . deep . equal ( [
183
- undefinedField ( 'nickname' , 'Pet' , [ 'Dog' , 'Cat' ] , [ 'name' ] , 3 , 9 ) ,
203
+ {
204
+ message :
205
+ 'Cannot query field "nickname" on type "Pet". Did you mean to use an inline fragment on "Dog" or "Cat"?' ,
206
+ locations : [ { line : 3 , column : 9 } ] ,
207
+ } ,
184
208
] ) ;
185
209
} ) ;
186
210
@@ -197,7 +221,12 @@ describe('Validate: Fields on correct type', () => {
197
221
fragment directFieldSelectionOnUnion on CatOrDog {
198
222
directField
199
223
}
200
- ` ) . to . deep . equal ( [ undefinedField ( 'directField' , 'CatOrDog' , [ ] , [ ] , 3 , 9 ) ] ) ;
224
+ ` ) . to . deep . equal ( [
225
+ {
226
+ message : 'Cannot query field "directField" on type "CatOrDog".' ,
227
+ locations : [ { line : 3 , column : 9 } ] ,
228
+ } ,
229
+ ] ) ;
201
230
} ) ;
202
231
203
232
it ( 'Defined on implementors queried on union' , ( ) => {
@@ -206,14 +235,11 @@ describe('Validate: Fields on correct type', () => {
206
235
name
207
236
}
208
237
` ) . to . deep . equal ( [
209
- undefinedField (
210
- 'name' ,
211
- 'CatOrDog' ,
212
- [ 'Being' , 'Pet' , 'Canine' , 'Dog' , 'Cat' ] ,
213
- [ ] ,
214
- 3 ,
215
- 9 ,
216
- ) ,
238
+ {
239
+ message :
240
+ 'Cannot query field "name" on type "CatOrDog". Did you mean to use an inline fragment on "Being", "Pet", "Canine", "Dog", or "Cat"?' ,
241
+ locations : [ { line : 3 , column : 9 } ] ,
242
+ } ,
217
243
] ) ;
218
244
} ) ;
219
245
@@ -231,42 +257,110 @@ describe('Validate: Fields on correct type', () => {
231
257
} ) ;
232
258
233
259
describe ( 'Fields on correct type error message' , ( ) => {
260
+ function expectErrorMessage ( schema , queryStr ) {
261
+ const errors = validate ( schema , parse ( queryStr ) , [ FieldsOnCorrectType ] ) ;
262
+ expect ( errors . length ) . to . equal ( 1 ) ;
263
+ return expect ( errors [ 0 ] . message ) ;
264
+ }
265
+
234
266
it ( 'Works with no suggestions' , ( ) => {
235
- expect ( undefinedFieldMessage ( 'f' , 'T' , [ ] , [ ] ) ) . to . equal (
267
+ const schema = buildSchema ( `
268
+ type T {
269
+ fieldWithVeryLongNameThatWillNeverBeSuggested: String
270
+ }
271
+ type Query { t: T }
272
+ ` ) ;
273
+
274
+ expectErrorMessage ( schema , '{ t { f } }' ) . to . equal (
236
275
'Cannot query field "f" on type "T".' ,
237
276
) ;
238
277
} ) ;
239
278
240
279
it ( 'Works with no small numbers of type suggestions' , ( ) => {
241
- expect ( undefinedFieldMessage ( 'f' , 'T' , [ 'A' , 'B' ] , [ ] ) ) . to . equal (
280
+ const schema = buildSchema ( `
281
+ union T = A | B
282
+ type Query { t: T }
283
+
284
+ type A { f: String }
285
+ type B { f: String }
286
+ ` ) ;
287
+
288
+ expectErrorMessage ( schema , '{ t { f } }' ) . to . equal (
242
289
'Cannot query field "f" on type "T". Did you mean to use an inline fragment on "A" or "B"?' ,
243
290
) ;
244
291
} ) ;
245
292
246
293
it ( 'Works with no small numbers of field suggestions' , ( ) => {
247
- expect ( undefinedFieldMessage ( 'f' , 'T' , [ ] , [ 'z' , 'y' ] ) ) . to . equal (
294
+ const schema = buildSchema ( `
295
+ type T {
296
+ z: String
297
+ y: String
298
+ }
299
+ type Query { t: T }
300
+ ` ) ;
301
+
302
+ expectErrorMessage ( schema , '{ t { f } }' ) . to . equal (
248
303
'Cannot query field "f" on type "T". Did you mean "z" or "y"?' ,
249
304
) ;
250
305
} ) ;
251
306
252
307
it ( 'Only shows one set of suggestions at a time, preferring types' , ( ) => {
253
- expect ( undefinedFieldMessage ( 'f' , 'T' , [ 'A' , 'B' ] , [ 'z' , 'y' ] ) ) . to . equal (
308
+ const schema = buildSchema ( `
309
+ interface T {
310
+ z: String
311
+ y: String
312
+ }
313
+ type Query { t: T }
314
+
315
+ type A implements T {
316
+ f: String
317
+ z: String
318
+ y: String
319
+ }
320
+ type B implements T {
321
+ f: String
322
+ z: String
323
+ y: String
324
+ }
325
+ ` ) ;
326
+
327
+ expectErrorMessage ( schema , '{ t { f } }' ) . to . equal (
254
328
'Cannot query field "f" on type "T". Did you mean to use an inline fragment on "A" or "B"?' ,
255
329
) ;
256
330
} ) ;
257
331
258
332
it ( 'Limits lots of type suggestions' , ( ) => {
259
- expect (
260
- undefinedFieldMessage ( 'f' , 'T' , [ 'A' , 'B' , 'C' , 'D' , 'E' , 'F' ] , [ ] ) ,
261
- ) . to . equal (
333
+ const schema = buildSchema ( `
334
+ union T = A | B | C | D | E | F
335
+ type Query { t: T }
336
+
337
+ type A { f: String }
338
+ type B { f: String }
339
+ type C { f: String }
340
+ type D { f: String }
341
+ type E { f: String }
342
+ type F { f: String }
343
+ ` ) ;
344
+
345
+ expectErrorMessage ( schema , '{ t { f } }' ) . to . equal (
262
346
'Cannot query field "f" on type "T". Did you mean to use an inline fragment on "A", "B", "C", "D", or "E"?' ,
263
347
) ;
264
348
} ) ;
265
349
266
350
it ( 'Limits lots of field suggestions' , ( ) => {
267
- expect (
268
- undefinedFieldMessage ( 'f' , 'T' , [ ] , [ 'z' , 'y' , 'x' , 'w' , 'v' , 'u' ] ) ,
269
- ) . to . equal (
351
+ const schema = buildSchema ( `
352
+ type T {
353
+ z: String
354
+ y: String
355
+ x: String
356
+ w: String
357
+ v: String
358
+ u: String
359
+ }
360
+ type Query { t: T }
361
+ ` ) ;
362
+
363
+ expectErrorMessage ( schema , '{ t { f } }' ) . to . equal (
270
364
'Cannot query field "f" on type "T". Did you mean "z", "y", "x", "w", or "v"?' ,
271
365
) ;
272
366
} ) ;
0 commit comments