Skip to content

Commit

Permalink
Merge pull request #3442 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
Valeriy Naida authored Nov 15, 2018
2 parents 8cc960c + a5ccff7 commit c49da73
Show file tree
Hide file tree
Showing 35 changed files with 879 additions and 37 deletions.
1 change: 1 addition & 0 deletions app/code/Magento/CatalogGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ interface CustomizableOptionInterface @typeResolver(class: "Magento\\CatalogGrap
title: String @doc(description: "The display name for this option")
required: Boolean @doc(description: "Indicates whether the option is required")
sort_order: Int @doc(description: "The order in which the option is displayed")
option_id: Int @doc(description: "Option ID")
}

interface CustomizableProductInterface @typeResolver(class: "Magento\\CatalogGraphQl\\Model\\ProductInterfaceTypeResolverComposite") @doc(description: "CustomizableProductInterface contains information about customizable product options.") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function execute(int $customerId, array $data): void

if (isset($data['email']) && $customer->getEmail() !== $data['email']) {
if (!isset($data['password']) || empty($data['password'])) {
throw new GraphQlInputException(__('For changing "email" you should provide current "password".'));
throw new GraphQlInputException(__('Provide the current "password" to change "email".'));
}

$this->checkCustomerPassword->execute($data['password'], $customerId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;

/**
* @inheritdoc
* Change customer password resolver
*/
class ChangePassword implements ResolverInterface
{
Expand Down Expand Up @@ -70,11 +70,11 @@ public function resolve(
array $args = null
) {
if (!isset($args['currentPassword'])) {
throw new GraphQlInputException(__('"currentPassword" value should be specified'));
throw new GraphQlInputException(__('Specify the "currentPassword" value.'));
}

if (!isset($args['newPassword'])) {
throw new GraphQlInputException(__('"newPassword" value should be specified'));
throw new GraphQlInputException(__('Specify the "newPassword" value.'));
}

$currentUserId = $context->getUserId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ public function resolve(
array $args = null
) {
if (!isset($args['email'])) {
throw new GraphQlInputException(__('"email" value should be specified'));
throw new GraphQlInputException(__('Specify the "email" value.'));
}

if (!isset($args['password'])) {
throw new GraphQlInputException(__('"password" value should be specified'));
throw new GraphQlInputException(__('Specify the "password" value.'));
}

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@ public function resolve(

$this->checkCustomerAccount->execute($currentUserId, $currentUserType);

return $this->customerTokenService->revokeCustomerAccessToken((int)$currentUserId);
return ['result' => $this->customerTokenService->revokeCustomerAccessToken((int)$currentUserId)];
}
}
12 changes: 8 additions & 4 deletions app/code/Magento/CustomerGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ type Query {
}

type Mutation {
generateCustomerToken(email: String!, password: String!): CustomerToken @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\GenerateCustomerToken") @doc(description:"Retrieve Customer token")
changeCustomerPassword(currentPassword: String!, newPassword: String!): Customer @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\ChangePassword") @doc(description:"Changes password for logged in customer")
updateCustomer (input: UpdateCustomerInput): UpdateCustomerOutput @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\UpdateCustomer") @doc(description:"Update customer personal information")
revokeCustomerToken: Boolean @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\RevokeCustomerToken") @doc(description:"Revoke Customer token")
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")
revokeCustomerToken: RevokeCustomerTokenOutput @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\RevokeCustomerToken") @doc(description:"Revoke the customer token")
}

type CustomerToken {
Expand All @@ -28,6 +28,10 @@ type UpdateCustomerOutput {
customer: Customer!
}

type RevokeCustomerTokenOutput {
result: Boolean!
}

type Customer @doc(description: "Customer defines the customer name and address and other details") {
created_at: String @doc(description: "Timestamp indicating when the account was created")
group_id: Int @doc(description: "The group assigned to the user. Default values are 0 (Not logged in), 1 (General), 2 (Wholesale), and 3 (Retailer)")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\QuoteGraphQl\Model\Cart\Address;

use Magento\Framework\Api\ExtensibleDataObjectConverter;
use Magento\Quote\Api\Data\AddressInterface;
use Magento\Quote\Api\Data\CartInterface;
use Magento\Quote\Model\Quote\Address as QuoteAddress;

/**
* Class AddressDataProvider
*
* Collect and return information about cart shipping and billing addresses
*/
class AddressDataProvider
{
/**
* @var ExtensibleDataObjectConverter
*/
private $dataObjectConverter;

/**
* AddressDataProvider constructor.
*
* @param ExtensibleDataObjectConverter $dataObjectConverter
*/
public function __construct(
ExtensibleDataObjectConverter $dataObjectConverter
) {
$this->dataObjectConverter = $dataObjectConverter;
}

/**
* Collect and return information about shipping and billing addresses
*
* @param CartInterface $cart
* @return array
*/
public function getCartAddresses(CartInterface $cart): array
{
$addressData = [];
$shippingAddress = $cart->getShippingAddress();
$billingAddress = $cart->getBillingAddress();

if ($shippingAddress) {
$shippingData = $this->dataObjectConverter->toFlatArray($shippingAddress, [], AddressInterface::class);
$shippingData['address_type'] = 'SHIPPING';
$addressData[] = array_merge($shippingData, $this->extractAddressData($shippingAddress));
}

if ($billingAddress) {
$billingData = $this->dataObjectConverter->toFlatArray($billingAddress, [], AddressInterface::class);
$billingData['address_type'] = 'BILLING';
$addressData[] = array_merge($billingData, $this->extractAddressData($billingAddress));
}

return $addressData;
}

/**
* Extract the necessary address fields from address model
*
* @param QuoteAddress $address
* @return array
*/
private function extractAddressData(QuoteAddress $address): array
{
$addressData = [
'country' => [
'code' => $address->getCountryId(),
'label' => $address->getCountry()
],
'region' => [
'code' => $address->getRegionCode(),
'label' => $address->getRegion()
],
'street' => $address->getStreet(),
'selected_shipping_method' => [
'code' => $address->getShippingMethod(),
'label' => $address->getShippingDescription(),
'free_shipping' => $address->getFreeShipping(),
],
'items_weight' => $address->getWeight(),
'customer_notes' => $address->getCustomerNotes()
];

return $addressData;
}
}
101 changes: 101 additions & 0 deletions app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingMethodOnCart.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\QuoteGraphQl\Model\Cart;

use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Exception\StateException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Quote\Model\Quote;
use Magento\Quote\Model\Quote\AddressFactory as QuoteAddressFactory;
use Magento\Quote\Model\ResourceModel\Quote\Address as QuoteAddressResource;
use Magento\Checkout\Model\ShippingInformationFactory;
use Magento\Checkout\Api\ShippingInformationManagementInterface;
use Magento\Checkout\Model\ShippingInformation;

/**
* Class SetShippingMethodsOnCart
*
* Set shipping method for a specified shopping cart address
*/
class SetShippingMethodOnCart
{
/**
* @var ShippingInformationFactory
*/
private $shippingInformationFactory;

/**
* @var QuoteAddressFactory
*/
private $quoteAddressFactory;

/**
* @var QuoteAddressResource
*/
private $quoteAddressResource;

/**
* @var ShippingInformationManagementInterface
*/
private $shippingInformationManagement;

/**
* @param ShippingInformationManagementInterface $shippingInformationManagement
* @param QuoteAddressFactory $quoteAddressFactory
* @param QuoteAddressResource $quoteAddressResource
* @param ShippingInformationFactory $shippingInformationFactory
*/
public function __construct(
ShippingInformationManagementInterface $shippingInformationManagement,
QuoteAddressFactory $quoteAddressFactory,
QuoteAddressResource $quoteAddressResource,
ShippingInformationFactory $shippingInformationFactory
) {
$this->shippingInformationManagement = $shippingInformationManagement;
$this->quoteAddressResource = $quoteAddressResource;
$this->quoteAddressFactory = $quoteAddressFactory;
$this->shippingInformationFactory = $shippingInformationFactory;
}

/**
* Sets shipping method for a specified shopping cart address
*
* @param Quote $cart
* @param int $cartAddressId
* @param string $carrierCode
* @param string $methodCode
* @throws GraphQlInputException
* @throws GraphQlNoSuchEntityException
*/
public function execute(Quote $cart, int $cartAddressId, string $carrierCode, string $methodCode): void
{
$quoteAddress = $this->quoteAddressFactory->create();
$this->quoteAddressResource->load($quoteAddress, $cartAddressId);

/** @var ShippingInformation $shippingInformation */
$shippingInformation = $this->shippingInformationFactory->create();

/* If the address is not a shipping address (but billing) the system will find the proper shipping address for
the selected cart and set the information there (actual for single shipping address) */
$shippingInformation->setShippingAddress($quoteAddress);
$shippingInformation->setShippingCarrierCode($carrierCode);
$shippingInformation->setShippingMethodCode($methodCode);

try {
$this->shippingInformationManagement->saveAddressInformation($cart->getId(), $shippingInformation);
} catch (NoSuchEntityException $exception) {
throw new GraphQlNoSuchEntityException(__($exception->getMessage()));
} catch (StateException $exception) {
throw new GraphQlInputException(__($exception->getMessage()));
} catch (InputException $exception) {
throw new GraphQlInputException(__($exception->getMessage()));
}
}
}
48 changes: 48 additions & 0 deletions app/code/Magento/QuoteGraphQl/Model/Resolver/CartAddresses.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\QuoteGraphQl\Model\Resolver;

use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\QuoteGraphQl\Model\Cart\Address\AddressDataProvider;

/**
* @inheritdoc
*/
class CartAddresses implements ResolverInterface
{
/**
* @var AddressDataProvider
*/
private $addressDataProvider;

/**
* @param AddressDataProvider $addressDataProvider
*/
public function __construct(
AddressDataProvider $addressDataProvider
) {
$this->addressDataProvider = $addressDataProvider;
}

/**
* @inheritdoc
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
if (!isset($value['model'])) {
throw new LocalizedException(__('"model" value should be specified'));
}

$cart = $value['model'];

return $this->addressDataProvider->getCartAddresses($cart);
}
}
Loading

0 comments on commit c49da73

Please sign in to comment.