@@ -28,6 +28,7 @@ import {
28
28
GraphQLScalarType ,
29
29
GraphQLEnumType ,
30
30
GraphQLInputObjectType ,
31
+ GraphQLAppliedDirectives ,
31
32
assertInputType ,
32
33
assertOutputType ,
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
@@ -349,6 +375,7 @@ export function extendSchema(
349
375
description : type . description ,
350
376
interfaces : ( ) => extendImplementedInterfaces ( type ) ,
351
377
fields : ( ) => extendFieldMap ( type ) ,
378
+ appliedDirectives : type . appliedDirectives ,
352
379
isTypeOf : type . isTypeOf ,
353
380
} ) ;
354
381
}
@@ -360,6 +387,7 @@ export function extendSchema(
360
387
name : type . name ,
361
388
description : type . description ,
362
389
fields : ( ) => extendFieldMap ( type ) ,
390
+ appliedDirectives : type . appliedDirectives ,
363
391
resolveType : type . resolveType ,
364
392
} ) ;
365
393
}
@@ -369,6 +397,7 @@ export function extendSchema(
369
397
name : type . name ,
370
398
description : type . description ,
371
399
types : type . getTypes ( ) . map ( getTypeFromDef ) ,
400
+ appliedDirectives : type . appliedDirectives ,
372
401
resolveType : type . resolveType ,
373
402
} ) ;
374
403
}
@@ -409,6 +438,7 @@ export function extendSchema(
409
438
deprecationReason : field . deprecationReason ,
410
439
type : extendFieldType ( field . type ) ,
411
440
args : keyMap ( field . args , arg => arg . name ) ,
441
+ appliedDirectives : field . appliedDirectives ,
412
442
resolve : field . resolve ,
413
443
} ;
414
444
} ) ;
@@ -431,6 +461,7 @@ export function extendSchema(
431
461
type : buildOutputFieldType ( field . type ) ,
432
462
args : buildInputValues ( field . arguments ) ,
433
463
deprecationReason : getDeprecationReason ( field . directives ) ,
464
+ appliedDirectives : makeAppliedDirectives ( field . directives ) ,
434
465
} ;
435
466
} ) ;
436
467
} ) ;
@@ -467,6 +498,7 @@ export function extendSchema(
467
498
description : getDescription ( typeNode ) ,
468
499
interfaces : ( ) => buildImplementedInterfaces ( typeNode ) ,
469
500
fields : ( ) => buildFieldMap ( typeNode ) ,
501
+ appliedDirectives : makeAppliedDirectives ( typeNode . directives ) ,
470
502
} ) ;
471
503
}
472
504
@@ -475,6 +507,7 @@ export function extendSchema(
475
507
name : typeNode . name . value ,
476
508
description : getDescription ( typeNode ) ,
477
509
fields : ( ) => buildFieldMap ( typeNode ) ,
510
+ appliedDirectives : makeAppliedDirectives ( typeNode . directives ) ,
478
511
resolveType : cannotExecuteExtendedSchema ,
479
512
} ) ;
480
513
}
@@ -484,6 +517,7 @@ export function extendSchema(
484
517
name : typeNode . name . value ,
485
518
description : getDescription ( typeNode ) ,
486
519
types : typeNode . types . map ( getObjectTypeFromAST ) ,
520
+ appliedDirectives : makeAppliedDirectives ( typeNode . directives ) ,
487
521
resolveType : cannotExecuteExtendedSchema ,
488
522
} ) ;
489
523
}
@@ -492,6 +526,7 @@ export function extendSchema(
492
526
return new GraphQLScalarType ( {
493
527
name : typeNode . name . value ,
494
528
description : getDescription ( typeNode ) ,
529
+ appliedDirectives : makeAppliedDirectives ( typeNode . directives ) ,
495
530
serialize : id => id ,
496
531
// Note: validation calls the parse functions to determine if a
497
532
// literal value is correct. Returning null would cause use of custom
@@ -512,8 +547,10 @@ export function extendSchema(
512
547
enumValue => ( {
513
548
description : getDescription ( enumValue ) ,
514
549
deprecationReason : getDeprecationReason ( enumValue . directives ) ,
550
+ appliedDirectives : makeAppliedDirectives ( enumValue . directives ) ,
515
551
} ) ,
516
552
) ,
553
+ appliedDirectives : makeAppliedDirectives ( typeNode . directives ) ,
517
554
} ) ;
518
555
}
519
556
@@ -522,6 +559,7 @@ export function extendSchema(
522
559
name : typeNode . name . value ,
523
560
description : getDescription ( typeNode ) ,
524
561
fields : ( ) => buildInputValues ( typeNode . fields ) ,
562
+ appliedDirectives : makeAppliedDirectives ( typeNode . directives ) ,
525
563
} ) ;
526
564
}
527
565
@@ -552,6 +590,7 @@ export function extendSchema(
552
590
description : getDescription ( field ) ,
553
591
args : buildInputValues ( field . arguments ) ,
554
592
deprecationReason : getDeprecationReason ( field . directives ) ,
593
+ appliedDirectives : makeAppliedDirectives ( field . directives ) ,
555
594
} )
556
595
) ;
557
596
}
@@ -565,7 +604,8 @@ export function extendSchema(
565
604
return {
566
605
type,
567
606
description : getDescription ( value ) ,
568
- defaultValue : valueFromAST ( value . defaultValue , type )
607
+ defaultValue : valueFromAST ( value . defaultValue , type ) ,
608
+ appliedDirectives : makeAppliedDirectives ( value . directives ) ,
569
609
} ;
570
610
}
571
611
) ;
0 commit comments