@@ -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,37 @@ 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,
255
265
} ) ;
256
266
267
+ function makeAppliedDirectives ( appliedDirectives : ?Array < DirectiveNode > ) {
268
+ return appliedDirectives && new GraphQLAppliedDirectives ( keyValMap (
269
+ appliedDirectives ,
270
+ directive => directive . name . value ,
271
+ directive => ( ( ) => {
272
+ const directiveName = directive . name . value ;
273
+ if ( ! innerDirectivesMap [ directiveName ] ) {
274
+ throw new Error (
275
+ `Directive "${ directiveName } " not found in document.` ) ;
276
+ }
277
+ return getArgumentValues ( innerDirectivesMap [ directiveName ] , directive ) ;
278
+ } )
279
+ ) ) ;
280
+ }
281
+
257
282
// Below are functions used for producing this schema that have closed over
258
283
// this scope and have access to the schema, cache, and newly defined types.
259
284
@@ -353,6 +378,7 @@ export function extendSchema(
353
378
description : type . description ,
354
379
interfaces : ( ) => extendImplementedInterfaces ( type ) ,
355
380
fields : ( ) => extendFieldMap ( type ) ,
381
+ appliedDirectives : type . appliedDirectives ,
356
382
isTypeOf : type . isTypeOf ,
357
383
} ) ;
358
384
}
@@ -364,6 +390,7 @@ export function extendSchema(
364
390
name : type . name ,
365
391
description : type . description ,
366
392
fields : ( ) => extendFieldMap ( type ) ,
393
+ appliedDirectives : type . appliedDirectives ,
367
394
resolveType : type . resolveType ,
368
395
} ) ;
369
396
}
@@ -373,6 +400,7 @@ export function extendSchema(
373
400
name : type . name ,
374
401
description : type . description ,
375
402
types : type . getTypes ( ) . map ( getTypeFromDef ) ,
403
+ appliedDirectives : type . appliedDirectives ,
376
404
resolveType : type . resolveType ,
377
405
} ) ;
378
406
}
@@ -413,6 +441,7 @@ export function extendSchema(
413
441
deprecationReason : field . deprecationReason ,
414
442
type : extendFieldType ( field . type ) ,
415
443
args : keyMap ( field . args , arg => arg . name ) ,
444
+ appliedDirectives : field . appliedDirectives ,
416
445
resolve : field . resolve ,
417
446
} ;
418
447
} ) ;
@@ -435,6 +464,7 @@ export function extendSchema(
435
464
type : buildOutputFieldType ( field . type ) ,
436
465
args : buildInputValues ( field . arguments ) ,
437
466
deprecationReason : getDeprecationReason ( field . directives ) ,
467
+ appliedDirectives : makeAppliedDirectives ( field . directives ) ,
438
468
} ;
439
469
} ) ;
440
470
} ) ;
@@ -471,6 +501,7 @@ export function extendSchema(
471
501
description : getDescription ( typeNode ) ,
472
502
interfaces : ( ) => buildImplementedInterfaces ( typeNode ) ,
473
503
fields : ( ) => buildFieldMap ( typeNode ) ,
504
+ appliedDirectives : makeAppliedDirectives ( typeNode . directives ) ,
474
505
} ) ;
475
506
}
476
507
@@ -479,6 +510,7 @@ export function extendSchema(
479
510
name : typeNode . name . value ,
480
511
description : getDescription ( typeNode ) ,
481
512
fields : ( ) => buildFieldMap ( typeNode ) ,
513
+ appliedDirectives : makeAppliedDirectives ( typeNode . directives ) ,
482
514
resolveType : cannotExecuteExtendedSchema ,
483
515
} ) ;
484
516
}
@@ -488,6 +520,7 @@ export function extendSchema(
488
520
name : typeNode . name . value ,
489
521
description : getDescription ( typeNode ) ,
490
522
types : typeNode . types . map ( getObjectTypeFromAST ) ,
523
+ appliedDirectives : makeAppliedDirectives ( typeNode . directives ) ,
491
524
resolveType : cannotExecuteExtendedSchema ,
492
525
} ) ;
493
526
}
@@ -496,6 +529,7 @@ export function extendSchema(
496
529
return new GraphQLScalarType ( {
497
530
name : typeNode . name . value ,
498
531
description : getDescription ( typeNode ) ,
532
+ appliedDirectives : makeAppliedDirectives ( typeNode . directives ) ,
499
533
serialize : id => id ,
500
534
// Note: validation calls the parse functions to determine if a
501
535
// literal value is correct. Returning null would cause use of custom
@@ -516,8 +550,10 @@ export function extendSchema(
516
550
enumValue => ( {
517
551
description : getDescription ( enumValue ) ,
518
552
deprecationReason : getDeprecationReason ( enumValue . directives ) ,
553
+ appliedDirectives : makeAppliedDirectives ( enumValue . directives ) ,
519
554
} ) ,
520
555
) ,
556
+ appliedDirectives : makeAppliedDirectives ( typeNode . directives ) ,
521
557
} ) ;
522
558
}
523
559
@@ -526,6 +562,7 @@ export function extendSchema(
526
562
name : typeNode . name . value ,
527
563
description : getDescription ( typeNode ) ,
528
564
fields : ( ) => buildInputValues ( typeNode . fields ) ,
565
+ appliedDirectives : makeAppliedDirectives ( typeNode . directives ) ,
529
566
} ) ;
530
567
}
531
568
@@ -556,6 +593,7 @@ export function extendSchema(
556
593
description : getDescription ( field ) ,
557
594
args : buildInputValues ( field . arguments ) ,
558
595
deprecationReason : getDeprecationReason ( field . directives ) ,
596
+ appliedDirectives : makeAppliedDirectives ( field . directives ) ,
559
597
} )
560
598
) ;
561
599
}
@@ -569,7 +607,8 @@ export function extendSchema(
569
607
return {
570
608
type,
571
609
description : getDescription ( value ) ,
572
- defaultValue : valueFromAST ( value . defaultValue , type )
610
+ defaultValue : valueFromAST ( value . defaultValue , type ) ,
611
+ appliedDirectives : makeAppliedDirectives ( value . directives ) ,
573
612
} ;
574
613
}
575
614
) ;
0 commit comments