@@ -28,6 +28,7 @@ import {
28
28
GraphQLScalarType ,
29
29
GraphQLEnumType ,
30
30
GraphQLInputObjectType ,
31
+ GraphQLAppliedDirectives ,
31
32
isInputType ,
32
33
isOutputType ,
33
34
} from '../type/definition' ;
@@ -82,6 +83,7 @@ import type {
82
83
83
84
import type {
84
85
DocumentNode ,
86
+ DirectiveNode ,
85
87
InputValueDefinitionNode ,
86
88
TypeNode ,
87
89
NamedTypeNode ,
@@ -95,6 +97,7 @@ import type {
95
97
DirectiveDefinitionNode ,
96
98
} from '../language/ast' ;
97
99
100
+ import { getArgumentValues } from '../execution/values' ;
98
101
99
102
/**
100
103
* Produces a new schema given an existing schema and a document which may
@@ -245,15 +248,38 @@ export function extendSchema(
245
248
types . push ( getTypeFromAST ( typeDefinitionMap [ typeName ] ) ) ;
246
249
} ) ;
247
250
251
+ const directives = getMergedDirectives ( ) ;
252
+ const innerDirectivesMap = keyValMap (
253
+ directives ,
254
+ directive => directive . name ,
255
+ directive => directive
256
+ ) ;
257
+
248
258
// Then produce and return a Schema with these types.
249
259
return new GraphQLSchema ( {
250
260
query : queryType ,
251
261
mutation : mutationType ,
252
262
subscription : subscriptionType ,
253
263
types,
254
- directives : getMergedDirectives ( ) ,
264
+ directives,
265
+ appliedDirectives : schema . getAppliedDirectives ( ) ,
255
266
} ) ;
256
267
268
+ function makeAppliedDirectives ( appliedDirectives : ?Array < DirectiveNode > ) {
269
+ return appliedDirectives && new GraphQLAppliedDirectives ( keyValMap (
270
+ appliedDirectives ,
271
+ directive => directive . name . value ,
272
+ directive => ( ( ) => {
273
+ const directiveName = directive . name . value ;
274
+ if ( ! innerDirectivesMap [ directiveName ] ) {
275
+ throw new Error (
276
+ `Directive "${ directiveName } " not found in document.` ) ;
277
+ }
278
+ return getArgumentValues ( innerDirectivesMap [ directiveName ] , directive ) ;
279
+ } )
280
+ ) ) ;
281
+ }
282
+
257
283
// Below are functions used for producing this schema that have closed over
258
284
// this scope and have access to the schema, cache, and newly defined types.
259
285
@@ -353,6 +379,7 @@ export function extendSchema(
353
379
description : type . description ,
354
380
interfaces : ( ) => extendImplementedInterfaces ( type ) ,
355
381
fields : ( ) => extendFieldMap ( type ) ,
382
+ appliedDirectives : type . appliedDirectives ,
356
383
isTypeOf : type . isTypeOf ,
357
384
} ) ;
358
385
}
@@ -364,6 +391,7 @@ export function extendSchema(
364
391
name : type . name ,
365
392
description : type . description ,
366
393
fields : ( ) => extendFieldMap ( type ) ,
394
+ appliedDirectives : type . appliedDirectives ,
367
395
resolveType : type . resolveType ,
368
396
} ) ;
369
397
}
@@ -373,6 +401,7 @@ export function extendSchema(
373
401
name : type . name ,
374
402
description : type . description ,
375
403
types : type . getTypes ( ) . map ( getTypeFromDef ) ,
404
+ appliedDirectives : type . appliedDirectives ,
376
405
resolveType : type . resolveType ,
377
406
} ) ;
378
407
}
@@ -413,6 +442,7 @@ export function extendSchema(
413
442
deprecationReason : field . deprecationReason ,
414
443
type : extendFieldType ( field . type ) ,
415
444
args : keyMap ( field . args , arg => arg . name ) ,
445
+ appliedDirectives : field . appliedDirectives ,
416
446
resolve : field . resolve ,
417
447
} ;
418
448
} ) ;
@@ -435,6 +465,7 @@ export function extendSchema(
435
465
type : buildOutputFieldType ( field . type ) ,
436
466
args : buildInputValues ( field . arguments ) ,
437
467
deprecationReason : getDeprecationReason ( field . directives ) ,
468
+ appliedDirectives : makeAppliedDirectives ( field . directives ) ,
438
469
} ;
439
470
} ) ;
440
471
} ) ;
@@ -471,6 +502,7 @@ export function extendSchema(
471
502
description : getDescription ( typeNode ) ,
472
503
interfaces : ( ) => buildImplementedInterfaces ( typeNode ) ,
473
504
fields : ( ) => buildFieldMap ( typeNode ) ,
505
+ appliedDirectives : makeAppliedDirectives ( typeNode . directives ) ,
474
506
} ) ;
475
507
}
476
508
@@ -479,6 +511,7 @@ export function extendSchema(
479
511
name : typeNode . name . value ,
480
512
description : getDescription ( typeNode ) ,
481
513
fields : ( ) => buildFieldMap ( typeNode ) ,
514
+ appliedDirectives : makeAppliedDirectives ( typeNode . directives ) ,
482
515
resolveType : cannotExecuteExtendedSchema ,
483
516
} ) ;
484
517
}
@@ -488,6 +521,7 @@ export function extendSchema(
488
521
name : typeNode . name . value ,
489
522
description : getDescription ( typeNode ) ,
490
523
types : typeNode . types . map ( getObjectTypeFromAST ) ,
524
+ appliedDirectives : makeAppliedDirectives ( typeNode . directives ) ,
491
525
resolveType : cannotExecuteExtendedSchema ,
492
526
} ) ;
493
527
}
@@ -496,6 +530,7 @@ export function extendSchema(
496
530
return new GraphQLScalarType ( {
497
531
name : typeNode . name . value ,
498
532
description : getDescription ( typeNode ) ,
533
+ appliedDirectives : makeAppliedDirectives ( typeNode . directives ) ,
499
534
serialize : id => id ,
500
535
// Note: validation calls the parse functions to determine if a
501
536
// literal value is correct. Returning null would cause use of custom
@@ -516,8 +551,10 @@ export function extendSchema(
516
551
enumValue => ( {
517
552
description : getDescription ( enumValue ) ,
518
553
deprecationReason : getDeprecationReason ( enumValue . directives ) ,
554
+ appliedDirectives : makeAppliedDirectives ( enumValue . directives ) ,
519
555
} ) ,
520
556
) ,
557
+ appliedDirectives : makeAppliedDirectives ( typeNode . directives ) ,
521
558
} ) ;
522
559
}
523
560
@@ -526,6 +563,7 @@ export function extendSchema(
526
563
name : typeNode . name . value ,
527
564
description : getDescription ( typeNode ) ,
528
565
fields : ( ) => buildInputValues ( typeNode . fields ) ,
566
+ appliedDirectives : makeAppliedDirectives ( typeNode . directives ) ,
529
567
} ) ;
530
568
}
531
569
@@ -556,6 +594,7 @@ export function extendSchema(
556
594
description : getDescription ( field ) ,
557
595
args : buildInputValues ( field . arguments ) ,
558
596
deprecationReason : getDeprecationReason ( field . directives ) ,
597
+ appliedDirectives : makeAppliedDirectives ( field . directives ) ,
559
598
} )
560
599
) ;
561
600
}
@@ -569,7 +608,8 @@ export function extendSchema(
569
608
return {
570
609
type,
571
610
description : getDescription ( value ) ,
572
- defaultValue : valueFromAST ( value . defaultValue , type )
611
+ defaultValue : valueFromAST ( value . defaultValue , type ) ,
612
+ appliedDirectives : makeAppliedDirectives ( value . directives ) ,
573
613
} ;
574
614
}
575
615
) ;
0 commit comments