From 1259e611b6dce21017fbfc26e1108ae26babb0d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dezs=C5=91=20BICZ=C3=93?= Date: Mon, 14 Aug 2023 10:00:37 +0200 Subject: [PATCH] fix(DataProducer): Fix missing cacheability bubble up on entity translations data producer --- .../GraphQL/DataProducer/Entity/EntityTranslation.php | 5 ++++- .../GraphQL/DataProducer/Entity/EntityTranslations.php | 7 +++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/Plugin/GraphQL/DataProducer/Entity/EntityTranslation.php b/src/Plugin/GraphQL/DataProducer/Entity/EntityTranslation.php index 633bdc29d..1a30c65dc 100644 --- a/src/Plugin/GraphQL/DataProducer/Entity/EntityTranslation.php +++ b/src/Plugin/GraphQL/DataProducer/Entity/EntityTranslation.php @@ -8,6 +8,7 @@ use Drupal\Core\Entity\TranslatableInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Session\AccountInterface; +use Drupal\graphql\GraphQL\Execution\FieldContext; use Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -97,10 +98,11 @@ public function __construct(array $configuration, $pluginId, $pluginDefinition, * @param bool|null $access * @param \Drupal\Core\Session\AccountInterface|null $accessUser * @param string|null $accessOperation + * @param \Drupal\graphql\GraphQL\Execution\FieldContext $context * * @return \Drupal\Core\Entity\EntityInterface|null */ - public function resolve(EntityInterface $entity, $language, ?bool $access, ?AccountInterface $accessUser, ?string $accessOperation) { + public function resolve(EntityInterface $entity, $language, ?bool $access, ?AccountInterface $accessUser, ?string $accessOperation, FieldContext $context) { if ($entity instanceof TranslatableInterface && $entity->isTranslatable()) { $entity = $entity->getTranslation($language); $entity->addCacheContexts(["static:language:{$language}"]); @@ -109,6 +111,7 @@ public function resolve(EntityInterface $entity, $language, ?bool $access, ?Acco if ($access) { /** @var \Drupal\Core\Access\AccessResultInterface $accessResult */ $accessResult = $entity->access($accessOperation, $accessUser, TRUE); + $context->addCacheableDependency($accessResult); if (!$accessResult->isAllowed()) { return NULL; } diff --git a/src/Plugin/GraphQL/DataProducer/Entity/EntityTranslations.php b/src/Plugin/GraphQL/DataProducer/Entity/EntityTranslations.php index 5a68d8e89..c78ac6b4b 100644 --- a/src/Plugin/GraphQL/DataProducer/Entity/EntityTranslations.php +++ b/src/Plugin/GraphQL/DataProducer/Entity/EntityTranslations.php @@ -9,6 +9,7 @@ use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Session\AccountInterface; +use Drupal\graphql\GraphQL\Execution\FieldContext; use Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -96,20 +97,22 @@ public function __construct(array $configuration, $pluginId, $pluginDefinition, * @param bool|null $access * @param \Drupal\Core\Session\AccountInterface|null $accessUser * @param string|null $accessOperation + * @param \Drupal\graphql\GraphQL\Execution\FieldContext $context * * @return array|null */ - public function resolve(EntityInterface $entity, ?bool $access, ?AccountInterface $accessUser, ?string $accessOperation) { + public function resolve(EntityInterface $entity, ?bool $access, ?AccountInterface $accessUser, ?string $accessOperation, FieldContext $context) { if ($entity instanceof TranslatableInterface && $entity->isTranslatable()) { $languages = $entity->getTranslationLanguages(); - return array_map(function (LanguageInterface $language) use ($entity, $access, $accessOperation, $accessUser) { + return array_map(function (LanguageInterface $language) use ($entity, $access, $accessOperation, $accessUser, $context) { $langcode = $language->getId(); $entity = $entity->getTranslation($langcode); $entity->addCacheContexts(["static:language:{$langcode}"]); if ($access) { /** @var \Drupal\Core\Access\AccessResultInterface $accessResult */ $accessResult = $entity->access($accessOperation, $accessUser, TRUE); + $context->addCacheableDependency($accessResult); if (!$accessResult->isAllowed()) { return NULL; }