@@ -31,6 +31,7 @@ import {
31
31
MapperKind ,
32
32
getDirectives ,
33
33
ExecutionResult ,
34
+ getDirective ,
34
35
} from '@graphql-tools/utils' ;
35
36
36
37
import { addMocksToSchema } from '@graphql-tools/mock' ;
@@ -122,8 +123,8 @@ describe('@directives', () => {
122
123
return schema => mapSchema ( schema , {
123
124
[ MapperKind . OBJECT_TYPE ] : type => {
124
125
const directives = getDirectives ( schema , type ) ;
125
- for ( const directiveName in directives ) {
126
- if ( directiveNames . includes ( directiveName ) ) {
126
+ for ( const directive of directives ) {
127
+ if ( directiveNames . includes ( directive . name ) ) {
127
128
expect ( type . name ) . toBe ( schema . getQueryType ( ) ?. name ) ;
128
129
visited . add ( type ) ;
129
130
}
@@ -149,8 +150,8 @@ describe('@directives', () => {
149
150
function recordSchemaDirectiveUses ( directiveNames : Array < string > ) : ( schema : GraphQLSchema ) => GraphQLSchema {
150
151
return schema => {
151
152
const directives = getDirectives ( schema , schema ) ;
152
- for ( const directiveName in directives ) {
153
- if ( directiveNames . includes ( directiveName ) ) {
153
+ for ( const directive of directives ) {
154
+ if ( directiveNames . includes ( directive . name ) ) {
154
155
visited . push ( schema ) ;
155
156
}
156
157
}
@@ -179,8 +180,8 @@ describe('@directives', () => {
179
180
upperDirectiveTypeDefs : `directive @${ directiveName } on FIELD_DEFINITION` ,
180
181
upperDirectiveTransformer : ( schema : GraphQLSchema ) => mapSchema ( schema , {
181
182
[ MapperKind . OBJECT_FIELD ] : ( fieldConfig ) => {
182
- const directives = getDirectives ( schema , fieldConfig ) ;
183
- if ( directives [ directiveName ] ) {
183
+ const upperDirective = getDirective ( schema , fieldConfig , directiveName ) ?. [ 0 ] ;
184
+ if ( upperDirective ) {
184
185
const { resolve = defaultFieldResolver } = fieldConfig ;
185
186
fieldConfig . resolve = async function ( source , args , context , info ) {
186
187
const result = await resolve ( source , args , context , info ) ;
@@ -234,18 +235,16 @@ describe('@directives', () => {
234
235
deprecatedDirectiveTypeDefs : `directive @${ directiveName } (reason: String) on FIELD_DEFINITION | ENUM_VALUE` ,
235
236
deprecatedDirectiveTransformer : ( schema : GraphQLSchema ) => mapSchema ( schema , {
236
237
[ MapperKind . OBJECT_FIELD ] : ( fieldConfig ) => {
237
- const directives = getDirectives ( schema , fieldConfig ) ;
238
- const directiveArgumentMap = directives [ directiveName ] ;
239
- if ( directiveArgumentMap ) {
240
- fieldConfig . deprecationReason = directiveArgumentMap . reason ;
238
+ const deprecatedDirective = getDirective ( schema , fieldConfig , directiveName ) ?. [ 0 ] ;
239
+ if ( deprecatedDirective ) {
240
+ fieldConfig . deprecationReason = deprecatedDirective [ 'reason' ] ;
241
241
return fieldConfig ;
242
242
}
243
243
} ,
244
244
[ MapperKind . ENUM_VALUE ] : ( enumValueConfig ) => {
245
- const directives = getDirectives ( schema , enumValueConfig ) ;
246
- const directiveArgumentMap = directives [ directiveName ] ;
247
- if ( directiveArgumentMap ) {
248
- enumValueConfig . deprecationReason = directiveArgumentMap . reason ;
245
+ const deprecatedDirective = getDirective ( schema , enumValueConfig , directiveName ) ?. [ 0 ] ;
246
+ if ( deprecatedDirective ) {
247
+ enumValueConfig . deprecationReason = deprecatedDirective [ 'reason' ] ;
249
248
return enumValueConfig ;
250
249
}
251
250
}
@@ -278,16 +277,14 @@ describe('@directives', () => {
278
277
dateDirectiveTypeDefs : `directive @${ directiveName } (format: String) on FIELD_DEFINITION` ,
279
278
dateDirectiveTransformer : ( schema : GraphQLSchema ) => mapSchema ( schema , {
280
279
[ MapperKind . OBJECT_FIELD ] : ( fieldConfig ) => {
281
- const directives = getDirectives ( schema , fieldConfig ) ;
282
- const directiveArgumentMap = directives [ directiveName ] ;
283
- if ( directiveArgumentMap ) {
280
+ const dateDirective = getDirective ( schema , fieldConfig , directiveName ) ?. [ 0 ] ;
281
+ if ( dateDirective ) {
284
282
const { resolve = defaultFieldResolver } = fieldConfig ;
285
- const { format } = directiveArgumentMap ;
286
- fieldConfig . resolve = async function ( source , args , context , info ) {
283
+ const { format } = dateDirective ;
284
+ fieldConfig . resolve = async ( source , args , context , info ) => {
287
285
const date = await resolve ( source , args , context , info ) ;
288
286
return formatDate ( date , format , true ) ;
289
-
290
- }
287
+ } ;
291
288
return fieldConfig ;
292
289
}
293
290
}
@@ -338,11 +335,10 @@ describe('@directives', () => {
338
335
` ,
339
336
formattableDateDirectiveTransformer : ( schema : GraphQLSchema ) => mapSchema ( schema , {
340
337
[ MapperKind . OBJECT_FIELD ] : ( fieldConfig ) => {
341
- const directives = getDirectives ( schema , fieldConfig ) ;
342
- const directiveArgumentMap = directives [ directiveName ] ;
343
- if ( directiveArgumentMap ) {
338
+ const dateDirective = getDirective ( schema , fieldConfig , directiveName ) ?. [ 0 ] ;
339
+ if ( dateDirective ) {
344
340
const { resolve = defaultFieldResolver } = fieldConfig ;
345
- const { defaultFormat } = directiveArgumentMap ;
341
+ const { defaultFormat } = dateDirective ;
346
342
347
343
if ( ! fieldConfig . args ) {
348
344
throw new Error ( "Unexpected Error. args should be defined." )
@@ -353,12 +349,12 @@ describe('@directives', () => {
353
349
} ;
354
350
355
351
fieldConfig . type = GraphQLString ;
356
- fieldConfig . resolve = async function (
352
+ fieldConfig . resolve = async (
357
353
source ,
358
354
{ format, ...args } ,
359
355
context ,
360
356
info ,
361
- ) {
357
+ ) => {
362
358
const newFormat = format || defaultFormat ;
363
359
const date = await resolve ( source , args , context , info ) ;
364
360
return formatDate ( date , newFormat , true ) ;
@@ -430,15 +426,16 @@ describe('@directives', () => {
430
426
}` ,
431
427
authDirectiveTransformer : ( schema : GraphQLSchema ) => mapSchema ( schema , {
432
428
[ MapperKind . TYPE ] : ( type ) => {
433
- const typeDirectives = getDirectives ( schema , type ) ;
434
- typeDirectiveArgumentMaps [ type . name ] = typeDirectives [ directiveName ] ;
429
+ const authDirective = getDirective ( schema , type , directiveName ) ?. [ 0 ] ;
430
+ if ( authDirective ) {
431
+ typeDirectiveArgumentMaps [ type . name ] = authDirective ;
432
+ }
435
433
return undefined ;
436
434
} ,
437
435
[ MapperKind . OBJECT_FIELD ] : ( fieldConfig , _fieldName , typeName ) => {
438
- const fieldDirectives = getDirectives ( schema , fieldConfig ) ;
439
- const directiveArgumentMap = fieldDirectives [ directiveName ] ?? typeDirectiveArgumentMaps [ typeName ] ;
440
- if ( directiveArgumentMap ) {
441
- const { requires } = directiveArgumentMap ;
436
+ const authDirective = getDirective ( schema , fieldConfig , directiveName ) ?. [ 0 ] ?? typeDirectiveArgumentMaps [ typeName ] ;
437
+ if ( authDirective ) {
438
+ const { requires } = authDirective ;
442
439
if ( requires ) {
443
440
const { resolve = defaultFieldResolver } = fieldConfig ;
444
441
fieldConfig . resolve = function ( source , args , context , info ) {
@@ -626,10 +623,9 @@ describe('@directives', () => {
626
623
lengthDirectiveTypeDefs : `directive @${ directiveName } (max: Int) on FIELD_DEFINITION | INPUT_FIELD_DEFINITION` ,
627
624
lengthDirectiveTransformer : ( schema : GraphQLSchema ) => mapSchema ( schema , {
628
625
[ MapperKind . FIELD ] : ( fieldConfig ) => {
629
- const directives = getDirectives ( schema , fieldConfig ) ;
630
- const directiveArgumentMap = directives [ directiveName ] ;
631
- if ( directiveArgumentMap ) {
632
- wrapType ( fieldConfig , directiveArgumentMap ) ;
626
+ const lengthDirective = getDirective ( schema , fieldConfig , directiveName ) ?. [ 0 ] ;
627
+ if ( lengthDirective ) {
628
+ wrapType ( fieldConfig , lengthDirective ) ;
633
629
return fieldConfig ;
634
630
}
635
631
}
@@ -717,10 +713,9 @@ describe('@directives', () => {
717
713
uniqueIDDirectiveTypeDefs : `directive @${ directiveName } (name: String, from: [String]) on OBJECT` ,
718
714
uniqueIDDirectiveTransformer : ( schema : GraphQLSchema ) => mapSchema ( schema , {
719
715
[ MapperKind . OBJECT_TYPE ] : ( type ) => {
720
- const directives = getDirectives ( schema , type ) ;
721
- const directiveArgumentMap = directives [ directiveName ] ;
722
- if ( directiveArgumentMap ) {
723
- const { name, from } = directiveArgumentMap ;
716
+ const uniqueIDDirective = getDirective ( schema , type , directiveName ) ?. [ 0 ] ;
717
+ if ( uniqueIDDirective ) {
718
+ const { name, from } = uniqueIDDirective ;
724
719
const config = type . toConfig ( ) ;
725
720
config . fields [ name ] = {
726
721
type : GraphQLID ,
@@ -827,9 +822,8 @@ describe('@directives', () => {
827
822
function renameObjectTypeToHumanDirective ( directiveName : string ) : ( schema : GraphQLSchema ) => GraphQLSchema {
828
823
return schema => mapSchema ( schema , {
829
824
[ MapperKind . OBJECT_TYPE ] : ( type ) => {
830
- const directives = getDirectives ( schema , type ) ;
831
- const directiveArgumentMap = directives [ directiveName ] ;
832
- if ( directiveArgumentMap ) {
825
+ const directive = getDirective ( schema , type , directiveName ) ?. [ 0 ] ;
826
+ if ( directive ) {
833
827
const config = type . toConfig ( ) ;
834
828
config . name = 'Human' ;
835
829
return new GraphQLObjectType ( config ) ;
@@ -875,9 +869,8 @@ describe('@directives', () => {
875
869
function removeEnumValueDirective ( directiveName : string ) : ( schema : GraphQLSchema ) => GraphQLSchema {
876
870
return schema => mapSchema ( schema , {
877
871
[ MapperKind . ENUM_VALUE ] : ( enumValueConfig ) => {
878
- const directives = getDirectives ( schema , enumValueConfig ) ;
879
- const directiveArgumentMap = directives [ directiveName ] ;
880
- if ( directiveArgumentMap && directiveArgumentMap . if ) {
872
+ const directive = getDirective ( schema , enumValueConfig , directiveName ) ?. [ 0 ] ;
873
+ if ( directive ?. [ 'if' ] ) {
881
874
return null ;
882
875
}
883
876
}
@@ -912,10 +905,9 @@ describe('@directives', () => {
912
905
function modifyExternalEnumValueDirective ( directiveName : string ) : ( schema : GraphQLSchema ) => GraphQLSchema {
913
906
return schema => mapSchema ( schema , {
914
907
[ MapperKind . ENUM_VALUE ] : ( enumValueConfig ) => {
915
- const directives = getDirectives ( schema , enumValueConfig ) ;
916
- const directiveArgumentMap = directives [ directiveName ] ;
917
- if ( directiveArgumentMap ) {
918
- return [ directiveArgumentMap . new , enumValueConfig ] ;
908
+ const directive = getDirective ( schema , enumValueConfig , directiveName ) ?. [ 0 ] ;
909
+ if ( directive ) {
910
+ return [ directive [ 'new' ] , enumValueConfig ] ;
919
911
}
920
912
}
921
913
} ) ;
@@ -950,10 +942,9 @@ describe('@directives', () => {
950
942
function modifyInternalEnumValueDirective ( directiveName : string ) : ( schema : GraphQLSchema ) => GraphQLSchema {
951
943
return schema => mapSchema ( schema , {
952
944
[ MapperKind . ENUM_VALUE ] : ( enumValueConfig ) => {
953
- const directives = getDirectives ( schema , enumValueConfig ) ;
954
- const directiveArgumentMap = directives [ directiveName ] ;
955
- if ( directiveArgumentMap ) {
956
- enumValueConfig . value = directiveArgumentMap . new ;
945
+ const directive = getDirective ( schema , enumValueConfig , directiveName ) ?. [ 0 ] ;
946
+ if ( directive ) {
947
+ enumValueConfig . value = directive [ 'new' ] ;
957
948
return enumValueConfig ;
958
949
}
959
950
}
@@ -989,11 +980,10 @@ describe('@directives', () => {
989
980
function renameObjectTypeDirective ( directiveName : string ) : ( schema : GraphQLSchema ) => GraphQLSchema {
990
981
return schema => mapSchema ( schema , {
991
982
[ MapperKind . OBJECT_TYPE ] : ( type ) => {
992
- const directives = getDirectives ( schema , type ) ;
993
- const directiveArgumentMap = directives [ directiveName ] ;
994
- if ( directiveArgumentMap ) {
983
+ const directive = getDirective ( schema , type , directiveName ) ?. [ 0 ] ;
984
+ if ( directive ) {
995
985
const config = type . toConfig ( ) ;
996
- config . name = directiveArgumentMap . to ;
986
+ config . name = directive [ 'to' ] ;
997
987
return new GraphQLObjectType ( config ) ;
998
988
}
999
989
}
@@ -1042,9 +1032,8 @@ describe('@directives', () => {
1042
1032
function addObjectTypeToSetDirective ( directiveName : string ) : ( schema : GraphQLSchema ) => GraphQLSchema {
1043
1033
return schema => mapSchema ( schema , {
1044
1034
[ MapperKind . OBJECT_TYPE ] : type => {
1045
- const directives = getDirectives ( schema , type ) ;
1046
- const directiveArgumentMap = directives [ directiveName ] ;
1047
- if ( directiveArgumentMap ) {
1035
+ const directive = getDirective ( schema , type , directiveName ) ?. [ 0 ] ;
1036
+ if ( directive ) {
1048
1037
expect ( type . name ) . toBe ( schema . getQueryType ( ) ?. name ) ;
1049
1038
visited . add ( type ) ;
1050
1039
}
@@ -1072,8 +1061,8 @@ describe('@directives', () => {
1072
1061
function upperDirective ( directiveName : string ) : ( schema : GraphQLSchema ) => GraphQLSchema {
1073
1062
return schema => mapSchema ( schema , {
1074
1063
[ MapperKind . OBJECT_FIELD ] : ( fieldConfig ) => {
1075
- const directives = getDirectives ( schema , fieldConfig ) ;
1076
- if ( directives [ directiveName ] ) {
1064
+ const upperDirective = getDirective ( schema , fieldConfig , directiveName ) ?. [ 0 ] ;
1065
+ if ( upperDirective ) {
1077
1066
const { resolve = defaultFieldResolver } = fieldConfig ;
1078
1067
fieldConfig . resolve = async function ( source , args , context , info ) {
1079
1068
const result = await resolve ( source , args , context , info ) ;
@@ -1091,8 +1080,8 @@ describe('@directives', () => {
1091
1080
function reverseDirective ( directiveName : string ) : ( schema : GraphQLSchema ) => GraphQLSchema {
1092
1081
return schema => mapSchema ( schema , {
1093
1082
[ MapperKind . OBJECT_FIELD ] : ( fieldConfig ) => {
1094
- const directives = getDirectives ( schema , fieldConfig ) ;
1095
- if ( directives [ directiveName ] ) {
1083
+ const reverseDirective = getDirective ( schema , fieldConfig , directiveName ) ?. [ 0 ] ;
1084
+ if ( reverseDirective ) {
1096
1085
const { resolve = defaultFieldResolver } = fieldConfig ;
1097
1086
fieldConfig . resolve = async function ( source , args , context , info ) {
1098
1087
const result = await resolve ( source , args , context , info ) ;
@@ -1145,10 +1134,10 @@ describe('@directives', () => {
1145
1134
const listWrapperTypes = new Map ( ) ;
1146
1135
return mapSchema ( schema , {
1147
1136
[ MapperKind . COMPOSITE_FIELD ] : ( fieldConfig , fieldName ) => {
1148
- const hasDirectiveAnnotation = ! ! getDirectives ( schema , fieldConfig ) [ 'addListWrapper' ] ;
1137
+ const directive = getDirective ( schema , fieldConfig , 'addListWrapper' ) ?. [ 0 ] ;
1149
1138
1150
1139
// Leave the field untouched if it does not have the directive annotation
1151
- if ( ! hasDirectiveAnnotation ) {
1140
+ if ( ! directive ) {
1152
1141
return undefined ;
1153
1142
}
1154
1143
0 commit comments