33namespace Drupal \graphql \Plugin \GraphQL \Interfaces ;
44
55use Drupal \Component \Plugin \PluginBase ;
6+ use Drupal \Core \Cache \Cache ;
7+ use Drupal \graphql \Plugin \GraphQL \Traits \CacheablePluginTrait ;
68use Drupal \graphql \Plugin \GraphQL \Traits \DescribablePluginTrait ;
79use Drupal \graphql \Plugin \SchemaBuilderInterface ;
810use Drupal \graphql \Plugin \TypePluginInterface ;
911use Drupal \graphql \Plugin \TypePluginManager ;
1012use GraphQL \Type \Definition \InterfaceType ;
1113
1214abstract class InterfacePluginBase extends PluginBase implements TypePluginInterface {
15+ use CacheablePluginTrait;
1316 use DescribablePluginTrait;
1417
1518 /**
@@ -19,6 +22,20 @@ public static function createInstance(SchemaBuilderInterface $builder, TypePlugi
1922 return new InterfaceType ([
2023 'name ' => $ definition ['name ' ],
2124 'description ' => $ definition ['description ' ],
25+ 'contexts ' => function () use ($ builder , $ definition ) {
26+ $ types = $ builder ->getSubTypes ($ definition ['name ' ]);
27+
28+ return array_reduce ($ types , function ($ carry , $ current ) use ($ builder ) {
29+ $ type = $ builder ->getType ($ current );
30+ if (!empty ($ type ->config ['contexts ' ])) {
31+ $ contexts = $ type ->config ['contexts ' ];
32+ $ contexts = is_callable ($ contexts ) ? $ contexts () : $ contexts ;
33+ return Cache::mergeContexts ($ carry , $ contexts );
34+ }
35+
36+ return $ carry ;
37+ }, $ definition ['contexts ' ]);
38+ },
2239 'fields ' => function () use ($ builder , $ definition ) {
2340 $ fields = $ builder ->getFields ($ definition ['name ' ]);
2441
@@ -49,6 +66,7 @@ public function getDefinition() {
4966 'name ' => $ definition ['name ' ],
5067 'description ' => $ this ->buildDescription ($ definition ),
5168 'interfaces ' => $ this ->buildInterfaces ($ definition ),
69+ 'contexts ' => $ this ->buildCacheContexts ($ definition ),
5270 ];
5371 }
5472
0 commit comments