Skip to content

Commit 09dc09c

Browse files
committed
Prototype of GraphQL mutations for customer update
1 parent f0c4347 commit 09dc09c

File tree

2 files changed

+161
-0
lines changed

2 files changed

+161
-0
lines changed
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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\Resolver;
9+
10+
use Magento\Authorization\Model\UserContextInterface;
11+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
12+
use Magento\CustomerGraphQl\Model\Resolver\Customer\CustomerDataProvider;
13+
use Magento\Framework\GraphQl\Config\Element\Field;
14+
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
15+
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
16+
use Magento\Framework\GraphQl\Query\Resolver\Value;
17+
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
18+
use Magento\Framework\GraphQl\Query\ResolverInterface;
19+
use Magento\Store\Api\StoreResolverInterface;
20+
use Magento\Framework\Encryption\EncryptorInterface as Encryptor;
21+
use Magento\Customer\Model\CustomerRegistry;
22+
23+
24+
/**
25+
* Customers field resolver, used for GraphQL request processing.
26+
*/
27+
class CustomerUpdate implements ResolverInterface
28+
{
29+
/**
30+
* @var CustomerDataProvider
31+
*/
32+
private $customerResolver;
33+
34+
/**
35+
* @var ValueFactory
36+
*/
37+
private $valueFactory;
38+
39+
/**
40+
* @var StoreResolverInterface
41+
*/
42+
private $storeResolver;
43+
44+
/**
45+
* @var \Magento\Newsletter\Model\SubscriberFactory
46+
*/
47+
protected $subscriberFactory;
48+
49+
/**
50+
* @var AuthenticationInterface
51+
*/
52+
private $authentication;
53+
54+
/**
55+
* @var CustomerRegistry
56+
*/
57+
protected $customerRegistry;
58+
59+
/**
60+
* @var Encryptor
61+
*/
62+
protected $encryptor;
63+
64+
/**
65+
* @param CustomerDataProvider $customerResolver
66+
* @param ValueFactory $valueFactory
67+
*/
68+
public function __construct(
69+
CustomerDataProvider $customerResolver,
70+
ValueFactory $valueFactory,
71+
\Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
72+
\Magento\Newsletter\Model\SubscriberFactory $subscriberFactory,
73+
StoreResolverInterface $storeResolver,
74+
Encryptor $encryptor,
75+
CustomerRegistry $customerRegistry
76+
) {
77+
$this->customerResolver = $customerResolver;
78+
$this->valueFactory = $valueFactory;
79+
$this->customerRepository = $customerRepository;
80+
$this->subscriberFactory = $subscriberFactory;
81+
$this->storeResolver = $storeResolver;
82+
$this->encryptor = $encryptor;
83+
$this->customerRegistry = $customerRegistry;
84+
}
85+
86+
/**
87+
* {@inheritdoc}
88+
*/
89+
public function resolve(
90+
Field $field,
91+
$context,
92+
ResolveInfo $info,
93+
array $value = null,
94+
array $args = null
95+
) : Value {
96+
97+
/** @var ContextInterface $context */
98+
if ((!$context->getUserId()) || $context->getUserType() == UserContextInterface::USER_TYPE_GUEST) {
99+
throw new GraphQlAuthorizationException(
100+
__(
101+
'Current customer does not have access to the resource "%1"',
102+
[\Magento\Customer\Model\Customer::ENTITY]
103+
)
104+
);
105+
}
106+
107+
$customer = $this->customerRepository->getById($context->getUserId());
108+
109+
if (isset($args['email']) && $customer->getEmail() !== $args['email']) {
110+
$customerSecure = $this->customerRegistry->retrieveSecureData($context->getUserId());
111+
$hash = $customerSecure->getPasswordHash();
112+
if (!$this->encryptor->validateHash($args['password'], $hash)) {
113+
throw new GraphQlAuthorizationException(__('Invalid login or password.'));
114+
}
115+
$customer->setEmail($args['email']);
116+
}
117+
118+
if (isset($args['firstname'])) {
119+
$customer->setFirstname($args['firstname']);
120+
}
121+
if (isset($args['lastname'])){
122+
$customer->setLastname($args['lastname']);
123+
}
124+
125+
$customer->setStoreId($this->storeResolver->getCurrentStoreId());
126+
$this->customerRepository->save($customer);
127+
128+
if (isset($args['is_subscribed'])) {
129+
130+
$checkSubscriber = $this->subscriberFactory->create()->loadByCustomerId($context->getUserId());
131+
132+
if ($args['is_subscribed'] === true && !$checkSubscriber->isSubscribed()) {
133+
$this->subscriberFactory->create()->subscribeCustomerById($context->getUserId());
134+
} elseif ($args['is_subscribed'] === false && $checkSubscriber->isSubscribed()) {
135+
$this->subscriberFactory->create()->unsubscribeCustomerById($context->getUserId());
136+
}
137+
}
138+
139+
140+
$data = $args;
141+
$result = function () use ($data) {
142+
return !empty($data) ? $data : [];
143+
};
144+
145+
return $this->valueFactory->create($result);
146+
147+
148+
}
149+
150+
}

app/code/Magento/CustomerGraphQl/etc/schema.graphqls

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,14 @@ type CustomerAddressRegion @doc(description: "CustomerAddressRegion defines the
5151
region_id: Int @doc(description: "Uniquely identifies the region")
5252
}
5353

54+
type Mutation {
55+
customerUpdate (firstname: String, lastname: String, email: String, password: String, is_subscribed: Boolean): customerItem @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\CustomerUpdate") @doc(description:"Update customer personal information")
56+
}
57+
58+
type customerItem {
59+
firstname: String
60+
lastname: String
61+
email: String
62+
password: String
63+
is_subscribed: Boolean
64+
}

0 commit comments

Comments
 (0)