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

Commit

Permalink
#141 Merge simple-product-graphql module with quote-graphql module
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman Glushko committed Sep 7, 2018
1 parent 236ce9a commit 49b0820
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
*/
declare(strict_types=1);

namespace Magento\SimpleProductGraphQl\Model\Resolver\Cart;
namespace Magento\QuoteGraphQl\Model\Resolver\Cart;

use Magento\Authorization\Model\UserContextInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\DataObject;
use Magento\Framework\DataObjectFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
Expand Down Expand Up @@ -134,12 +135,20 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value

foreach ($cartItems as $cartItem) {
$sku = $this->arrayManager->get('details/sku', $cartItem);
$product = $this->productRepository->get($sku);
$product = $this->productRepository->get($sku);

$cart->addProduct($product, $this->getBuyRequest($cartItem));
$message = $cart->addProduct($product, $this->getBuyRequest($cartItem));

if (is_string($message)) {
throw new GraphQlInputException(
__('%1: %2', $sku, $message)
);
}

if ($cart->getData('has_error')) {
throw new GraphQlInputException(__('Cart has an error: %1 (%2)', $this->getCartErrors($cart), $sku));
throw new GraphQlInputException(
__('%1: %2', $sku, $this->getCartErrors($cart))
);
}
}

Expand All @@ -162,9 +171,9 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
*/
private function getBuyRequest($cartItem): DataObject
{
$customOptions = [];
$qty = $this->arrayManager->get('details/qty', $cartItem);
$customizableOptions = $this->arrayManager->get('customizable_options', $cartItem, []);
$customOptions = [];

foreach ($customizableOptions as $customizableOption) {
$customOptions[$customizableOption['id']] = $customizableOption['value'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
use Magento\Catalog\Model\Product\Option\Type\DefaultType as DefaultOptionType;
use Magento\Catalog\Model\Product\Option\Type\Select as SelectOptionType;
use Magento\Catalog\Model\Product\Option\Type\Text as TextOptionType;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\Resolver\Value;
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
Expand Down Expand Up @@ -103,6 +105,8 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
* @param QuoteItem $cartItem
* @param int $optionId
* @return array
* @throws NoSuchEntityException
* @throws LocalizedException
*/
private function getOptionData($cartItem, int $optionId): array
{
Expand Down Expand Up @@ -204,6 +208,8 @@ private function getOptionData($cartItem, int $optionId): array

/**
* @param string $priceType
* @return string
* @throws NoSuchEntityException
*/
private function getPriceValueUnits(string $priceType): string
{
Expand All @@ -216,8 +222,8 @@ private function getPriceValueUnits(string $priceType): string

/**
* Get currency symbol
*
* @return string
* @throws NoSuchEntityException
*/
private function getCurrencySymbol(): string
{
Expand Down
7 changes: 7 additions & 0 deletions app/code/Magento/QuoteGraphQl/etc/graphql/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,11 @@
</argument>
</arguments>
</type>
<type name="Magento\QuoteGraphQl\Model\Resolver\CartItemTypeResolver">
<arguments>
<argument name="cartItemTypes" xsi:type="array">
<item name="simple" xsi:type="string">SimpleCartItem</item>
</argument>
</arguments>
</type>
</config>
24 changes: 24 additions & 0 deletions app/code/Magento/QuoteGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,30 @@

type Mutation {
createEmptyCart: String @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\Cart\\CreateEmptyCart") @doc(description:"Creates empty shopping cart for guest or logged in user")
addSimpleProductsToCart(input: AddSimpleProductsToCartInput): AddSimpleProductsToCartOutput @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\Cart\\AddSimpleProductsToCart")
}

input AddSimpleProductsToCartInput {
cart_id: String!
cartItems: [SimpleProductCartItemInput!]!
}

input SimpleProductCartItemInput {
details: CartItemDetailsInput!
customizable_options:[CustomizableOptionInput!]
}

input CustomizableOptionInput {
id: Int!
value: String!
}

type AddSimpleProductsToCartOutput {
cart: Cart!
}

type SimpleCartItem implements CartItemInterface @doc(description: "Simple Cart Item") {
customizable_options: [SelectedCustomizableOption] @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\CartItem\\CustomizableOptions")
}

type Cart {
Expand Down
26 changes: 0 additions & 26 deletions app/code/Magento/SimpleProductGraphQl/composer.json

This file was deleted.

16 changes: 0 additions & 16 deletions app/code/Magento/SimpleProductGraphQl/etc/graphql/di.xml

This file was deleted.

15 changes: 0 additions & 15 deletions app/code/Magento/SimpleProductGraphQl/etc/module.xml

This file was deleted.

29 changes: 0 additions & 29 deletions app/code/Magento/SimpleProductGraphQl/etc/schema.graphqls

This file was deleted.

14 changes: 0 additions & 14 deletions app/code/Magento/SimpleProductGraphQl/registration.php

This file was deleted.

0 comments on commit 49b0820

Please sign in to comment.