@@ -71,12 +71,12 @@ export function buildServiceDefinition(
7171 typeExtensionsMap [ typeName ] = [ definition ] ;
7272 }
7373 } else if ( definition . kind === Kind . DIRECTIVE_DEFINITION ) {
74- const typeName = definition . name . value ;
74+ const directiveName = definition . name . value ;
7575
76- if ( directivesMap [ typeName ] ) {
77- directivesMap [ typeName ] . push ( definition ) ;
76+ if ( directivesMap [ directiveName ] ) {
77+ directivesMap [ directiveName ] . push ( definition ) ;
7878 } else {
79- directivesMap [ typeName ] = [ definition ] ;
79+ directivesMap [ directiveName ] = [ definition ] ;
8080 }
8181 } else if ( definition . kind === Kind . SCHEMA_DEFINITION ) {
8282 schemaDefinitions . push ( definition ) ;
@@ -99,6 +99,17 @@ export function buildServiceDefinition(
9999 }
100100 }
101101
102+ for ( const [ directiveName , directives ] of Object . entries ( directivesMap ) ) {
103+ if ( directives . length > 1 ) {
104+ errors . push (
105+ new GraphQLError (
106+ `Directive "${ directiveName } " was defined more than once.` ,
107+ directives
108+ )
109+ ) ;
110+ }
111+ }
112+
102113 let operationTypeMap : { [ operation in OperationTypeNode ] ?: string } ;
103114
104115 if ( schemaDefinitions . length > 0 || schemaExtensions . length > 0 ) {
@@ -168,10 +179,11 @@ export function buildServiceDefinition(
168179
169180 try {
170181 const typeDefinitions = Object . values ( typeDefinitionsMap ) . flat ( ) ;
182+ const directives = Object . values ( directivesMap ) . flat ( ) ;
171183
172184 let schema = buildASTSchema ( {
173185 kind : Kind . DOCUMENT ,
174- definitions : typeDefinitions
186+ definitions : [ ... typeDefinitions , ... directives ]
175187 } ) ;
176188
177189 const typeExtensions = Object . values ( typeExtensionsMap ) . flat ( ) ;
@@ -183,15 +195,6 @@ export function buildServiceDefinition(
183195 } ) ;
184196 }
185197
186- const directiveExtensions = Object . values ( directivesMap ) . flat ( ) ;
187-
188- if ( directiveExtensions . length > 0 ) {
189- schema = extendSchema ( schema , {
190- kind : Kind . DOCUMENT ,
191- definitions : directiveExtensions
192- } ) ;
193- }
194-
195198 for ( const module of modules ) {
196199 if ( ! module . resolvers ) continue ;
197200
0 commit comments