Skip to content
This repository has been archived by the owner on Dec 19, 2019. It is now read-only.

Commit

Permalink
Merge pull request #3848 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 Mar 6, 2019
2 parents 74ebef8 + a1fa332 commit 75cf826
Show file tree
Hide file tree
Showing 38 changed files with 1,134 additions and 237 deletions.
4 changes: 4 additions & 0 deletions app/code/Magento/CatalogGraphQl/etc/graphql/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,15 @@
</item>
<item name="customizable_options" xsi:type="array">
<item name="field" xsi:type="string">CustomizableFieldOption</item>
<item name="date" xsi:type="string">CustomizableDateOption</item>
<item name="date_time" xsi:type="string">CustomizableDateOption</item>
<item name="time" xsi:type="string">CustomizableDateOption</item>
<item name="file" xsi:type="string">CustomizableFileOption</item>
<item name="area" xsi:type="string">CustomizableAreaOption</item>
<item name="drop_down" xsi:type="string">CustomizableDropDownOption</item>
<item name="multiple" xsi:type="string">CustomizableMultipleOption</item>
<item name="radio" xsi:type="string">CustomizableRadioOption</item>
<item name="checkbox" xsi:type="string">CustomizableCheckboxOption</item>
</item>
</argument>
</arguments>
Expand Down
26 changes: 26 additions & 0 deletions app/code/Magento/CatalogGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,19 @@ type CustomizableDropDownValue @doc(description: "CustomizableDropDownValue defi
sort_order: Int @doc(description: "The order in which the option is displayed")
}

type CustomizableMultipleOption implements CustomizableOptionInterface @doc(description: "CustomizableMultipleOption contains information about a multiselect that is defined as part of a customizable option") {
value: [CustomizableMultipleValue] @doc(description: "An array that defines the set of options for a multiselect")
}

type CustomizableMultipleValue @doc(description: "CustomizableMultipleValue defines the price and sku of a product whose page contains a customized multiselect") {
option_type_id: Int @doc(description: "The ID assigned to the value")
price: Float @doc(description: "The price assigned to this option")
price_type: PriceTypeEnum @doc(description: "FIXED, PERCENT, or DYNAMIC")
sku: String @doc(description: "The Stock Keeping Unit for this option")
title: String @doc(description: "The display name for this option")
sort_order: Int @doc(description: "The order in which the option is displayed")
}

type CustomizableFieldOption implements CustomizableOptionInterface @doc(description: "CustomizableFieldOption contains information about a text field that is defined as part of a customizable option") {
value: CustomizableFieldValue @doc(description: "An object that defines a text field")
product_sku: String @doc(description: "The Stock Keeping Unit of the base product")
Expand Down Expand Up @@ -407,6 +420,19 @@ type CustomizableRadioValue @doc(description: "CustomizableRadioValue defines t
sort_order: Int @doc(description: "The order in which the radio button is displayed")
}

type CustomizableCheckboxOption implements CustomizableOptionInterface @doc(description: "CustomizableCheckbbixOption contains information about a set of checkbox values that are defined as part of a customizable option") {
value: [CustomizableCheckboxValue] @doc(description: "An array that defines a set of checkbox values")
}

type CustomizableCheckboxValue @doc(description: "CustomizableCheckboxValue defines the price and sku of a product whose page contains a customized set of checkbox values") {
option_type_id: Int @doc(description: "The ID assigned to the value")
price: Float @doc(description: "The price assigned to this option")
price_type: PriceTypeEnum @doc(description: "FIXED, PERCENT, or DYNAMIC")
sku: String @doc(description: "The Stock Keeping Unit for this option")
title: String @doc(description: "The display name for this option")
sort_order: Int @doc(description: "The order in which the checkbox value is displayed")
}

type VirtualProduct implements ProductInterface, CustomizableProductInterface @doc(description: "A virtual product is non-tangible product that does not require shipping and is not kept in inventory") {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Quote\Api\Data\AddressInterface;
use Magento\Quote\Api\Data\CartInterface;
use Magento\Quote\Api\BillingAddressManagementInterface;
use Magento\Quote\Model\Quote\Address as QuoteAddress;

/**
* Set billing address for a specified shopping cart
Expand All @@ -38,22 +38,22 @@ public function __construct(
* Assign billing address to cart
*
* @param CartInterface $cart
* @param QuoteAddress $billingAddress
* @param AddressInterface $billingAddress
* @param bool $useForShipping
* @throws GraphQlInputException
* @throws GraphQlNoSuchEntityException
*/
public function execute(
CartInterface $cart,
QuoteAddress $billingAddress,
AddressInterface $billingAddress,
bool $useForShipping
): void {
try {
$this->billingAddressManagement->assign($cart->getId(), $billingAddress, $useForShipping);
} catch (NoSuchEntityException $e) {
throw new GraphQlNoSuchEntityException(__($e->getMessage()));
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
} catch (LocalizedException $e) {
throw new GraphQlInputException(__($e->getMessage()));
throw new GraphQlInputException(__($e->getMessage()), $e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Quote\Api\Data\AddressInterface;
use Magento\Quote\Api\Data\CartInterface;
use Magento\Quote\Model\Quote\Address as QuoteAddress;
use Magento\Quote\Model\ShippingAddressManagementInterface;

/**
Expand All @@ -38,20 +38,20 @@ public function __construct(
* Assign shipping address to cart
*
* @param CartInterface $cart
* @param QuoteAddress $shippingAddress
* @param AddressInterface $shippingAddress
* @throws GraphQlInputException
* @throws GraphQlNoSuchEntityException
*/
public function execute(
CartInterface $cart,
QuoteAddress $shippingAddress
AddressInterface $shippingAddress
): void {
try {
$this->shippingAddressManagement->assign($cart->getId(), $shippingAddress);
} catch (NoSuchEntityException $e) {
throw new GraphQlNoSuchEntityException(__($e->getMessage()));
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
} catch (LocalizedException $e) {
throw new GraphQlInputException(__($e->getMessage()));
throw new GraphQlInputException(__($e->getMessage()), $e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\QuoteGraphQl\Model\Cart;

use Magento\Checkout\Api\Data\ShippingInformationInterface;
use Magento\Checkout\Api\Data\ShippingInformationInterfaceFactory;
use Magento\Checkout\Api\ShippingInformationManagementInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Quote\Api\Data\AddressInterface;
use Magento\Quote\Api\Data\CartInterface;

/**
* Assign shipping method to cart
*/
class AssignShippingMethodToCart
{
/**
* @var ShippingInformationInterfaceFactory
*/
private $shippingInformationFactory;

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

/**
* @param ShippingInformationInterfaceFactory $shippingInformationFactory
* @param ShippingInformationManagementInterface $shippingInformationManagement
*/
public function __construct(
ShippingInformationInterfaceFactory $shippingInformationFactory,
ShippingInformationManagementInterface $shippingInformationManagement
) {
$this->shippingInformationFactory = $shippingInformationFactory;
$this->shippingInformationManagement = $shippingInformationManagement;
}

/**
* Assign shipping method to cart
*
* @param CartInterface $cart
* @param AddressInterface $quoteAddress
* @param string $carrierCode
* @param string $methodCode
* @throws GraphQlInputException
* @throws GraphQlNoSuchEntityException
*/
public function execute(
CartInterface $cart,
AddressInterface $quoteAddress,
string $carrierCode,
string $methodCode
): void {
/** @var ShippingInformationInterface $shippingInformation */
$shippingInformation = $this->shippingInformationFactory->create([
'data' => [
/* 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) */
ShippingInformationInterface::SHIPPING_ADDRESS => $quoteAddress,
ShippingInformationInterface::SHIPPING_CARRIER_CODE => $carrierCode,
ShippingInformationInterface::SHIPPING_METHOD_CODE => $methodCode,
],
]);

try {
$this->shippingInformationManagement->saveAddressInformation($cart->getId(), $shippingInformation);
} catch (NoSuchEntityException $e) {
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
} catch (LocalizedException $e) {
throw new GraphQlInputException(__($e->getMessage()), $e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Magento\QuoteGraphQl\Model\Cart;

use Magento\Customer\Model\Address\AbstractAddress;
use Magento\Framework\Api\ExtensibleDataObjectConverter;
use Magento\Quote\Api\Data\AddressInterface;
use Magento\Quote\Model\Quote\Address as QuoteAddress;
Expand Down Expand Up @@ -40,8 +41,17 @@ public function execute(QuoteAddress $address): array
$addressData = $this->dataObjectConverter->toFlatArray($address, [], AddressInterface::class);
$addressData['model'] = $address;

if ($address->getAddressType() == AbstractAddress::TYPE_SHIPPING) {
$addressType = 'SHIPPING';
} elseif ($address->getAddressType() == AbstractAddress::TYPE_BILLING) {
$addressType = 'BILLING';
} else {
$addressType = null;
}

$addressData = array_merge($addressData, [
'address_id' => $address->getId(),
'address_type' => $addressType,
'country' => [
'code' => $address->getCountryId(),
'label' => $address->getCountry()
Expand All @@ -51,12 +61,6 @@ public function execute(QuoteAddress $address): array
'label' => $address->getRegion()
],
'street' => $address->getStreet(),
'selected_shipping_method' => [
'code' => $address->getShippingMethod(),
'label' => $address->getShippingDescription(),
'free_shipping' => $address->getFreeShipping(),
'amount' => $address->getShippingAmount()
],
'items_weight' => $address->getWeight(),
'customer_notes' => $address->getCustomerNotes()
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;

/**
* Get customer address. Throws exception if customer is not owner of address
* Get customer address
*/
class GetCustomerAddress
{
Expand Down Expand Up @@ -52,7 +52,7 @@ public function execute(int $addressId, int $customerId): AddressInterface
__('Could not find a address with ID "%address_id"', ['address_id' => $addressId])
);
} catch (LocalizedException $e) {
throw new GraphQlInputException(__($e->getMessage()));
throw new GraphQlInputException(__($e->getMessage()), $e);
}

if ((int)$customerAddress->getCustomerId() !== $customerId) {
Expand Down
82 changes: 82 additions & 0 deletions app/code/Magento/QuoteGraphQl/Model/Cart/GetQuoteAddress.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?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\GraphQl\Exception\GraphQlAuthorizationException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Quote\Api\Data\AddressInterface;
use Magento\Quote\Api\Data\AddressInterfaceFactory;
use Magento\Quote\Model\ResourceModel\Quote\Address as AddressResource;

/**
* Get quote address
*/
class GetQuoteAddress
{
/**
* @var AddressInterfaceFactory
*/
private $quoteAddressFactory;

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

/**
* @param AddressInterfaceFactory $quoteAddressFactory
* @param AddressResource $quoteAddressResource
*/
public function __construct(
AddressInterfaceFactory $quoteAddressFactory,
AddressResource $quoteAddressResource
) {
$this->quoteAddressFactory = $quoteAddressFactory;
$this->quoteAddressResource = $quoteAddressResource;
}

/**
* Get quote address
*
* @param int $quoteAddressId
* @param int|null $customerId
* @return AddressInterface
* @throws GraphQlInputException
* @throws GraphQlNoSuchEntityException
* @throws GraphQlAuthorizationException
*/
public function execute(int $quoteAddressId, ?int $customerId): AddressInterface
{
$quoteAddress = $this->quoteAddressFactory->create();

$this->quoteAddressResource->load($quoteAddress, $quoteAddressId);
if (null === $quoteAddress->getId()) {
throw new GraphQlNoSuchEntityException(
__('Could not find a cart address with ID "%cart_address_id"', ['cart_address_id' => $quoteAddressId])
);
}

$quoteAddressCustomerId = (int)$quoteAddress->getCustomerId();

/* Guest cart, allow operations */
if (!$quoteAddressCustomerId && null === $customerId) {
return $quoteAddress;
}

if ($quoteAddressCustomerId !== $customerId) {
throw new GraphQlAuthorizationException(
__(
'The current user cannot use cart address with ID "%cart_address_id"',
['cart_address_id' => $quoteAddressId]
)
);
}
return $quoteAddress;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function createBasedOnCustomerAddress(CustomerAddress $customerAddress):
try {
$quoteAddress->importCustomerAddressData($customerAddress);
} catch (LocalizedException $e) {
throw new GraphQlInputException(__($e->getMessage()));
throw new GraphQlInputException(__($e->getMessage()), $e);
}
return $quoteAddress;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,19 @@ public function __construct(
*
* @param ContextInterface $context
* @param CartInterface $cart
* @param array $billingAddress
* @param array $billingAddressInput
* @return void
* @throws GraphQlInputException
* @throws GraphQlAuthenticationException
* @throws GraphQlAuthorizationException
* @throws GraphQlNoSuchEntityException
*/
public function execute(ContextInterface $context, CartInterface $cart, array $billingAddress): void
public function execute(ContextInterface $context, CartInterface $cart, array $billingAddressInput): void
{
$customerAddressId = $billingAddress['customer_address_id'] ?? null;
$addressInput = $billingAddress['address'] ?? null;
$useForShipping = isset($billingAddress['use_for_shipping'])
? (bool)$billingAddress['use_for_shipping'] : false;
$customerAddressId = $billingAddressInput['customer_address_id'] ?? null;
$addressInput = $billingAddressInput['address'] ?? null;
$useForShipping = isset($billingAddressInput['use_for_shipping'])
? (bool)$billingAddressInput['use_for_shipping'] : false;

if (null === $customerAddressId && null === $addressInput) {
throw new GraphQlInputException(
Expand Down
Loading

0 comments on commit 75cf826

Please sign in to comment.