Skip to content

Commit

Permalink
Prototype of GraphQL mutations for customer update
Browse files Browse the repository at this point in the history
  • Loading branch information
nuzil committed Aug 21, 2018
1 parent f0c4347 commit 09dc09c
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 0 deletions.
150 changes: 150 additions & 0 deletions app/code/Magento/CustomerGraphQl/Model/Resolver/CustomerUpdate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CustomerGraphQl\Model\Resolver;

use Magento\Authorization\Model\UserContextInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\CustomerGraphQl\Model\Resolver\Customer\CustomerDataProvider;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
use Magento\Framework\GraphQl\Query\Resolver\Value;
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Store\Api\StoreResolverInterface;
use Magento\Framework\Encryption\EncryptorInterface as Encryptor;
use Magento\Customer\Model\CustomerRegistry;


/**
* Customers field resolver, used for GraphQL request processing.
*/
class CustomerUpdate implements ResolverInterface
{
/**
* @var CustomerDataProvider
*/
private $customerResolver;

/**
* @var ValueFactory
*/
private $valueFactory;

/**
* @var StoreResolverInterface
*/
private $storeResolver;

/**
* @var \Magento\Newsletter\Model\SubscriberFactory
*/
protected $subscriberFactory;

/**
* @var AuthenticationInterface
*/
private $authentication;

/**
* @var CustomerRegistry
*/
protected $customerRegistry;

/**
* @var Encryptor
*/
protected $encryptor;

/**
* @param CustomerDataProvider $customerResolver
* @param ValueFactory $valueFactory
*/
public function __construct(
CustomerDataProvider $customerResolver,
ValueFactory $valueFactory,
\Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
\Magento\Newsletter\Model\SubscriberFactory $subscriberFactory,
StoreResolverInterface $storeResolver,
Encryptor $encryptor,
CustomerRegistry $customerRegistry
) {
$this->customerResolver = $customerResolver;
$this->valueFactory = $valueFactory;
$this->customerRepository = $customerRepository;
$this->subscriberFactory = $subscriberFactory;
$this->storeResolver = $storeResolver;
$this->encryptor = $encryptor;
$this->customerRegistry = $customerRegistry;
}

/**
* {@inheritdoc}
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
) : Value {

/** @var ContextInterface $context */
if ((!$context->getUserId()) || $context->getUserType() == UserContextInterface::USER_TYPE_GUEST) {
throw new GraphQlAuthorizationException(
__(
'Current customer does not have access to the resource "%1"',
[\Magento\Customer\Model\Customer::ENTITY]
)
);
}

$customer = $this->customerRepository->getById($context->getUserId());

if (isset($args['email']) && $customer->getEmail() !== $args['email']) {
$customerSecure = $this->customerRegistry->retrieveSecureData($context->getUserId());
$hash = $customerSecure->getPasswordHash();
if (!$this->encryptor->validateHash($args['password'], $hash)) {
throw new GraphQlAuthorizationException(__('Invalid login or password.'));
}
$customer->setEmail($args['email']);
}

if (isset($args['firstname'])) {
$customer->setFirstname($args['firstname']);
}
if (isset($args['lastname'])){
$customer->setLastname($args['lastname']);
}

$customer->setStoreId($this->storeResolver->getCurrentStoreId());
$this->customerRepository->save($customer);

if (isset($args['is_subscribed'])) {

$checkSubscriber = $this->subscriberFactory->create()->loadByCustomerId($context->getUserId());

if ($args['is_subscribed'] === true && !$checkSubscriber->isSubscribed()) {
$this->subscriberFactory->create()->subscribeCustomerById($context->getUserId());
} elseif ($args['is_subscribed'] === false && $checkSubscriber->isSubscribed()) {
$this->subscriberFactory->create()->unsubscribeCustomerById($context->getUserId());
}
}


$data = $args;
$result = function () use ($data) {
return !empty($data) ? $data : [];
};

return $this->valueFactory->create($result);


}

}
11 changes: 11 additions & 0 deletions app/code/Magento/CustomerGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,14 @@ type CustomerAddressRegion @doc(description: "CustomerAddressRegion defines the
region_id: Int @doc(description: "Uniquely identifies the region")
}

type Mutation {
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")
}

type customerItem {
firstname: String
lastname: String
email: String
password: String
is_subscribed: Boolean
}

0 comments on commit 09dc09c

Please sign in to comment.