1+ import { expectJSON } from '../../__testUtils__/expectJSON' ;
12import { describe , it } from 'mocha' ;
23
34import { parse } from '../../language/parser' ;
@@ -6,6 +7,7 @@ import type { GraphQLSchema } from '../../type/schema';
67
78import { extendSchema } from '../../utilities/extendSchema' ;
89
10+ import { validateSDL } from '../validate' ;
911import { UniqueDirectivesPerLocationRule } from '../rules/UniqueDirectivesPerLocationRule' ;
1012
1113import {
@@ -42,6 +44,14 @@ function expectSDLErrors(sdlStr: string, schema?: GraphQLSchema) {
4244 ) ;
4345}
4446
47+ function expectExperimentalSDLErrors ( sdlStr : string , schema ?: GraphQLSchema ) {
48+ const doc = parse ( sdlStr , {
49+ experimentalDirectivesOnDirectiveDefinitions : true ,
50+ } ) ;
51+ const errors = validateSDL ( doc , schema , [ UniqueDirectivesPerLocationRule ] ) ;
52+ return expectJSON ( errors ) ;
53+ }
54+
4555describe ( 'Validate: Directives Are Unique Per Location' , ( ) => {
4656 it ( 'no directives' , ( ) => {
4757 expectValid ( `
@@ -391,4 +401,39 @@ describe('Validate: Directives Are Unique Per Location', () => {
391401 } ,
392402 ] ) ;
393403 } ) ;
404+
405+ it ( 'duplicate directives on directive definitions' , ( ) => {
406+ expectExperimentalSDLErrors ( `
407+ directive @nonRepeatable on DIRECTIVE_DEFINITION
408+
409+ directive @testDirective @nonRepeatable @nonRepeatable on FIELD_DEFINITION
410+ ` ) . toDeepEqual ( [
411+ {
412+ message :
413+ 'The directive "@nonRepeatable" can only be used once at this location.' ,
414+ locations : [
415+ { line : 4 , column : 32 } ,
416+ { line : 4 , column : 47 } ,
417+ ] ,
418+ } ,
419+ ] ) ;
420+ } ) ;
421+
422+ it ( 'duplicate directives between directive definitions and extensions' , ( ) => {
423+ expectExperimentalSDLErrors ( `
424+ directive @nonRepeatable on DIRECTIVE_DEFINITION
425+
426+ directive @testDirective @nonRepeatable on FIELD_DEFINITION
427+ extend directive @testDirective @nonRepeatable
428+ ` ) . toDeepEqual ( [
429+ {
430+ message :
431+ 'The directive "@nonRepeatable" can only be used once at this location.' ,
432+ locations : [
433+ { line : 4 , column : 32 } ,
434+ { line : 5 , column : 39 } ,
435+ ] ,
436+ } ,
437+ ] ) ;
438+ } ) ;
394439} ) ;
0 commit comments