11import {
2- ASTNode ,
32 getNamedType ,
43 GraphQLFieldMap ,
54 GraphQLSchema ,
@@ -11,6 +10,7 @@ import {
1110 isSpecifiedScalarType ,
1211 isUnionType ,
1312} from 'graphql' ;
13+ import { DirectableGraphQLObject } from './get-directives.js' ;
1414import { getImplementingTypes } from './get-implementing-types.js' ;
1515import { MapperKind } from './Interfaces.js' ;
1616import { mapSchema } from './mapSchema.js' ;
@@ -152,12 +152,7 @@ function visitQueue(
152152 if ( isEnumType ( type ) ) {
153153 // Visit enum values directives argument types
154154 queue . push (
155- ...type . getValues ( ) . flatMap ( value => {
156- if ( value . astNode ) {
157- return getDirectivesArgumentsTypeNames ( schema , value . astNode ) ;
158- }
159- return [ ] ;
160- } ) ,
155+ ...type . getValues ( ) . flatMap ( value => getDirectivesArgumentsTypeNames ( schema , value ) ) ,
161156 ) ;
162157 }
163158 // Visit interfaces this type is implementing if they haven't been visited yet
@@ -180,9 +175,7 @@ function visitQueue(
180175 queue . push (
181176 ...field . args . flatMap ( arg => {
182177 const typeNames = [ getNamedType ( arg . type ) . name ] ;
183- if ( arg . astNode ) {
184- typeNames . push ( ...getDirectivesArgumentsTypeNames ( schema , arg . astNode ) ) ;
185- }
178+ typeNames . push ( ...getDirectivesArgumentsTypeNames ( schema , arg ) ) ;
186179 return typeNames ;
187180 } ) ,
188181 ) ;
@@ -192,9 +185,7 @@ function visitQueue(
192185
193186 queue . push ( namedType . name ) ;
194187
195- if ( field . astNode ) {
196- queue . push ( ...getDirectivesArgumentsTypeNames ( schema , field . astNode ) ) ;
197- }
188+ queue . push ( ...getDirectivesArgumentsTypeNames ( schema , field ) ) ;
198189
199190 // Interfaces returned on fields need to be revisited to add their implementations
200191 if ( isInterfaceType ( namedType ) && ! ( namedType . name in revisit ) ) {
@@ -203,9 +194,7 @@ function visitQueue(
203194 }
204195 }
205196
206- if ( type . astNode ) {
207- queue . push ( ...getDirectivesArgumentsTypeNames ( schema , type . astNode ) ) ;
208- }
197+ queue . push ( ...getDirectivesArgumentsTypeNames ( schema , type ) ) ;
209198
210199 visited . add ( typeName ) ; // Mark as visited (and therefore it is used and should be kept)
211200 }
@@ -215,10 +204,30 @@ function visitQueue(
215204
216205function getDirectivesArgumentsTypeNames (
217206 schema : GraphQLSchema ,
218- astNode : Extract < ASTNode , { readonly directives ?: any } > ,
207+ directableObj : DirectableGraphQLObject ,
219208) {
220- return ( astNode . directives ?? [ ] ) . flatMap (
221- directive =>
222- schema . getDirective ( directive . name . value ) ?. args . map ( arg => getNamedType ( arg . type ) . name ) ?? [ ] ,
223- ) ;
209+ const argTypeNames = new Set < string > ( ) ;
210+ if ( directableObj . astNode ?. directives ) {
211+ for ( const directiveNode of directableObj . astNode . directives ) {
212+ const directive = schema . getDirective ( directiveNode . name . value ) ;
213+ if ( directive ?. args ) {
214+ for ( const arg of directive . args ) {
215+ const argType = getNamedType ( arg . type ) ;
216+ argTypeNames . add ( argType . name ) ;
217+ }
218+ }
219+ }
220+ }
221+ if ( directableObj . extensions ?. [ 'directives' ] ) {
222+ for ( const directiveName in directableObj . extensions [ 'directives' ] ) {
223+ const directive = schema . getDirective ( directiveName ) ;
224+ if ( directive ?. args ) {
225+ for ( const arg of directive . args ) {
226+ const argType = getNamedType ( arg . type ) ;
227+ argTypeNames . add ( argType . name ) ;
228+ }
229+ }
230+ }
231+ }
232+ return [ ...argTypeNames ] ;
224233}
0 commit comments