Skip to content

Commit 1a94a29

Browse files
SpadXIIILuigisa
andauthored
Merge caching for entity fields (#996)
I noticed an issue with missing caching tags when requesting long text fields (wysiwyg editor-enabled) with inline media. The cache tags of the inline media were not available in the response. I traced it down to this file (EntityFieldBase.php) in the graphql_core sub-module. With this small change, the caching tags set by the entity fields will be used in the response. And it solves the issue that a change of an image in a media item does not invalidate the cache of the pages where it is used in the long text fields. Co-authored-by: Luis Gil <luis.gil@biko2.com>
1 parent 9e09c8e commit 1a94a29

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

modules/graphql_core/src/Plugin/GraphQL/Fields/EntityFieldBase.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Drupal\graphql_core\Plugin\GraphQL\Fields;
44

55
use Drupal\Component\Render\MarkupInterface;
6+
use Drupal\Core\Cache\CacheableDependencyInterface;
7+
use Drupal\Core\Cache\CacheableMetadata;
68
use Drupal\Core\Entity\ContentEntityInterface;
79
use Drupal\Core\Entity\Entity;
810
use Drupal\Core\Entity\EntityInterface;
@@ -25,7 +27,8 @@ protected function resolveItem($item, array $args, ResolveContext $context, Reso
2527
if ($item instanceof FieldItemInterface) {
2628
$definition = $this->getPluginDefinition();
2729
$property = $definition['property'];
28-
$result = $item->get($property)->getValue();
30+
$itemProperty = $item->get($property);
31+
$result = $itemProperty->getValue();
2932
$result = $result instanceof MarkupInterface ? $result->__toString() : $result;
3033

3134
$type = $info->returnType;
@@ -40,6 +43,12 @@ protected function resolveItem($item, array $args, ResolveContext $context, Reso
4043
}
4144
}
4245

46+
if ($itemProperty instanceof CacheableDependencyInterface) {
47+
$context->addCacheTags($itemProperty->getCacheTags());
48+
$context->addCacheContexts($itemProperty->getCacheContexts());
49+
$context->mergeCacheMaxAge($itemProperty->getCacheMaxAge());
50+
}
51+
4352
return $result;
4453
}
4554
}

0 commit comments

Comments
 (0)