Skip to content

Commit

Permalink
Merge pull request magento#3678 from magento-engcom/graphql-develop-prs
Browse files Browse the repository at this point in the history
[EngCom] Public Pull Requests - GraphQL
  • Loading branch information
naydav authored Feb 1, 2019
2 parents 68a02ca + b56e52e commit 664a673
Show file tree
Hide file tree
Showing 13 changed files with 738 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public function __construct(
* Add parent id/sku pair to use for option filter at fetch time.
*
* @param int $parentId
* @param int $parentEntityId
* @param string $sku
*/
public function addParentFilterData(int $parentId, int $parentEntityId, string $sku) : void
Expand Down
11 changes: 11 additions & 0 deletions app/code/Magento/CatalogGraphQl/Model/Category/LevelCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@
*/
class LevelCalculator
{
/**
* @var ResourceConnection
*/
private $resourceConnection;

/**
* @var Category
*/
private $resourceCategory;

/**
* @param ResourceConnection $resourceConnection
* @param Category $resourceCategory
Expand All @@ -39,6 +49,7 @@ public function calculate(int $rootCategoryId) : int
$select = $connection->select()
->from($this->resourceConnection->getTableName('catalog_category_entity'), 'level')
->where($this->resourceCategory->getLinkField() . " = ?", $rootCategoryId);

return (int) $connection->fetchOne($select);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,25 @@
namespace Magento\ConfigurableProductGraphQl\Model;

use Magento\Framework\GraphQl\Query\Resolver\TypeResolverInterface;
use Magento\ConfigurableProduct\Model\Product\Type\Configurable as Type;

/**
* {@inheritdoc}
* @inheritdoc
*/
class ConfigurableProductTypeResolver implements TypeResolverInterface
{
/**
* {@inheritdoc}
* Configurable product type resolver code
*/
public function resolveType(array $data) : string
const TYPE_RESOLVER = 'ConfigurableProduct';

/**
* @inheritdoc
*/
public function resolveType(array $data): string
{
if (isset($data['type_id']) && $data['type_id'] == 'configurable') {
return 'ConfigurableProduct';
if (isset($data['type_id']) && $data['type_id'] == Type::TYPE_CODE) {
return self::TYPE_RESOLVER;
}
return '';
}
Expand Down
85 changes: 85 additions & 0 deletions app/code/Magento/CustomerGraphQl/Model/Customer/CreateAccount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CustomerGraphQl\Model\Customer;

use Magento\Customer\Api\AccountManagementInterface;
use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Customer\Api\Data\CustomerInterfaceFactory;
use Magento\Framework\Api\DataObjectHelper;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Store\Model\StoreManagerInterface;

/**
* Class CreateAccount creates new customer account
*/
class CreateAccount
{
/**
* @var DataObjectHelper
*/
private $dataObjectHelper;

/**
* @var CustomerInterfaceFactory
*/
private $customerFactory;

/**
* @var AccountManagementInterface
*/
private $accountManagement;

/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* @param DataObjectHelper $dataObjectHelper
* @param CustomerInterfaceFactory $customerFactory
* @param StoreManagerInterface $storeManager
* @param AccountManagementInterface $accountManagement
*/
public function __construct(
DataObjectHelper $dataObjectHelper,
CustomerInterfaceFactory $customerFactory,
StoreManagerInterface $storeManager,
AccountManagementInterface $accountManagement
) {
$this->dataObjectHelper = $dataObjectHelper;
$this->customerFactory = $customerFactory;
$this->accountManagement = $accountManagement;
$this->storeManager = $storeManager;
}

/**
* Creates new customer account
*
* @param array $args
* @return CustomerInterface
* @throws LocalizedException
* @throws NoSuchEntityException
*/
public function execute(array $args): CustomerInterface
{
$customerDataObject = $this->customerFactory->create();
$this->dataObjectHelper->populateWithArray(
$customerDataObject,
$args['input'],
CustomerInterface::class
);
$store = $this->storeManager->getStore();
$customerDataObject->setWebsiteId($store->getWebsiteId());
$customerDataObject->setStoreId($store->getId());

$password = array_key_exists('password', $args['input']) ? $args['input']['password'] : null;

return $this->accountManagement->createAccount($customerDataObject, $password);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CustomerGraphQl\Model\Customer;

use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
use Magento\Authorization\Model\UserContextInterface;

/**
* Set up user context after creating new customer account
*/
class SetUpUserContext
{
/**
* Set up user context after creating new customer account
*
* @param ContextInterface $context
* @param CustomerInterface $customer
*/
public function execute(ContextInterface $context, CustomerInterface $customer)
{
$context->setUserId((int)$customer->getId());
$context->setUserType(UserContextInterface::USER_TYPE_CUSTOMER);
}
}
95 changes: 95 additions & 0 deletions app/code/Magento/CustomerGraphQl/Model/Resolver/CreateCustomer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CustomerGraphQl\Model\Resolver;

use Magento\CustomerGraphQl\Model\Customer\ChangeSubscriptionStatus;
use Magento\CustomerGraphQl\Model\Customer\CreateAccount;
use Magento\CustomerGraphQl\Model\Customer\CustomerDataProvider;
use Magento\CustomerGraphQl\Model\Customer\SetUpUserContext;
use Magento\Framework\Exception\State\InputMismatchException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\Validator\Exception as ValidatorException;

/**
* Create customer account resolver
*/
class CreateCustomer implements ResolverInterface
{
/**
* @var CustomerDataProvider
*/
private $customerDataProvider;

/**
* @var ChangeSubscriptionStatus
*/
private $changeSubscriptionStatus;

/**
* @var CreateAccount
*/
private $createAccount;

/**
* @var SetUpUserContext
*/
private $setUpUserContext;

/**
* @param CustomerDataProvider $customerDataProvider
* @param ChangeSubscriptionStatus $changeSubscriptionStatus
* @param SetUpUserContext $setUpUserContext
* @param CreateAccount $createAccount
*/
public function __construct(
CustomerDataProvider $customerDataProvider,
ChangeSubscriptionStatus $changeSubscriptionStatus,
SetUpUserContext $setUpUserContext,
CreateAccount $createAccount
) {
$this->customerDataProvider = $customerDataProvider;
$this->changeSubscriptionStatus = $changeSubscriptionStatus;
$this->createAccount = $createAccount;
$this->setUpUserContext = $setUpUserContext;
}

/**
* @inheritdoc
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
) {
if (!isset($args['input']) || !is_array($args['input']) || empty($args['input'])) {
throw new GraphQlInputException(__('"input" value should be specified'));
}
try {
$customer = $this->createAccount->execute($args);
$customerId = (int)$customer->getId();
$this->setUpUserContext->execute($context, $customer);
if (array_key_exists('is_subscribed', $args['input'])) {
if ($args['input']['is_subscribed']) {
$this->changeSubscriptionStatus->execute($customerId, true);
}
}
$data = $this->customerDataProvider->getCustomerById($customerId);
} catch (ValidatorException $e) {
throw new GraphQlInputException(__($e->getMessage()));
} catch (InputMismatchException $e) {
throw new GraphQlInputException(__($e->getMessage()));
}

return ['customer' => $data];
}
}
25 changes: 16 additions & 9 deletions app/code/Magento/CustomerGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ type Query {
type Mutation {
generateCustomerToken(email: String!, password: String!): CustomerToken @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\GenerateCustomerToken") @doc(description:"Retrieve the customer token")
changeCustomerPassword(currentPassword: String!, newPassword: String!): Customer @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\ChangePassword") @doc(description:"Changes the password for the logged-in customer")
updateCustomer (input: UpdateCustomerInput!): UpdateCustomerOutput @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\UpdateCustomer") @doc(description:"Update the customer's personal information")
createCustomer (input: CustomerInput!): CustomerOutput @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\CreateCustomer") @doc(description:"Create customer account")
updateCustomer (input: CustomerInput!): CustomerOutput @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\UpdateCustomer") @doc(description:"Update the customer's personal information")
revokeCustomerToken: RevokeCustomerTokenOutput @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\RevokeCustomerToken") @doc(description:"Revoke the customer token")
createCustomerAddress(input: CustomerAddressInput!): CustomerAddress @resolver(class: "Magento\\CustomerGraphQl\\Model\\Resolver\\CreateCustomerAddress") @doc(description: "Create customer address")
updateCustomerAddress(id: Int!, input: CustomerAddressInput): CustomerAddress @resolver(class: "Magento\\CustomerGraphQl\\Model\\Resolver\\UpdateCustomerAddress") @doc(description: "Update customer address")
Expand Down Expand Up @@ -50,15 +51,21 @@ type CustomerToken {
token: String @doc(description: "The customer token")
}

input UpdateCustomerInput {
firstname: String
lastname: String
email: String
password: String
is_subscribed: Boolean
input CustomerInput {
prefix: String @doc(description: "An honorific, such as Dr., Mr., or Mrs.")
firstname: String @doc(description: "The customer's first name")
middlename: String @doc(description: "The customer's middle name")
lastname: String @doc(description: "The customer's family name")
suffix: String @doc(description: "A value such as Sr., Jr., or III")
email: String @doc(description: "The customer's email address. Required")
dob: String @doc(description: "The customer's date of birth")
taxvat: String @doc(description: "The customer's Tax/VAT number (for corporate customers)")
gender: Int @doc(description: "The customer's gender(Male - 1, Female - 2)")
password: String @doc(description: "The customer's password")
is_subscribed: Boolean @doc(description: "Indicates whether the customer is subscribed to the company's newsletter")
}

type UpdateCustomerOutput {
type CustomerOutput {
customer: Customer!
}

Expand Down Expand Up @@ -365,4 +372,4 @@ enum CountryCodeEnum @doc(description: "The list of countries codes") {
YE @doc(description: "Yemen")
ZM @doc(description: "Zambia")
ZW @doc(description: "Zimbabwe")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@ public function execute(Quote $cart): array
];
}

$appliedCoupon = $cart->getCouponCode();

return [
'items' => $items,
'applied_coupon' => $appliedCoupon ? ['code' => $appliedCoupon] : null
];
}
}
Loading

0 comments on commit 664a673

Please sign in to comment.