Skip to content

Commit

Permalink
Merge pull request #4400 from magento-engcom/graphql-develop-prs
Browse files Browse the repository at this point in the history
[Magento Community Engineering] Community Contributions - GraphQL
  • Loading branch information
naydav authored Jun 25, 2019
2 parents 5027a80 + 275699f commit 9fc254f
Show file tree
Hide file tree
Showing 25 changed files with 1,095 additions and 264 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CatalogGraphQl\Model\Resolver\Category;

use Magento\Catalog\Api\Data\CategoryInterface;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Framework\EntityManager\MetadataPool;
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;

/**
* Check if category is active.
*/
class CheckCategoryIsActive
{
/**
* @var CollectionFactory
*/
private $collectionFactory;

/**
* @var MetadataPool
*/
private $metadata;

/**
* @param CollectionFactory $collectionFactory
* @param MetadataPool $metadata
*/
public function __construct(
CollectionFactory $collectionFactory,
MetadataPool $metadata
) {
$this->collectionFactory = $collectionFactory;
$this->metadata = $metadata;
}

/**
* Check if category is active.
*
* @param int $categoryId
* @throws GraphQlNoSuchEntityException
*/
public function execute(int $categoryId): void
{
$collection = $this->collectionFactory->create();
$collection->addAttributeToFilter(CategoryInterface::KEY_IS_ACTIVE, ['eq' => 1])
->getSelect()
->where(
$collection->getSelect()
->getConnection()
->quoteIdentifier(
'e.' .
$this->metadata->getMetadata(CategoryInterface::class)->getIdentifierField()
) . ' = ?',
$categoryId
);

if ($collection->count() === 0) {
throw new GraphQlNoSuchEntityException(__('Category doesn\'t exist'));
}
}
}
15 changes: 14 additions & 1 deletion app/code/Magento/CatalogGraphQl/Model/Resolver/CategoryTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace Magento\CatalogGraphQl\Model\Resolver;

use Magento\Catalog\Model\Category;
use Magento\CatalogGraphQl\Model\Resolver\Category\CheckCategoryIsActive;
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\ExtractDataFromCategoryTree;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\GraphQl\Config\Element\Field;
Expand Down Expand Up @@ -34,16 +36,24 @@ class CategoryTree implements ResolverInterface
*/
private $extractDataFromCategoryTree;

/**
* @var CheckCategoryIsActive
*/
private $checkCategoryIsActive;

/**
* @param \Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\CategoryTree $categoryTree
* @param ExtractDataFromCategoryTree $extractDataFromCategoryTree
* @param CheckCategoryIsActive $checkCategoryIsActive
*/
public function __construct(
\Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\CategoryTree $categoryTree,
ExtractDataFromCategoryTree $extractDataFromCategoryTree
ExtractDataFromCategoryTree $extractDataFromCategoryTree,
CheckCategoryIsActive $checkCategoryIsActive
) {
$this->categoryTree = $categoryTree;
$this->extractDataFromCategoryTree = $extractDataFromCategoryTree;
$this->checkCategoryIsActive = $checkCategoryIsActive;
}

/**
Expand Down Expand Up @@ -72,6 +82,9 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
}

$rootCategoryId = $this->getCategoryId($args);
if ($rootCategoryId !== Category::TREE_ROOT_ID) {
$this->checkCategoryIsActive->execute($rootCategoryId);
}
$categoriesTree = $this->categoryTree->getTree($info, $rootCategoryId);

if (empty($categoriesTree) || ($categoriesTree->count() == 0)) {
Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/GraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type Mutation {

input FilterTypeInput @doc(description: "FilterTypeInput specifies which action will be performed in a query ") {
eq: String @doc(description: "Equals")
finset: [String] @doc(description: "Find in set. The value can contain a set of comma-separated values")
finset: [String] @deprecated (reason: "The finset filter is deprecated. Magento doesn't recomend to store comma separated values, therefore finset filter is redundant.")
from: String @doc(description: "From. Must be used with 'to'")
gt: String @doc(description: "Greater than")
gteq: String @doc(description: "Greater than or equal to")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
namespace Magento\QuoteGraphQl\Model\Cart;

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\DataObject;
use Magento\Framework\DataObjectFactory;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
Expand All @@ -21,25 +19,25 @@
class AddSimpleProductToCart
{
/**
* @var DataObjectFactory
* @var CreateBuyRequest
*/
private $dataObjectFactory;
private $createBuyRequest;

/**
* @var ProductRepositoryInterface
*/
private $productRepository;

/**
* @param DataObjectFactory $dataObjectFactory
* @param ProductRepositoryInterface $productRepository
* @param CreateBuyRequest $createBuyRequest
*/
public function __construct(
DataObjectFactory $dataObjectFactory,
ProductRepositoryInterface $productRepository
ProductRepositoryInterface $productRepository,
CreateBuyRequest $createBuyRequest
) {
$this->dataObjectFactory = $dataObjectFactory;
$this->productRepository = $productRepository;
$this->createBuyRequest = $createBuyRequest;
}

/**
Expand All @@ -56,7 +54,7 @@ public function execute(Quote $cart, array $cartItemData): void
{
$sku = $this->extractSku($cartItemData);
$quantity = $this->extractQuantity($cartItemData);
$customizableOptions = $this->extractCustomizableOptions($cartItemData);
$customizableOptions = $cartItemData['customizable_options'] ?? [];

try {
$product = $this->productRepository->get($sku);
Expand All @@ -65,7 +63,7 @@ public function execute(Quote $cart, array $cartItemData): void
}

try {
$result = $cart->addProduct($product, $this->createBuyRequest($quantity, $customizableOptions));
$result = $cart->addProduct($product, $this->createBuyRequest->execute($quantity, $customizableOptions));
} catch (\Exception $e) {
throw new GraphQlInputException(
__(
Expand Down Expand Up @@ -116,60 +114,4 @@ private function extractQuantity(array $cartItemData): float
}
return $quantity;
}

/**
* Extract Customizable Options from cart item data
*
* @param array $cartItemData
* @return array
*/
private function extractCustomizableOptions(array $cartItemData): array
{
if (!isset($cartItemData['customizable_options']) || empty($cartItemData['customizable_options'])) {
return [];
}

$customizableOptionsData = [];
foreach ($cartItemData['customizable_options'] as $customizableOption) {
if (isset($customizableOption['value_string'])) {
$customizableOptionsData[$customizableOption['id']] = $this->convertCustomOptionValue(
$customizableOption['value_string']
);
}
}
return $customizableOptionsData;
}

/**
* Format GraphQl input data to a shape that buy request has
*
* @param float $quantity
* @param array $customOptions
* @return DataObject
*/
private function createBuyRequest(float $quantity, array $customOptions): DataObject
{
return $this->dataObjectFactory->create([
'data' => [
'qty' => $quantity,
'options' => $customOptions,
],
]);
}

/**
* Convert custom options vakue
*
* @param string $value
* @return string|array
*/
private function convertCustomOptionValue(string $value)
{
$value = trim($value);
if (substr($value, 0, 1) === "[" &&
substr($value, strlen($value) - 1, 1) === "]") {
return explode(',', substr($value, 1, -1));
}
return $value;
}
}
75 changes: 75 additions & 0 deletions app/code/Magento/QuoteGraphQl/Model/Cart/CreateBuyRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?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\DataObject;
use Magento\Framework\DataObjectFactory;

/**
* Creates buy request that can be used for working with cart items
*/
class CreateBuyRequest
{
/**
* @var DataObjectFactory
*/
private $dataObjectFactory;

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

/**
* Returns buy request for working with cart items
*
* @param float $qty
* @param array $customizableOptionsData
* @return DataObject
*/
public function execute(float $qty, array $customizableOptionsData): DataObject
{
$customizableOptions = [];
foreach ($customizableOptionsData as $customizableOption) {
if (isset($customizableOption['value_string'])) {
$customizableOptions[$customizableOption['id']] = $this->convertCustomOptionValue(
$customizableOption['value_string']
);
}
}

return $this->dataObjectFactory->create(
[
'data' => [
'qty' => $qty,
'options' => $customizableOptions,
],
]
);
}

/**
* Convert custom options value
*
* @param string $value
* @return string|array
*/
private function convertCustomOptionValue(string $value)
{
$value = trim($value);
if (substr($value, 0, 1) === "[" &&
substr($value, strlen($value) - 1, 1) === "]") {
return explode(',', substr($value, 1, -1));
}
return $value;
}
}
Loading

0 comments on commit 9fc254f

Please sign in to comment.