Skip to content

Commit 7536b39

Browse files
committed
Improve cache handling of data producers.
1 parent 11164ee commit 7536b39

2 files changed

Lines changed: 28 additions & 11 deletions

File tree

src/Entity/Server.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,11 @@ protected function getFieldResolver(ResolverRegistryInterface $registry) {
273273
return function ($value, $args, ResolveContext $context, ResolveInfo $info) use ($registry) {
274274
$field = new FieldContext($context, $info);
275275
$result = $registry->resolveField($value, $args, $context, $info, $field);
276-
return DeferredUtility::applyFinally($result, function () use ($field, $context) {
276+
return DeferredUtility::applyFinally($result, function ($result) use ($field, $context) {
277+
if ($result instanceof CacheableDependencyInterface) {
278+
$field->addCacheableDependency($result);
279+
}
280+
277281
$context->addCacheableDependency($field);
278282
});
279283
};

src/Plugin/GraphQL/DataProducer/DataProducerProxy.php

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Drupal\graphql\Plugin\GraphQL\DataProducer;
44

55
use Drupal\Core\Cache\Cache;
6+
use Drupal\Core\Cache\CacheableDependencyInterface;
67
use Drupal\Core\Cache\CacheableMetadata;
78
use Drupal\Core\Cache\CacheBackendInterface;
89
use Drupal\Core\Cache\Context\CacheContextsManager;
@@ -226,10 +227,6 @@ protected function resolveCached(DataProducerPluginCachingInterface $plugin, Res
226227

227228
$output = $this->resolveUncached($plugin, $context, $field);
228229
return DeferredUtility::applyFinally($output, function ($value) use ($context, $field, $prefix) {
229-
if ($field->getCacheMaxAge() === 0) {
230-
return;
231-
}
232-
233230
$this->cacheWrite($prefix, $value, $field);
234231
});
235232
}
@@ -276,16 +273,32 @@ protected function cacheRead($prefix) {
276273
* @param \Drupal\graphql\GraphQL\Execution\FieldContext $field
277274
*/
278275
protected function cacheWrite($prefix, $value, FieldContext $field) {
279-
$expire = $this->maxAgeToExpire($field->getCacheMaxAge());
280-
$tags = $field->getCacheTags();
281-
$tokens = $field->getCacheContexts();
282-
283-
$keys = !empty($tokens) ? $this->contextsManager->convertTokensToKeys($tokens)->getKeys() : [];
284-
$keys = serialize($keys);
276+
// Bail out early if the field context is already uncacheable.
277+
if ($field->getCacheMaxAge() === 0) {
278+
return;
279+
}
285280

286281
$metadata = new CacheableMetadata();
287282
$metadata->addCacheableDependency($field);
288283

284+
// Do not add the cache contexts from the result value because they are not
285+
// known at fetch time and would render the written cache unusable.
286+
if ($value instanceof CacheableDependencyInterface) {
287+
$metadata->addCacheTags($value->getCacheTags());
288+
$metadata->mergeCacheMaxAge($value->getCacheMaxAge());
289+
}
290+
291+
if ($metadata->getCacheMaxAge() === 0) {
292+
return;
293+
}
294+
295+
$expire = $this->maxAgeToExpire($metadata->getCacheMaxAge());
296+
$tags = $metadata->getCacheTags();
297+
$tokens = $metadata->getCacheContexts();
298+
299+
$keys = !empty($tokens) ? $this->contextsManager->convertTokensToKeys($tokens)->getKeys() : [];
300+
$keys = serialize($keys);
301+
289302
$this->cacheBackend->setMultiple([
290303
"$prefix:context" => [
291304
'data' => $tokens,

0 commit comments

Comments
 (0)