Skip to content

Commit 35b5c11

Browse files
authored
Merge pull request #245 from magento-arcticfoxes/B2B-2677-prehydration
B2 b 2677 prehydration
2 parents b00dc07 + 1d7643d commit 35b5c11

File tree

7 files changed

+67
-12
lines changed

7 files changed

+67
-12
lines changed

app/code/Magento/CatalogGraphQl/Model/Resolver/Cache/Product/MediaGallery/ProductModelDehydrator.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,12 @@ public function __construct(
4545
public function dehydrate(array &$resolvedValue): void
4646
{
4747
if (count($resolvedValue) > 0) {
48-
$keys = array_keys($resolvedValue);
49-
$firstKey = array_pop($keys);
48+
$firstKey = array_key_first($resolvedValue);
5049
$this->dehydrateMediaGalleryEntity($resolvedValue[$firstKey]);
51-
foreach ($keys as $key) {
52-
$resolvedValue[$key]['model_info'] = &$resolvedValue[$firstKey]['model_info'];
50+
foreach ($resolvedValue as $key => &$value) {
51+
if ($key !== $firstKey) {
52+
unset($value['model']);
53+
}
5354
}
5455
}
5556
}

app/code/Magento/CatalogGraphQl/Model/Resolver/Cache/Product/MediaGallery/ProductModelHydrator.php

+13-1
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111
use Magento\Catalog\Model\ProductFactory;
1212
use Magento\Framework\EntityManager\HydratorPool;
1313
use Magento\GraphQlResolverCache\Model\Resolver\Result\HydratorInterface;
14+
use Magento\GraphQlResolverCache\Model\Resolver\Result\PrehydratorInterface;
1415

1516
/**
1617
* Product resolver data hydrator to rehydrate propagated model.
1718
*/
18-
class ProductModelHydrator implements HydratorInterface
19+
class ProductModelHydrator implements HydratorInterface, PrehydratorInterface
1920
{
2021
/**
2122
* @var ProductFactory
@@ -62,4 +63,15 @@ public function hydrate(array &$resolverData): void
6263
unset($resolverData['model_info']);
6364
}
6465
}
66+
67+
/**
68+
* @inheritDoc
69+
*/
70+
public function prehydrate(array &$resolverData): void
71+
{
72+
$firstKey = array_key_first($resolverData);
73+
foreach ($resolverData as &$value) {
74+
$value['model_info'] = &$resolverData[$firstKey]['model_info'];
75+
}
76+
}
6577
}

app/code/Magento/GraphQlResolverCache/Model/Resolver/Result/HydratorComposite.php

+21-4
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010
/**
1111
* Composite hydrator for resolver result data.
1212
*/
13-
class HydratorComposite implements HydratorInterface
13+
class HydratorComposite implements HydratorInterface, PrehydratorInterface
1414
{
1515
/**
16-
* @var HydratorInterface[]
16+
* @var HydratorInterface[]|PrehydratorInterface[]
1717
*/
1818
private array $hydrators = [];
1919

2020
/**
21-
* @param HydratorInterface[] $hydrators
21+
* @param HydratorInterface[]|PrehydratorInterface[] $hydrators
2222
*/
2323
public function __construct(array $hydrators = [])
2424
{
@@ -34,7 +34,24 @@ public function hydrate(array &$resolverData): void
3434
return;
3535
}
3636
foreach ($this->hydrators as $hydrator) {
37-
$hydrator->hydrate($resolverData);
37+
if ($hydrator instanceof HydratorInterface) {
38+
$hydrator->hydrate($resolverData);
39+
}
40+
}
41+
}
42+
43+
/**
44+
* @inheritDoc
45+
*/
46+
public function prehydrate(array &$resolverData): void
47+
{
48+
if (empty($resolverData)) {
49+
return;
50+
}
51+
foreach ($this->hydrators as $hydrator) {
52+
if ($hydrator instanceof PrehydratorInterface) {
53+
$hydrator->prehydrate($resolverData);
54+
}
3855
}
3956
}
4057
}

app/code/Magento/GraphQlResolverCache/Model/Resolver/Result/HydratorInterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
interface HydratorInterface
1414
{
1515
/**
16-
* Hydrate resolved data.
16+
* Hydrates resolved data before passing to child resolver.
1717
*
1818
* @param array $resolverData
1919
* @return void
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\GraphQlResolverCache\Model\Resolver\Result;
9+
10+
/**
11+
* Prehydrator interface for resolver data.
12+
*/
13+
interface PrehydratorInterface
14+
{
15+
/**
16+
* Pre-hydrates the whole cached record right after cache read.
17+
*
18+
* @param array $resolverData
19+
* @return void
20+
*/
21+
public function prehydrate(array &$resolverData): void;
22+
}

app/code/Magento/GraphQlResolverCache/Model/Resolver/Result/ValueProcessor.php

+1
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ public function processCachedValueAfterLoad(
113113
$hydrator = $this->hydratorProvider->getHydratorForResolver($resolver);
114114
if ($hydrator) {
115115
$this->hydrators[$cacheKey] = $hydrator;
116+
$hydrator->prehydrate($value);
116117
$this->getFlagSetterForType($info)->setFlagOnValue($value, $cacheKey);
117118
}
118119
}

dev/tests/integration/testsuite/Magento/GraphQlResolverCache/Model/Resolver/Result/HydratorDehydratorProviderTest.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\GraphQlResolverCache\Model\Resolver\Result;
99

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

105-
$testModelHydrator = $this->getMockBuilder(HydratorInterface::class)
106+
$testModelHydrator = $this->getMockBuilder(ProductModelHydrator::class)
106107
->disableOriginalConstructor()
107108
->setMockClassName('TestResolverModelHydrator')
108-
->onlyMethods(['hydrate'])
109+
->onlyMethods(['hydrate', 'prehydrate'])
109110
->getMock();
110111
$testModelHydrator->expects($this->once())
111112
->method('hydrate')
@@ -154,6 +155,7 @@ public function testHydratorChainProvider()
154155

155156
$this->objectManager->removeSharedInstance('TestResolverModelHydrator');
156157
$this->objectManager->removeSharedInstance('TestResolverNestedItemsHydrator');
158+
$this->objectManager->removeSharedInstance('TestResolverModelDehydrator');
157159
}
158160

159161
/**

0 commit comments

Comments
 (0)