Skip to content

Commit 708f5b7

Browse files
committed
initial commit
0 parents  commit 708f5b7

File tree

8 files changed

+222
-0
lines changed

8 files changed

+222
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Graycore\GraphQlIntrospectionCache\Model\Cache\Identity\Introspection;
4+
5+
use Magento\Catalog\Model\Product;
6+
use Magento\Eav\Api\AttributeRepositoryInterface;
7+
use Magento\Eav\Api\Data\AttributeInterface;
8+
use Magento\Eav\Model\Entity\Attribute;
9+
use Magento\Framework\Api\SearchCriteriaBuilder;
10+
use Magento\Framework\GraphQl\Query\Resolver\IdentityInterface;
11+
12+
class ProductAttributeFilterInput implements IdentityInterface
13+
{
14+
private AttributeRepositoryInterface $attributeRepository;
15+
private SearchCriteriaBuilder $searchCriteriaBuilder;
16+
17+
public function __construct(
18+
AttributeRepositoryInterface $attributeRepository,
19+
SearchCriteriaBuilder $searchCriteriaBuilder
20+
) {
21+
$this->attributeRepository = $attributeRepository;
22+
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
23+
}
24+
25+
public function getIdentities(array $resolvedData): array
26+
{
27+
return array_map(
28+
fn($attribute) => Attribute::CACHE_TAG . '_' . $attribute->getAttributeId(),
29+
$this->attributeRepository->getList(
30+
Product::ENTITY,
31+
$this->searchCriteriaBuilder->addFilter(
32+
AttributeInterface::ATTRIBUTE_CODE,
33+
array_map(fn($attribute) => $attribute['name'], $resolvedData['__type']['inputFields'] ?? []),
34+
'in'
35+
)->create()
36+
)->getItems()
37+
);
38+
}
39+
}

Plugin/CachePlugin.php

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace Graycore\GraphQlIntrospectionCache\Plugin;
4+
5+
use Closure;
6+
use GraphQL\Executor\Promise\Adapter\SyncPromiseAdapter;
7+
use GraphQL\Executor\Promise\Promise;
8+
use GraphQL\Executor\ReferenceExecutor;
9+
use Magento\Framework\GraphQl\Query\Resolver\IdentityInterface;
10+
use Magento\GraphQlCache\Model\CacheableQuery;
11+
12+
class CachePlugin
13+
{
14+
private CacheableQuery $cacheableQuery;
15+
private SyncPromiseAdapter $syncPromiseAdapter;
16+
private array $introspectionHandlers;
17+
18+
public function __construct(
19+
CacheableQuery $cacheableQuery,
20+
SyncPromiseAdapter $syncPromiseAdapter,
21+
array $introspectionHandlers = []
22+
) {
23+
$this->cacheableQuery = $cacheableQuery;
24+
$this->syncPromiseAdapter = $syncPromiseAdapter;
25+
$this->introspectionHandlers = $introspectionHandlers;
26+
}
27+
28+
public function afterDoExecute(ReferenceExecutor $subject, Promise $result): Promise
29+
{
30+
$executionContext = (Closure::bind(fn() => $this->exeContext, $subject, ReferenceExecutor::class))();
31+
if ($executionContext === null) {
32+
return $result;
33+
}
34+
35+
foreach ($executionContext->operation->selectionSet->selections as $selection) {
36+
if ($selection->name->value !== '__type') {
37+
continue;
38+
}
39+
40+
foreach ($selection->arguments as $argument) {
41+
if ($argument->name->value !== 'name' || !isset($this->introspectionHandlers[$argument->value->value])) {
42+
continue;
43+
}
44+
45+
$cacheIdentity = $this->introspectionHandlers[$argument->value->value];
46+
if ($cacheIdentity instanceof IdentityInterface) {
47+
$this->cacheableQuery->addCacheTags(
48+
$cacheIdentity->getIdentities($this->syncPromiseAdapter->wait($result)->data)
49+
);
50+
}
51+
}
52+
}
53+
54+
return $result;
55+
}
56+
}

Plugin/SetExecutorPlugin.php

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace Graycore\GraphQlIntrospectionCache\Plugin;
4+
5+
use Closure;
6+
use GraphQL\Executor\Executor;
7+
use GraphQL\Executor\ReferenceExecutor;
8+
use Magento\Framework\GraphQl\Query\QueryProcessor;
9+
use Magento\Framework\ObjectManagerInterface;
10+
11+
class SetExecutorPlugin
12+
{
13+
private ObjectManagerInterface $objectManager;
14+
15+
public function __construct(ObjectManagerInterface $objectManager)
16+
{
17+
$this->objectManager = $objectManager;
18+
}
19+
20+
/**
21+
* Before processing a GraphQL query, replace the factory with a wrapper that uses the object manager.
22+
* This allows us to create plugins on the ReferenceExecutor.
23+
*/
24+
public function beforeProcess(
25+
QueryProcessor $subject,
26+
...$args
27+
): array {
28+
Executor::setImplementationFactory(
29+
fn(
30+
$promiseAdapter,
31+
$schema,
32+
$documentNode,
33+
$rootValue,
34+
$contextValue,
35+
$variableValues,
36+
$operationName,
37+
$fieldResolver
38+
) => $this->objectManager->create(
39+
ReferenceExecutor::class,
40+
[
41+
'context' => Closure::bind(
42+
fn() => ReferenceExecutor::buildExecutionContext(
43+
$schema,
44+
$documentNode,
45+
$rootValue,
46+
$contextValue,
47+
$variableValues,
48+
$operationName,
49+
$fieldResolver,
50+
$promiseAdapter
51+
),
52+
null,
53+
ReferenceExecutor::class
54+
)()
55+
]
56+
)
57+
);
58+
return $args;
59+
}
60+
}

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Magento 2 GraphQL Introspection Cache
2+
3+
## License
4+
5+
Copyright 2022 Graycore, LLC
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
8+
9+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10+
11+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12+

composer.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "graycore/magento2-graphql-introspection-cache",
3+
"type": "magento2-module",
4+
"require": {
5+
"magento/framework": "*",
6+
"php": ">=7.4"
7+
},
8+
"license": [
9+
"MIT"
10+
],
11+
"autoload": {
12+
"files": [
13+
"registration.php"
14+
],
15+
"psr-4": {
16+
"Graycore\\GraphQlIntrospectionCache\\": ""
17+
}
18+
}
19+
}

etc/di.xml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0"?>
2+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
4+
<type name="GraphQL\Executor\ReferenceExecutor">
5+
<plugin name="cache_plugin"
6+
type="Graycore\GraphQlIntrospectionCache\Plugin\CachePlugin"/>
7+
</type>
8+
<type name="Magento\Framework\GraphQl\Query\QueryProcessor">
9+
<plugin name="set_executor_plugin"
10+
type="Graycore\GraphQlIntrospectionCache\Plugin\SetExecutorPlugin"/>
11+
</type>
12+
<type name="Graycore\GraphQlIntrospectionCache\Plugin\CachePlugin">
13+
<arguments>
14+
<argument xsi:type="array" name="introspectionHandlers">
15+
<item xsi:type="object" name="ProductAttributeFilterInput">Graycore\GraphQlIntrospectionCache\Model\Cache\Identity\Introspection\ProductAttributeFilterInput</item>
16+
</argument>
17+
</arguments>
18+
</type>
19+
</config>

etc/module.xml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0"?>
2+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
4+
<module name="Graycore_GraphQlIntrospectionCache">
5+
<sequence>
6+
<module name="Magento_Catalog"/>
7+
<module name="Magento_Eav"/>
8+
<module name="Magento_GraphQlCache"/>
9+
<module name="Magento_GraphQl"/>
10+
</sequence>
11+
</module>
12+
</config>

registration.php

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
use Magento\Framework\Component\ComponentRegistrar;
4+
5+
ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Graycore_GraphQlIntrospectionCache', __DIR__);

0 commit comments

Comments
 (0)