Skip to content

Commit 0d505fe

Browse files
committed
magento/graphql-ce#741: Add extension point to set custom parameters to Query Context object
1 parent 87296c1 commit 0d505fe

File tree

10 files changed

+24
-144
lines changed

10 files changed

+24
-144
lines changed

app/code/Magento/CustomerGraphQl/Model/Context/AddUserInfoToContext.php

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,13 @@ class AddUserInfoToContext implements ContextParametersProcessorInterface
2121
*/
2222
private $userContext;
2323

24-
/**
25-
* @var GetCustomer
26-
*/
27-
private $getCustomer;
28-
2924
/**
3025
* @param UserContextInterface $userContext
31-
* @param GetCustomer $getCustomer
3226
*/
3327
public function __construct(
34-
UserContextInterface $userContext,
35-
GetCustomer $getCustomer
28+
UserContextInterface $userContext
3629
) {
3730
$this->userContext = $userContext;
38-
$this->getCustomer = $getCustomer;
3931
}
4032

4133
/**
@@ -53,28 +45,8 @@ public function execute(ContextParametersInterface $contextParameters): ContextP
5345
$currentUserType = (int)$currentUserType;
5446
}
5547

56-
if (false === $this->isUserGuest($currentUserId, $currentUserType)) {
57-
$customer = $this->getCustomer->execute($currentUserId);
58-
$contextParameters->addExtensionAttribute('customer', $customer);
59-
}
60-
6148
$contextParameters->setUserId($currentUserId);
6249
$contextParameters->setUserType($currentUserType);
6350
return $contextParameters;
6451
}
65-
66-
/**
67-
* Checking if current customer is guest
68-
*
69-
* @param int|null $customerId
70-
* @param int|null $customerType
71-
* @return bool
72-
*/
73-
private function isUserGuest(?int $customerId, ?int $customerType): bool
74-
{
75-
if (null === $customerId || null === $customerType) {
76-
return true;
77-
}
78-
return 0 === (int)$customerId || (int)$customerType === UserContextInterface::USER_TYPE_GUEST;
79-
}
8052
}

app/code/Magento/CustomerGraphQl/Model/Context/GetCustomer.php

Lines changed: 0 additions & 94 deletions
This file was deleted.

app/code/Magento/CustomerGraphQl/Model/Customer/SaveCustomer.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Magento\Framework\Exception\AlreadyExistsException;
1212
use Magento\Framework\Exception\LocalizedException;
1313
use Magento\Framework\GraphQl\Exception\GraphQlAlreadyExistsException;
14-
use Magento\Framework\GraphQl\Exception\GraphQlAuthenticationException;
1514
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1615
use Magento\Customer\Api\Data\CustomerInterface;
1716

@@ -39,7 +38,6 @@ public function __construct(
3938
*
4039
* @param CustomerInterface $customer
4140
* @throws GraphQlAlreadyExistsException
42-
* @throws GraphQlAuthenticationException
4341
* @throws GraphQlInputException
4442
*/
4543
public function execute(CustomerInterface $customer): void

app/code/Magento/CustomerGraphQl/Model/Resolver/IsSubscribed.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
namespace Magento\CustomerGraphQl\Model\Resolver;
99

10-
use Magento\CustomerGraphQl\Model\Customer\GetCustomer;
1110
use Magento\Framework\Exception\LocalizedException;
1211
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1312
use Magento\Framework\GraphQl\Config\Element\Field;
@@ -19,25 +18,17 @@
1918
*/
2019
class IsSubscribed implements ResolverInterface
2120
{
22-
/**
23-
* @var GetCustomer
24-
*/
25-
private $getCustomer;
26-
2721
/**
2822
* @var SubscriberFactory
2923
*/
3024
private $subscriberFactory;
3125

3226
/**
33-
* @param GetCustomer $getCustomer
3427
* @param SubscriberFactory $subscriberFactory
3528
*/
3629
public function __construct(
37-
GetCustomer $getCustomer,
3830
SubscriberFactory $subscriberFactory
3931
) {
40-
$this->getCustomer = $getCustomer;
4132
$this->subscriberFactory = $subscriberFactory;
4233
}
4334

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\Framework\App\ResponseInterface;
1414
use Magento\Framework\GraphQl\Exception\ExceptionFormatter;
1515
use Magento\Framework\GraphQl\Query\QueryProcessor;
16+
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
1617
use Magento\Framework\GraphQl\Schema\SchemaGeneratorInterface;
1718
use Magento\Framework\Serialize\SerializerInterface;
1819
use Magento\Framework\Webapi\Response;
@@ -57,9 +58,10 @@ class GraphQl implements FrontControllerInterface
5758
private $graphQlError;
5859

5960
/**
60-
* @var ContextFactoryInterface
61+
* @var ContextInterface
62+
* @deprecated $contextFactory is used for creating Context object
6163
*/
62-
private $contextFactory;
64+
private $resolverContext;
6365

6466
/**
6567
* @var HttpRequestProcessor
@@ -81,17 +83,23 @@ class GraphQl implements FrontControllerInterface
8183
*/
8284
private $httpResponse;
8385

86+
/**
87+
* @var ContextFactoryInterface
88+
*/
89+
private $contextFactory;
90+
8491
/**
8592
* @param Response $response
8693
* @param SchemaGeneratorInterface $schemaGenerator
8794
* @param SerializerInterface $jsonSerializer
8895
* @param QueryProcessor $queryProcessor
8996
* @param ExceptionFormatter $graphQlError
90-
* @param ContextFactoryInterface $contextFactory
97+
* @param ContextInterface $resolverContext Deprecated. $contextFactory is used for creating Context object.
9198
* @param HttpRequestProcessor $requestProcessor
9299
* @param QueryFields $queryFields
93100
* @param JsonFactory|null $jsonFactory
94101
* @param HttpResponse|null $httpResponse
102+
* @param ContextFactoryInterface $contextFactory
95103
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
96104
*/
97105
public function __construct(
@@ -100,22 +108,24 @@ public function __construct(
100108
SerializerInterface $jsonSerializer,
101109
QueryProcessor $queryProcessor,
102110
ExceptionFormatter $graphQlError,
103-
ContextFactoryInterface $contextFactory,
111+
ContextInterface $resolverContext,
104112
HttpRequestProcessor $requestProcessor,
105113
QueryFields $queryFields,
106114
JsonFactory $jsonFactory = null,
107-
HttpResponse $httpResponse = null
115+
HttpResponse $httpResponse = null,
116+
ContextFactoryInterface $contextFactory = null
108117
) {
109118
$this->response = $response;
110119
$this->schemaGenerator = $schemaGenerator;
111120
$this->jsonSerializer = $jsonSerializer;
112121
$this->queryProcessor = $queryProcessor;
113122
$this->graphQlError = $graphQlError;
114-
$this->contextFactory = $contextFactory;
123+
$this->resolverContext = $resolverContext;
115124
$this->requestProcessor = $requestProcessor;
116125
$this->queryFields = $queryFields;
117126
$this->jsonFactory = $jsonFactory ?: ObjectManager::getInstance()->get(JsonFactory::class);
118127
$this->httpResponse = $httpResponse ?: ObjectManager::getInstance()->get(HttpResponse::class);
128+
$this->contextFactory = $contextFactory ?: ObjectManager::getInstance()->get(ContextFactoryInterface::class);
119129
}
120130

121131
/**

app/code/Magento/GraphQl/Model/Query/ContextFactory.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class ContextFactory implements ContextFactoryInterface
1919
/**
2020
* @var ExtensionAttributesFactory
2121
*/
22-
protected $extensionAttributesFactory;
22+
private $extensionAttributesFactory;
2323

2424
/**
2525
* @var ObjectManagerInterface
@@ -64,7 +64,9 @@ public function create(): ContextInterface
6464

6565
$extensionAttributes = $this->extensionAttributesFactory->create(
6666
ContextInterface::class,
67-
$contextParameters->getExtensionAttributesData()
67+
[
68+
'data' => $contextParameters->getExtensionAttributesData(),
69+
]
6870
);
6971

7072
$context = $this->objectManager->create(

app/code/Magento/GraphQl/etc/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
99
<preference for="Magento\Framework\GraphQl\Schema\SchemaGeneratorInterface" type="Magento\Framework\GraphQl\Schema\SchemaGenerator" />
10+
<preference for="Magento\Framework\GraphQl\Query\Resolver\ContextInterface" type="Magento\GraphQl\Model\Query\Resolver\Context" />
1011
<preference for="Magento\GraphQl\Model\Query\ContextInterface" type="Magento\GraphQl\Model\Query\Context" />
1112
<preference for="Magento\Framework\GraphQl\Schema\Type\Entity\MapperInterface" type="Magento\Framework\GraphQl\Schema\Type\Entity\DefaultMapper"/>
1213
<preference for="Magento\Framework\GraphQl\Schema\Type\Enum\DataMapperInterface" type="Magento\Framework\GraphQl\Schema\Type\Enum\DefaultDataMapper"/>

app/code/Magento/QuoteGraphQl/Model/Cart/GetCartForUser.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public function __construct(
5858
* @return Quote
5959
* @throws GraphQlAuthorizationException
6060
* @throws GraphQlNoSuchEntityException
61+
* @throws NoSuchEntityException
6162
*/
6263
public function execute(string $cartHash, ?int $customerId): Quote
6364
{

app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddressFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ public function createBasedOnInputData(array $addressInput): QuoteAddress
8080
* @param int $customerAddressId
8181
* @param int $customerId
8282
* @return QuoteAddress
83-
* @throws GraphQlInputException
8483
* @throws GraphQlAuthorizationException
84+
* @throws GraphQlInputException
8585
* @throws GraphQlNoSuchEntityException
8686
*/
8787
public function createBasedOnCustomerAddress(int $customerAddressId, int $customerId): QuoteAddress

app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingAddressesOnCartInterface.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ interface SetShippingAddressesOnCartInterface
3131
* @return void
3232
* @throws GraphQlInputException
3333
* @throws GraphQlAuthorizationException
34-
* @throws GraphQlAuthenticationException
3534
* @throws GraphQlNoSuchEntityException
3635
*/
3736
public function execute(ContextInterface $context, CartInterface $cart, array $shippingAddressesInput): void;

0 commit comments

Comments
 (0)