Skip to content

Commit 11164ee

Browse files
committed
Fixing partial caching.
1 parent 66745eb commit 11164ee

3 files changed

Lines changed: 18 additions & 8 deletions

File tree

src/Plugin/GraphQL/DataProducer/DataProducerPluginCachingTrait.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,23 @@
22

33
namespace Drupal\graphql\Plugin\GraphQL\DataProducer;
44

5+
use Drupal\Core\Entity\EntityInterface;
6+
57
trait DataProducerPluginCachingTrait {
68

79
/**
810
* {@inheritdoc}
911
*/
1012
public function edgeCachePrefix() {
11-
return hash('sha256', serialize($this->getContextValues()));
13+
$contexts = array_map(function ($context) {
14+
if ($context instanceof EntityInterface) {
15+
return $context->uuid();
16+
}
17+
18+
return $context;
19+
}, $this->getContextValues());
20+
21+
return hash('sha256', serialize($contexts));
1222
}
1323

1424
/**

src/Plugin/GraphQL/DataProducer/DataProducerProxy.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -240,17 +240,16 @@ protected function resolveCached(DataProducerPluginCachingInterface $plugin, Res
240240
* @return string
241241
*/
242242
protected function edgeCachePrefix(DataProducerPluginCachingInterface $plugin) {
243-
$id = $plugin->getPluginId();
244-
$keys = $this->contextsManager->convertTokensToKeys($plugin->getCacheContexts())->getKeys();
245-
246243
try {
247-
$vectors = $plugin->edgeCachePrefix();
244+
$prefix = $plugin->edgeCachePrefix();
248245
}
249246
catch (\Exception $e) {
250-
throw new \LogicException(sprintf('Failed to serialize edge cache vectors for plugin %s.', $id));
247+
throw new \LogicException(sprintf('Failed to serialize edge cache vectors for plugin %s.', $plugin->getPluginId()));
251248
}
252249

253-
return md5(serialize([$id, $vectors, $keys]));
250+
$contexts = $plugin->getCacheContexts();
251+
$keys = $this->contextsManager->convertTokensToKeys($contexts)->getKeys();
252+
return md5(serialize([$plugin->getPluginId(), $prefix, $keys]));
254253
}
255254

256255
/**

src/Plugin/GraphQL/DataProducer/Entity/EntityLabel.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Drupal\Core\Entity\EntityInterface;
66
use Drupal\graphql\GraphQL\Execution\ResolveContext;
7+
use Drupal\graphql\Plugin\DataProducerPluginCachingInterface;
78
use Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase;
89
use GraphQL\Type\Definition\ResolveInfo;
910

@@ -22,7 +23,7 @@
2223
* }
2324
* )
2425
*/
25-
class EntityLabel extends DataProducerPluginBase {
26+
class EntityLabel extends DataProducerPluginBase implements DataProducerPluginCachingInterface {
2627

2728
/**
2829
* @param \Drupal\Core\Entity\EntityInterface $entity

0 commit comments

Comments
 (0)