File tree 7 files changed +67
-12
lines changed
CatalogGraphQl/Model/Resolver/Cache/Product/MediaGallery
GraphQlResolverCache/Model/Resolver/Result
dev/tests/integration/testsuite/Magento/GraphQlResolverCache/Model/Resolver/Result
7 files changed +67
-12
lines changed Original file line number Diff line number Diff line change @@ -45,11 +45,12 @@ public function __construct(
45
45
public function dehydrate (array &$ resolvedValue ): void
46
46
{
47
47
if (count ($ resolvedValue ) > 0 ) {
48
- $ keys = array_keys ($ resolvedValue );
49
- $ firstKey = array_pop ($ keys );
48
+ $ firstKey = array_key_first ($ resolvedValue );
50
49
$ 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
+ }
53
54
}
54
55
}
55
56
}
Original file line number Diff line number Diff line change 11
11
use Magento \Catalog \Model \ProductFactory ;
12
12
use Magento \Framework \EntityManager \HydratorPool ;
13
13
use Magento \GraphQlResolverCache \Model \Resolver \Result \HydratorInterface ;
14
+ use Magento \GraphQlResolverCache \Model \Resolver \Result \PrehydratorInterface ;
14
15
15
16
/**
16
17
* Product resolver data hydrator to rehydrate propagated model.
17
18
*/
18
- class ProductModelHydrator implements HydratorInterface
19
+ class ProductModelHydrator implements HydratorInterface, PrehydratorInterface
19
20
{
20
21
/**
21
22
* @var ProductFactory
@@ -62,4 +63,15 @@ public function hydrate(array &$resolverData): void
62
63
unset($ resolverData ['model_info ' ]);
63
64
}
64
65
}
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
+ }
65
77
}
Original file line number Diff line number Diff line change 10
10
/**
11
11
* Composite hydrator for resolver result data.
12
12
*/
13
- class HydratorComposite implements HydratorInterface
13
+ class HydratorComposite implements HydratorInterface, PrehydratorInterface
14
14
{
15
15
/**
16
- * @var HydratorInterface[]
16
+ * @var HydratorInterface[]|PrehydratorInterface[]
17
17
*/
18
18
private array $ hydrators = [];
19
19
20
20
/**
21
- * @param HydratorInterface[] $hydrators
21
+ * @param HydratorInterface[]|PrehydratorInterface[] $hydrators
22
22
*/
23
23
public function __construct (array $ hydrators = [])
24
24
{
@@ -34,7 +34,24 @@ public function hydrate(array &$resolverData): void
34
34
return ;
35
35
}
36
36
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
+ }
38
55
}
39
56
}
40
57
}
Original file line number Diff line number Diff line change 13
13
interface HydratorInterface
14
14
{
15
15
/**
16
- * Hydrate resolved data.
16
+ * Hydrates resolved data before passing to child resolver .
17
17
*
18
18
* @param array $resolverData
19
19
* @return void
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -113,6 +113,7 @@ public function processCachedValueAfterLoad(
113
113
$ hydrator = $ this ->hydratorProvider ->getHydratorForResolver ($ resolver );
114
114
if ($ hydrator ) {
115
115
$ this ->hydrators [$ cacheKey ] = $ hydrator ;
116
+ $ hydrator ->prehydrate ($ value );
116
117
$ this ->getFlagSetterForType ($ info )->setFlagOnValue ($ value , $ cacheKey );
117
118
}
118
119
}
Original file line number Diff line number Diff line change 7
7
8
8
namespace Magento \GraphQlResolverCache \Model \Resolver \Result ;
9
9
10
+ use Magento \CatalogGraphQl \Model \Resolver \Cache \Product \MediaGallery \ProductModelHydrator ;
10
11
use Magento \Framework \DataObject ;
11
12
use Magento \Framework \GraphQl \Query \ResolverInterface ;
12
13
use Magento \StoreGraphQl \Model \Resolver \StoreConfigResolver ;
@@ -102,10 +103,10 @@ public function testHydratorChainProvider()
102
103
unset($ resolverData ['model ' ]);
103
104
});
104
105
105
- $ testModelHydrator = $ this ->getMockBuilder (HydratorInterface ::class)
106
+ $ testModelHydrator = $ this ->getMockBuilder (ProductModelHydrator ::class)
106
107
->disableOriginalConstructor ()
107
108
->setMockClassName ('TestResolverModelHydrator ' )
108
- ->onlyMethods (['hydrate ' ])
109
+ ->onlyMethods (['hydrate ' , ' prehydrate ' ])
109
110
->getMock ();
110
111
$ testModelHydrator ->expects ($ this ->once ())
111
112
->method ('hydrate ' )
@@ -154,6 +155,7 @@ public function testHydratorChainProvider()
154
155
155
156
$ this ->objectManager ->removeSharedInstance ('TestResolverModelHydrator ' );
156
157
$ this ->objectManager ->removeSharedInstance ('TestResolverNestedItemsHydrator ' );
158
+ $ this ->objectManager ->removeSharedInstance ('TestResolverModelDehydrator ' );
157
159
}
158
160
159
161
/**
You can’t perform that action at this time.
0 commit comments