Skip to content

Commit 6cdc2b2

Browse files
ENGCOM-5288: GraphQL-741: Add extension point to set custom parameters to Query Context object #742
- Merge Pull Request magento/graphql-ce#742 from magento/graphql-ce:magento/graphql-ce#741 - Merged commits: 1. 3096057 2. e514ce0 3. 5d12f8e 4. ed35766 5. ac05279 6. 8831221 7. 13ea1f8 8. bb19773 9. 7b3024a 10. 892c131 11. 4485e23 12. 785ab2b 13. 4788d35 14. 87296c1 15. 0d505fe 16. 28aaefa
2 parents 883a4b1 + 28aaefa commit 6cdc2b2

File tree

23 files changed

+499
-134
lines changed

23 files changed

+499
-134
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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\CustomerGraphQl\Model\Context;
9+
10+
use Magento\Authorization\Model\UserContextInterface;
11+
use Magento\GraphQl\Model\Query\ContextParametersInterface;
12+
use Magento\GraphQl\Model\Query\ContextParametersProcessorInterface;
13+
14+
/**
15+
* @inheritdoc
16+
*/
17+
class AddUserInfoToContext implements ContextParametersProcessorInterface
18+
{
19+
/**
20+
* @var UserContextInterface
21+
*/
22+
private $userContext;
23+
24+
/**
25+
* @param UserContextInterface $userContext
26+
*/
27+
public function __construct(
28+
UserContextInterface $userContext
29+
) {
30+
$this->userContext = $userContext;
31+
}
32+
33+
/**
34+
* @inheritdoc
35+
*/
36+
public function execute(ContextParametersInterface $contextParameters): ContextParametersInterface
37+
{
38+
$currentUserId = $this->userContext->getUserId();
39+
if (null !== $currentUserId) {
40+
$currentUserId = (int)$currentUserId;
41+
}
42+
43+
$currentUserType = $this->userContext->getUserType();
44+
if (null !== $currentUserType) {
45+
$currentUserType = (int)$currentUserType;
46+
}
47+
48+
$contextParameters->setUserId($currentUserId);
49+
$contextParameters->setUserType($currentUserType);
50+
return $contextParameters;
51+
}
52+
}

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/CreateCustomer.php

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

88
namespace Magento\CustomerGraphQl\Model\Resolver;
99

10-
use Magento\Authorization\Model\UserContextInterface;
1110
use Magento\CustomerGraphQl\Model\Customer\CreateCustomerAccount;
1211
use Magento\CustomerGraphQl\Model\Customer\ExtractCustomerData;
1312
use Magento\Framework\GraphQl\Config\Element\Field;
@@ -58,9 +57,6 @@ public function resolve(
5857

5958
$customer = $this->createCustomerAccount->execute($args['input']);
6059

61-
$context->setUserId((int)$customer->getId());
62-
$context->setUserType(UserContextInterface::USER_TYPE_CUSTOMER);
63-
6460
$data = $this->extractCustomerData->execute($customer);
6561
return ['customer' => $data];
6662
}

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

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

88
namespace Magento\CustomerGraphQl\Model\Resolver;
99

10-
use Magento\CustomerGraphQl\Model\Customer\GetCustomer;
10+
use Magento\Framework\Exception\LocalizedException;
1111
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1212
use Magento\Framework\GraphQl\Config\Element\Field;
1313
use Magento\Framework\GraphQl\Query\ResolverInterface;
@@ -18,25 +18,17 @@
1818
*/
1919
class IsSubscribed implements ResolverInterface
2020
{
21-
/**
22-
* @var GetCustomer
23-
*/
24-
private $getCustomer;
25-
2621
/**
2722
* @var SubscriberFactory
2823
*/
2924
private $subscriberFactory;
3025

3126
/**
32-
* @param GetCustomer $getCustomer
3327
* @param SubscriberFactory $subscriberFactory
3428
*/
3529
public function __construct(
36-
GetCustomer $getCustomer,
3730
SubscriberFactory $subscriberFactory
3831
) {
39-
$this->getCustomer = $getCustomer;
4032
$this->subscriberFactory = $subscriberFactory;
4133
}
4234

@@ -50,7 +42,11 @@ public function resolve(
5042
array $value = null,
5143
array $args = null
5244
) {
53-
$customer = $this->getCustomer->execute($context);
45+
if (!isset($value['model'])) {
46+
throw new LocalizedException(__('"model" value should be specified'));
47+
}
48+
/** @var Customer $customer */
49+
$customer = $value['model'];
5450

5551
$status = $this->subscriberFactory->create()->loadByCustomerId((int)$customer->getId())->isSubscribed();
5652
return (bool)$status;

app/code/Magento/CustomerGraphQl/composer.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,15 @@
44
"type": "magento2-module",
55
"require": {
66
"php": "~7.1.3||~7.2.0",
7-
"magento/module-customer": "*",
87
"magento/module-authorization": "*",
8+
"magento/module-customer": "*",
99
"magento/module-eav": "*",
10+
"magento/module-graph-ql": "*",
1011
"magento/module-newsletter": "*",
1112
"magento/module-integration": "*",
1213
"magento/module-store": "*",
1314
"magento/framework": "*"
1415
},
15-
"suggest": {
16-
"magento/module-graph-ql": "*"
17-
},
1816
"license": [
1917
"OSL-3.0",
2018
"AFL-3.0"
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\GraphQl\Model\Query\ContextInterface">
10+
<attribute code="customer" type="Magento\Customer\Api\Data\CustomerInterface"/>
11+
</extension_attributes>
12+
</config>

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,11 @@
1313
</argument>
1414
</arguments>
1515
</type>
16-
</config>
16+
<type name="Magento\GraphQl\Model\Query\ContextFactory">
17+
<arguments>
18+
<argument name="contextParametersProcessors" xsi:type="array">
19+
<item name="add_user_info_to_context" xsi:type="object">Magento\CustomerGraphQl\Model\Context\AddUserInfoToContext</item>
20+
</argument>
21+
</arguments>
22+
</type>
23+
</config>

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Magento\Framework\GraphQl\Query\Fields as QueryFields;
2222
use Magento\Framework\Controller\Result\JsonFactory;
2323
use Magento\Framework\App\ObjectManager;
24+
use Magento\GraphQl\Model\Query\ContextFactoryInterface;
2425

2526
/**
2627
* Front controller for web API GraphQL area.
@@ -58,6 +59,7 @@ class GraphQl implements FrontControllerInterface
5859

5960
/**
6061
* @var ContextInterface
62+
* @deprecated $contextFactory is used for creating Context object
6163
*/
6264
private $resolverContext;
6365

@@ -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 ContextInterface $resolverContext
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(
@@ -104,7 +112,8 @@ public function __construct(
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;
@@ -116,6 +125,7 @@ public function __construct(
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
/**
@@ -144,7 +154,7 @@ public function dispatch(RequestInterface $request) : ResponseInterface
144154
$result = $this->queryProcessor->process(
145155
$schema,
146156
$query,
147-
$this->resolverContext,
157+
$this->contextFactory->create(),
148158
$data['variables'] ?? []
149159
);
150160
} catch (\Exception $error) {
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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;
9+
10+
/**
11+
* Concrete implementation for @see ContextInterface
12+
*
13+
* The purpose for this that GraphQL specification wants to make use of such object where multiple modules can
14+
* participate with data through extension attributes.
15+
*/
16+
class Context implements ContextInterface
17+
{
18+
/**
19+
* @var int|null
20+
*/
21+
private $userType;
22+
23+
/**
24+
* @var int|null
25+
*/
26+
private $userId;
27+
28+
/**
29+
* @var ContextExtensionInterface
30+
*/
31+
private $extensionAttributes;
32+
33+
/**
34+
* @param int|null $userType
35+
* @param int|null $userId
36+
* @param ContextExtensionInterface $extensionAttributes
37+
*/
38+
public function __construct(
39+
?int $userType,
40+
?int $userId,
41+
ContextExtensionInterface $extensionAttributes
42+
) {
43+
$this->userType = $userType;
44+
$this->userId = $userId;
45+
$this->extensionAttributes = $extensionAttributes;
46+
}
47+
48+
/**
49+
* @inheritdoc
50+
*/
51+
public function getUserType(): ?int
52+
{
53+
return $this->userType;
54+
}
55+
56+
/**
57+
* @inheritdoc
58+
*/
59+
public function getUserId(): ?int
60+
{
61+
return $this->userId;
62+
}
63+
64+
/**
65+
* @inheritdoc
66+
*/
67+
public function getExtensionAttributes(): ContextExtensionInterface
68+
{
69+
return $this->extensionAttributes;
70+
}
71+
}

0 commit comments

Comments
 (0)