Skip to content

Commit

Permalink
Merge pull request #245 from magento-arcticfoxes/B2B-2677-prehydration
Browse files Browse the repository at this point in the history
B2 b 2677 prehydration
  • Loading branch information
joanhe authored Jun 20, 2023
2 parents b00dc07 + 1d7643d commit 35b5c11
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@ public function __construct(
public function dehydrate(array &$resolvedValue): void
{
if (count($resolvedValue) > 0) {
$keys = array_keys($resolvedValue);
$firstKey = array_pop($keys);
$firstKey = array_key_first($resolvedValue);
$this->dehydrateMediaGalleryEntity($resolvedValue[$firstKey]);
foreach ($keys as $key) {
$resolvedValue[$key]['model_info'] = &$resolvedValue[$firstKey]['model_info'];
foreach ($resolvedValue as $key => &$value) {
if ($key !== $firstKey) {
unset($value['model']);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
use Magento\Catalog\Model\ProductFactory;
use Magento\Framework\EntityManager\HydratorPool;
use Magento\GraphQlResolverCache\Model\Resolver\Result\HydratorInterface;
use Magento\GraphQlResolverCache\Model\Resolver\Result\PrehydratorInterface;

/**
* Product resolver data hydrator to rehydrate propagated model.
*/
class ProductModelHydrator implements HydratorInterface
class ProductModelHydrator implements HydratorInterface, PrehydratorInterface
{
/**
* @var ProductFactory
Expand Down Expand Up @@ -62,4 +63,15 @@ public function hydrate(array &$resolverData): void
unset($resolverData['model_info']);
}
}

/**
* @inheritDoc
*/
public function prehydrate(array &$resolverData): void
{
$firstKey = array_key_first($resolverData);
foreach ($resolverData as &$value) {
$value['model_info'] = &$resolverData[$firstKey]['model_info'];
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
/**
* Composite hydrator for resolver result data.
*/
class HydratorComposite implements HydratorInterface
class HydratorComposite implements HydratorInterface, PrehydratorInterface
{
/**
* @var HydratorInterface[]
* @var HydratorInterface[]|PrehydratorInterface[]
*/
private array $hydrators = [];

/**
* @param HydratorInterface[] $hydrators
* @param HydratorInterface[]|PrehydratorInterface[] $hydrators
*/
public function __construct(array $hydrators = [])
{
Expand All @@ -34,7 +34,24 @@ public function hydrate(array &$resolverData): void
return;
}
foreach ($this->hydrators as $hydrator) {
$hydrator->hydrate($resolverData);
if ($hydrator instanceof HydratorInterface) {
$hydrator->hydrate($resolverData);
}
}
}

/**
* @inheritDoc
*/
public function prehydrate(array &$resolverData): void
{
if (empty($resolverData)) {
return;
}
foreach ($this->hydrators as $hydrator) {
if ($hydrator instanceof PrehydratorInterface) {
$hydrator->prehydrate($resolverData);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
interface HydratorInterface
{
/**
* Hydrate resolved data.
* Hydrates resolved data before passing to child resolver.
*
* @param array $resolverData
* @return void
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\GraphQlResolverCache\Model\Resolver\Result;

/**
* Prehydrator interface for resolver data.
*/
interface PrehydratorInterface
{
/**
* Pre-hydrates the whole cached record right after cache read.
*
* @param array $resolverData
* @return void
*/
public function prehydrate(array &$resolverData): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public function processCachedValueAfterLoad(
$hydrator = $this->hydratorProvider->getHydratorForResolver($resolver);
if ($hydrator) {
$this->hydrators[$cacheKey] = $hydrator;
$hydrator->prehydrate($value);
$this->getFlagSetterForType($info)->setFlagOnValue($value, $cacheKey);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Magento\GraphQlResolverCache\Model\Resolver\Result;

use Magento\CatalogGraphQl\Model\Resolver\Cache\Product\MediaGallery\ProductModelHydrator;
use Magento\Framework\DataObject;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\StoreGraphQl\Model\Resolver\StoreConfigResolver;
Expand Down Expand Up @@ -102,10 +103,10 @@ public function testHydratorChainProvider()
unset($resolverData['model']);
});

$testModelHydrator = $this->getMockBuilder(HydratorInterface::class)
$testModelHydrator = $this->getMockBuilder(ProductModelHydrator::class)
->disableOriginalConstructor()
->setMockClassName('TestResolverModelHydrator')
->onlyMethods(['hydrate'])
->onlyMethods(['hydrate', 'prehydrate'])
->getMock();
$testModelHydrator->expects($this->once())
->method('hydrate')
Expand Down Expand Up @@ -154,6 +155,7 @@ public function testHydratorChainProvider()

$this->objectManager->removeSharedInstance('TestResolverModelHydrator');
$this->objectManager->removeSharedInstance('TestResolverNestedItemsHydrator');
$this->objectManager->removeSharedInstance('TestResolverModelDehydrator');
}

/**
Expand Down

0 comments on commit 35b5c11

Please sign in to comment.