Skip to content

Commit a05afc4

Browse files
committed
Inject store_id to context using extension attributes
1 parent 64be222 commit a05afc4

File tree

7 files changed

+117
-31
lines changed

7 files changed

+117
-31
lines changed

app/code/Magento/GraphQl/Controller/GraphQl.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@
88
namespace Magento\GraphQl\Controller;
99

1010
use Magento\Framework\App\FrontControllerInterface;
11+
use Magento\Framework\App\ObjectManager;
1112
use Magento\Framework\App\Request\Http;
1213
use Magento\Framework\App\RequestInterface;
14+
use Magento\Framework\App\Response\Http as HttpResponse;
1315
use Magento\Framework\App\ResponseInterface;
16+
use Magento\Framework\Controller\Result\JsonFactory;
1417
use Magento\Framework\GraphQl\Exception\ExceptionFormatter;
18+
use Magento\Framework\GraphQl\Query\Fields as QueryFields;
1519
use Magento\Framework\GraphQl\Query\QueryProcessor;
16-
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
1720
use Magento\Framework\GraphQl\Schema\SchemaGeneratorInterface;
1821
use Magento\Framework\Serialize\SerializerInterface;
1922
use Magento\Framework\Webapi\Response;
20-
use Magento\Framework\App\Response\Http as HttpResponse;
21-
use Magento\Framework\GraphQl\Query\Fields as QueryFields;
22-
use Magento\Framework\Controller\Result\JsonFactory;
23-
use Magento\Framework\App\ObjectManager;
23+
use Magento\GraphQl\Model\Query\Resolver\ContextFactory;
2424

2525
/**
2626
* Front controller for web API GraphQL area.
@@ -57,9 +57,9 @@ class GraphQl implements FrontControllerInterface
5757
private $graphQlError;
5858

5959
/**
60-
* @var ContextInterface
60+
* @var ContextFactory
6161
*/
62-
private $resolverContext;
62+
private $resolverContextFactory;
6363

6464
/**
6565
* @var HttpRequestProcessor
@@ -87,7 +87,7 @@ class GraphQl implements FrontControllerInterface
8787
* @param SerializerInterface $jsonSerializer
8888
* @param QueryProcessor $queryProcessor
8989
* @param ExceptionFormatter $graphQlError
90-
* @param ContextInterface $resolverContext
90+
* @param ContextFactory $resolverContextFactory
9191
* @param HttpRequestProcessor $requestProcessor
9292
* @param QueryFields $queryFields
9393
* @param JsonFactory|null $jsonFactory
@@ -100,7 +100,7 @@ public function __construct(
100100
SerializerInterface $jsonSerializer,
101101
QueryProcessor $queryProcessor,
102102
ExceptionFormatter $graphQlError,
103-
ContextInterface $resolverContext,
103+
ContextFactory $resolverContextFactory,
104104
HttpRequestProcessor $requestProcessor,
105105
QueryFields $queryFields,
106106
JsonFactory $jsonFactory = null,
@@ -111,7 +111,7 @@ public function __construct(
111111
$this->jsonSerializer = $jsonSerializer;
112112
$this->queryProcessor = $queryProcessor;
113113
$this->graphQlError = $graphQlError;
114-
$this->resolverContext = $resolverContext;
114+
$this->resolverContextFactory = $resolverContextFactory;
115115
$this->requestProcessor = $requestProcessor;
116116
$this->queryFields = $queryFields;
117117
$this->jsonFactory = $jsonFactory ?: ObjectManager::getInstance()->get(JsonFactory::class);
@@ -141,10 +141,12 @@ public function dispatch(RequestInterface $request) : ResponseInterface
141141
$this->queryFields->setQuery($query, $variables);
142142
$schema = $this->schemaGenerator->generate();
143143

144+
$resolverContext = $this->resolverContextFactory->create();
145+
144146
$result = $this->queryProcessor->process(
145147
$schema,
146148
$query,
147-
$this->resolverContext,
149+
$resolverContext,
148150
$data['variables'] ?? []
149151
);
150152
} catch (\Exception $error) {

app/code/Magento/GraphQl/Model/Query/Resolver/Context.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -138,17 +138,6 @@ public function setUserType(int $typeId) : ContextInterface
138138
*/
139139
public function getStoreId(): int
140140
{
141-
if (null === $this->getData(self::STORE_ID)) {
142-
$this->setStoreId((int)$this->storeManager->getStore()->getId());
143-
}
144-
return $this->getData(self::STORE_ID);
145-
}
146-
147-
/**
148-
* @inheritDoc
149-
*/
150-
public function setStoreId(int $storeId) : ContextInterface
151-
{
152-
return $this->setData(self::STORE_ID, $storeId);
141+
return $this->getExtensionAttributes()->getStoreId();
153142
}
154143
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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\GraphQl\Model\Query\Resolver;
9+
10+
use Magento\Framework\ObjectManagerInterface;
11+
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
12+
13+
class ContextFactory
14+
{
15+
/**
16+
* @var ObjectManagerInterface
17+
*/
18+
protected $objectManager;
19+
20+
/**
21+
* @param ObjectManagerInterface $objectManager
22+
*/
23+
public function __construct(ObjectManagerInterface $objectManager)
24+
{
25+
$this->objectManager = $objectManager;
26+
}
27+
28+
/**
29+
* @return ContextInterface
30+
*/
31+
public function create()
32+
{
33+
return $this->objectManager->create(ContextInterface::class);
34+
}
35+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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\StoreGraphQl\Model\Plugin\Query\Resolver;
9+
10+
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
11+
use Magento\Store\Model\StoreManagerInterface;
12+
use Magento\GraphQl\Model\Query\Resolver\ContextFactory as ResolverContextFactory;
13+
use Magento\Framework\Exception\NoSuchEntityException;
14+
15+
/**
16+
* Plugin for injecting store information into resolver context
17+
*/
18+
class ContextFactory
19+
{
20+
/**
21+
* @var StoreManagerInterface
22+
*/
23+
private $storeManager;
24+
25+
/**
26+
* @param StoreManagerInterface $storeManager
27+
*/
28+
public function __construct(
29+
StoreManagerInterface $storeManager
30+
) {
31+
$this->storeManager = $storeManager;
32+
}
33+
34+
35+
/**
36+
* @param ResolverContextFactory $subject
37+
* @param ContextInterface $resultContext
38+
* @return ContextInterface
39+
*
40+
* @throws NoSuchEntityException
41+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
42+
*/
43+
public function afterCreate(
44+
ResolverContextFactory $subject,
45+
ContextInterface $resultContext
46+
) {
47+
$extensionAttributes = $resultContext->getExtensionAttributes();
48+
$extensionAttributes->setStoreId((int)$this->storeManager->getStore()->getId());
49+
$resultContext->setExtensionAttributes($extensionAttributes);
50+
51+
return $resultContext;
52+
}
53+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
9+
<extension_attributes for="Magento\Framework\GraphQl\Query\Resolver\ContextInterface">
10+
<attribute code="store_id" type="int" />
11+
</extension_attributes>
12+
</config>

app/code/Magento/StoreGraphQl/etc/graphql/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,7 @@
1616
</argument>
1717
</arguments>
1818
</type>
19+
<type name="Magento\GraphQl\Model\Query\Resolver\ContextFactory">
20+
<plugin name="add_store_id_to_resolver_context" type="Magento\StoreGraphQl\Model\Plugin\Query\Resolver\ContextFactory"/>
21+
</type>
1922
</config>

lib/internal/Magento/Framework/GraphQl/Query/Resolver/ContextInterface.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,6 @@ public function setUserId(int $userId) : ContextInterface;
5959
*/
6060
public function getStoreId() : int;
6161

62-
/**
63-
* Set current store id
64-
*
65-
* @param int $storeId
66-
* @return ContextInterface
67-
*/
68-
public function setStoreId(int $storeId) : ContextInterface;
69-
7062
/**
7163
* Retrieve existing extension attributes object or create a new one.
7264
*

0 commit comments

Comments
 (0)