6
6
*/
7
7
8
8
import { describe , it } from 'mocha' ;
9
- import { expectPassesRule , expectFailsRule } from './harness' ;
9
+ import { buildSchema } from '../../utilities' ;
10
+ import {
11
+ expectPassesRule ,
12
+ expectFailsRule ,
13
+ expectSDLErrorsFromRule ,
14
+ } from './harness' ;
15
+
10
16
import {
11
17
KnownDirectives ,
12
18
unknownDirectiveMessage ,
13
19
misplacedDirectiveMessage ,
14
20
} from '../rules/KnownDirectives' ;
15
21
22
+ const expectSDLErrors = expectSDLErrorsFromRule . bind (
23
+ undefined ,
24
+ KnownDirectives ,
25
+ ) ;
26
+
16
27
function unknownDirective ( directiveName , line , column ) {
17
28
return {
18
29
message : unknownDirectiveMessage ( directiveName ) ,
@@ -27,6 +38,20 @@ function misplacedDirective(directiveName, placement, line, column) {
27
38
} ;
28
39
}
29
40
41
+ const schemaWithSDLDirectives = buildSchema ( `
42
+ directive @onSchema on SCHEMA
43
+ directive @onScalar on SCALAR
44
+ directive @onObject on OBJECT
45
+ directive @onFieldDefinition on FIELD_DEFINITION
46
+ directive @onArgumentDefinition on ARGUMENT_DEFINITION
47
+ directive @onInterface on INTERFACE
48
+ directive @onUnion on UNION
49
+ directive @onEnum on ENUM
50
+ directive @onEnumValue on ENUM_VALUE
51
+ directive @onInputObject on INPUT_OBJECT
52
+ directive @onInputFieldDefinition on INPUT_FIELD_DEFINITION
53
+ ` ) ;
54
+
30
55
describe ( 'Validate: Known directives' , ( ) => {
31
56
it ( 'with no directives' , ( ) => {
32
57
expectPassesRule (
@@ -138,10 +163,36 @@ describe('Validate: Known directives', () => {
138
163
) ;
139
164
} ) ;
140
165
141
- describe ( 'within schema language' , ( ) => {
166
+ describe ( 'within SDL' , ( ) => {
167
+ it ( 'with directive defined inside SDL' , ( ) => {
168
+ expectSDLErrors ( `
169
+ type Query {
170
+ foo: String @test
171
+ }
172
+
173
+ directive @test on FIELD_DEFINITION
174
+ ` ) . to . deep . equal ( [ ] ) ;
175
+ } ) ;
176
+
177
+ it ( 'with standard directive' , ( ) => {
178
+ expectSDLErrors ( `
179
+ type Query {
180
+ foo: String @deprecated
181
+ }
182
+ ` ) . to . deep . equal ( [ ] ) ;
183
+ } ) ;
184
+
185
+ it ( 'with overrided standard directive' , ( ) => {
186
+ expectSDLErrors ( `
187
+ schema @deprecated {
188
+ query: Query
189
+ }
190
+ directive @deprecated on SCHEMA
191
+ ` ) . to . deep . equal ( [ ] ) ;
192
+ } ) ;
193
+
142
194
it ( 'with well placed directives' , ( ) => {
143
- expectPassesRule (
144
- KnownDirectives ,
195
+ expectSDLErrors (
145
196
`
146
197
type MyObj implements MyInterface @onObject {
147
198
myField(myArg: Int @onArgumentDefinition): String @onFieldDefinition
@@ -180,13 +231,13 @@ describe('Validate: Known directives', () => {
180
231
}
181
232
182
233
extend schema @onSchema
183
- ` ,
184
- ) ;
234
+ ` ,
235
+ schemaWithSDLDirectives ,
236
+ ) . to . deep . equal ( [ ] ) ;
185
237
} ) ;
186
238
187
239
it ( 'with misplaced directives' , ( ) => {
188
- expectFailsRule (
189
- KnownDirectives ,
240
+ expectSDLErrors (
190
241
`
191
242
type MyObj implements MyInterface @onInterface {
192
243
myField(myArg: Int @onInputFieldDefinition): String @onInputFieldDefinition
@@ -213,49 +264,39 @@ describe('Validate: Known directives', () => {
213
264
}
214
265
215
266
extend schema @onObject
216
- ` ,
217
- [
218
- misplacedDirective ( 'onInterface' , 'OBJECT' , 2 , 43 ) ,
219
- misplacedDirective (
220
- 'onInputFieldDefinition' ,
221
- 'ARGUMENT_DEFINITION' ,
222
- 3 ,
223
- 30 ,
224
- ) ,
225
- misplacedDirective (
226
- 'onInputFieldDefinition' ,
227
- 'FIELD_DEFINITION' ,
228
- 3 ,
229
- 63 ,
230
- ) ,
231
- misplacedDirective ( 'onEnum' , 'SCALAR' , 6 , 25 ) ,
232
- misplacedDirective ( 'onObject' , 'INTERFACE' , 8 , 31 ) ,
233
- misplacedDirective (
234
- 'onInputFieldDefinition' ,
235
- 'ARGUMENT_DEFINITION' ,
236
- 9 ,
237
- 30 ,
238
- ) ,
239
- misplacedDirective (
240
- 'onInputFieldDefinition' ,
241
- 'FIELD_DEFINITION' ,
242
- 9 ,
243
- 63 ,
244
- ) ,
245
- misplacedDirective ( 'onEnumValue' , 'UNION' , 12 , 23 ) ,
246
- misplacedDirective ( 'onScalar' , 'ENUM' , 14 , 21 ) ,
247
- misplacedDirective ( 'onUnion' , 'ENUM_VALUE' , 15 , 20 ) ,
248
- misplacedDirective ( 'onEnum' , 'INPUT_OBJECT' , 18 , 23 ) ,
249
- misplacedDirective (
250
- 'onArgumentDefinition' ,
251
- 'INPUT_FIELD_DEFINITION' ,
252
- 19 ,
253
- 24 ,
254
- ) ,
255
- misplacedDirective ( 'onObject' , 'SCHEMA' , 22 , 16 ) ,
256
- misplacedDirective ( 'onObject' , 'SCHEMA' , 26 , 23 ) ,
257
- ] ,
258
- ) ;
267
+ ` ,
268
+ schemaWithSDLDirectives ,
269
+ ) . to . deep . equal ( [
270
+ misplacedDirective ( 'onInterface' , 'OBJECT' , 2 , 43 ) ,
271
+ misplacedDirective (
272
+ 'onInputFieldDefinition' ,
273
+ 'ARGUMENT_DEFINITION' ,
274
+ 3 ,
275
+ 30 ,
276
+ ) ,
277
+ misplacedDirective ( 'onInputFieldDefinition' , 'FIELD_DEFINITION' , 3 , 63 ) ,
278
+ misplacedDirective ( 'onEnum' , 'SCALAR' , 6 , 25 ) ,
279
+ misplacedDirective ( 'onObject' , 'INTERFACE' , 8 , 31 ) ,
280
+ misplacedDirective (
281
+ 'onInputFieldDefinition' ,
282
+ 'ARGUMENT_DEFINITION' ,
283
+ 9 ,
284
+ 30 ,
285
+ ) ,
286
+ misplacedDirective ( 'onInputFieldDefinition' , 'FIELD_DEFINITION' , 9 , 63 ) ,
287
+ misplacedDirective ( 'onEnumValue' , 'UNION' , 12 , 23 ) ,
288
+ misplacedDirective ( 'onScalar' , 'ENUM' , 14 , 21 ) ,
289
+ misplacedDirective ( 'onUnion' , 'ENUM_VALUE' , 15 , 20 ) ,
290
+ misplacedDirective ( 'onEnum' , 'INPUT_OBJECT' , 18 , 23 ) ,
291
+ misplacedDirective (
292
+ 'onArgumentDefinition' ,
293
+ 'INPUT_FIELD_DEFINITION' ,
294
+ 19 ,
295
+ 24 ,
296
+ ) ,
297
+ misplacedDirective ( 'onObject' , 'SCHEMA' , 22 , 16 ) ,
298
+ misplacedDirective ( 'onObject' , 'SCHEMA' , 26 , 23 ) ,
299
+ ] ) ;
259
300
} ) ;
260
301
} ) ;
261
302
} ) ;
0 commit comments