-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3470 from magento-engcom/graphql-develop-prs
[EngCom] Public Pull Requests - GraphQL
- Loading branch information
Showing
13 changed files
with
495 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
app/code/Magento/CatalogGraphQl/Model/Resolver/Category/ProductsCount.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?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\Model\Category; | ||
use Magento\Catalog\Model\Product\Visibility; | ||
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product\CollectionProcessor\StockProcessor; | ||
use Magento\Framework\Api\SearchCriteriaInterface; | ||
use Magento\Framework\GraphQl\Exception\GraphQlInputException; | ||
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; | ||
use Magento\Framework\GraphQl\Config\Element\Field; | ||
use Magento\Framework\GraphQl\Query\ResolverInterface; | ||
|
||
/** | ||
* Retrieves products count for a category | ||
*/ | ||
class ProductsCount implements ResolverInterface | ||
{ | ||
/** | ||
* @var Visibility | ||
*/ | ||
private $catalogProductVisibility; | ||
|
||
/** | ||
* @var StockProcessor | ||
*/ | ||
private $stockProcessor; | ||
|
||
/** | ||
* @var SearchCriteriaInterface | ||
*/ | ||
private $searchCriteria; | ||
|
||
/** | ||
* @param Visibility $catalogProductVisibility | ||
* @param SearchCriteriaInterface $searchCriteria | ||
* @param StockProcessor $stockProcessor | ||
*/ | ||
public function __construct( | ||
Visibility $catalogProductVisibility, | ||
SearchCriteriaInterface $searchCriteria, | ||
StockProcessor $stockProcessor | ||
) { | ||
$this->catalogProductVisibility = $catalogProductVisibility; | ||
$this->searchCriteria = $searchCriteria; | ||
$this->stockProcessor = $stockProcessor; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) | ||
{ | ||
if (!isset($value['model'])) { | ||
throw new GraphQlInputException(__('"model" value should be specified')); | ||
} | ||
/** @var Category $category */ | ||
$category = $value['model']; | ||
$productsCollection = $category->getProductCollection(); | ||
$productsCollection->setVisibility($this->catalogProductVisibility->getVisibleInSiteIds()); | ||
$productsCollection = $this->stockProcessor->process($productsCollection, $this->searchCriteria, []); | ||
|
||
return $productsCollection->getSize(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
198 changes: 198 additions & 0 deletions
198
app/code/Magento/SendFriendGraphQl/Model/Resolver/SendEmailToFriend.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\SendFriendGraphQl\Model\Resolver; | ||
|
||
use Magento\Catalog\Api\Data\ProductInterface; | ||
use Magento\Catalog\Api\ProductRepositoryInterface; | ||
use Magento\Framework\DataObjectFactory; | ||
use Magento\Framework\Event\ManagerInterface; | ||
use Magento\Framework\Exception\NoSuchEntityException; | ||
use Magento\Framework\GraphQl\Config\Element\Field; | ||
use Magento\Framework\GraphQl\Exception\GraphQlInputException; | ||
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException; | ||
use Magento\Framework\GraphQl\Query\ResolverInterface; | ||
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; | ||
use Magento\SendFriend\Model\SendFriend; | ||
use Magento\SendFriend\Model\SendFriendFactory; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
class SendEmailToFriend implements ResolverInterface | ||
{ | ||
/** | ||
* @var SendFriendFactory | ||
*/ | ||
private $sendFriendFactory; | ||
|
||
/** | ||
* @var ProductRepositoryInterface | ||
*/ | ||
private $productRepository; | ||
|
||
/** | ||
* @var DataObjectFactory | ||
*/ | ||
private $dataObjectFactory; | ||
|
||
/** | ||
* @var ManagerInterface | ||
*/ | ||
private $eventManager; | ||
|
||
/** | ||
* @param SendFriendFactory $sendFriendFactory | ||
* @param ProductRepositoryInterface $productRepository | ||
* @param DataObjectFactory $dataObjectFactory | ||
* @param ManagerInterface $eventManager | ||
*/ | ||
public function __construct( | ||
SendFriendFactory $sendFriendFactory, | ||
ProductRepositoryInterface $productRepository, | ||
DataObjectFactory $dataObjectFactory, | ||
ManagerInterface $eventManager | ||
) { | ||
$this->sendFriendFactory = $sendFriendFactory; | ||
$this->productRepository = $productRepository; | ||
$this->dataObjectFactory = $dataObjectFactory; | ||
$this->eventManager = $eventManager; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) | ||
{ | ||
/** @var SendFriend $sendFriend */ | ||
$sendFriend = $this->sendFriendFactory->create(); | ||
|
||
if ($sendFriend->getMaxSendsToFriend() && $sendFriend->isExceedLimit()) { | ||
throw new GraphQlInputException( | ||
__('You can\'t send messages more than %1 times an hour.', $sendFriend->getMaxSendsToFriend()) | ||
); | ||
} | ||
|
||
$product = $this->getProduct($args['input']['product_id']); | ||
$this->eventManager->dispatch('sendfriend_product', ['product' => $product]); | ||
$sendFriend->setProduct($product); | ||
|
||
$senderData = $this->extractSenderData($args); | ||
$sendFriend->setSender($senderData); | ||
|
||
$recipientsData = $this->extractRecipientsData($args); | ||
$sendFriend->setRecipients($recipientsData); | ||
|
||
$this->validateSendFriendModel($sendFriend, $senderData, $recipientsData); | ||
$sendFriend->send(); | ||
|
||
return array_merge($senderData, $recipientsData); | ||
} | ||
|
||
/** | ||
* Validate send friend model | ||
* | ||
* @param SendFriend $sendFriend | ||
* @param array $senderData | ||
* @param array $recipientsData | ||
* @return void | ||
* @throws GraphQlInputException | ||
*/ | ||
private function validateSendFriendModel(SendFriend $sendFriend, array $senderData, array $recipientsData): void | ||
{ | ||
$sender = $this->dataObjectFactory->create()->setData($senderData['sender']); | ||
$sendFriend->setData('_sender', $sender); | ||
|
||
$emails = array_column($recipientsData['recipients'], 'email'); | ||
$recipients = $this->dataObjectFactory->create()->setData('emails', $emails); | ||
$sendFriend->setData('_recipients', $recipients); | ||
|
||
$validationResult = $sendFriend->validate(); | ||
if ($validationResult !== true) { | ||
throw new GraphQlInputException(__(implode($validationResult))); | ||
} | ||
} | ||
|
||
/** | ||
* Get product | ||
* | ||
* @param int $productId | ||
* @return ProductInterface | ||
* @throws GraphQlNoSuchEntityException | ||
*/ | ||
private function getProduct(int $productId): ProductInterface | ||
{ | ||
try { | ||
$product = $this->productRepository->getById($productId); | ||
if (!$product->isVisibleInCatalog()) { | ||
throw new GraphQlNoSuchEntityException( | ||
__("The product that was requested doesn't exist. Verify the product and try again.") | ||
); | ||
} | ||
} catch (NoSuchEntityException $e) { | ||
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e); | ||
} | ||
return $product; | ||
} | ||
|
||
/** | ||
* Extract recipients data | ||
* | ||
* @param array $args | ||
* @return array | ||
* @throws GraphQlInputException | ||
*/ | ||
private function extractRecipientsData(array $args): array | ||
{ | ||
$recipients = []; | ||
foreach ($args['input']['recipients'] as $recipient) { | ||
if (empty($recipient['name'])) { | ||
throw new GraphQlInputException(__('Please provide Name for all of recipients.')); | ||
} | ||
|
||
if (empty($recipient['email'])) { | ||
throw new GraphQlInputException(__('Please provide Email for all of recipients.')); | ||
} | ||
|
||
$recipients[] = [ | ||
'name' => $recipient['name'], | ||
'email' => $recipient['email'], | ||
]; | ||
} | ||
return ['recipients' => $recipients]; | ||
} | ||
|
||
/** | ||
* Extract sender data | ||
* | ||
* @param array $args | ||
* @return array | ||
* @throws GraphQlInputException | ||
*/ | ||
private function extractSenderData(array $args): array | ||
{ | ||
if (empty($args['input']['sender']['name'])) { | ||
throw new GraphQlInputException(__('Please provide Name of sender.')); | ||
} | ||
|
||
if (empty($args['input']['sender']['email'])) { | ||
throw new GraphQlInputException(__('Please provide Email of sender.')); | ||
} | ||
|
||
if (empty($args['input']['sender']['message'])) { | ||
throw new GraphQlInputException(__('Please provide Message.')); | ||
} | ||
|
||
return [ | ||
'sender' => [ | ||
'name' => $args['input']['sender']['name'], | ||
'email' => $args['input']['sender']['email'], | ||
'message' => $args['input']['sender']['message'], | ||
], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# SendFriendGraphQl | ||
|
||
**SendFriendGraphQl** provides support of GraphQL for SendFriend functionality. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"name": "magento/module-send-friend-graph-ql", | ||
"description": "N/A", | ||
"type": "magento2-module", | ||
"require": { | ||
"php": "~7.1.3||~7.2.0", | ||
"magento/framework": "*", | ||
"magento/module-catalog": "*", | ||
"magento/module-send-friend": "*" | ||
}, | ||
"suggest": { | ||
"magento/module-graph-ql": "*" | ||
}, | ||
"license": [ | ||
"OSL-3.0", | ||
"AFL-3.0" | ||
], | ||
"autoload": { | ||
"files": [ | ||
"registration.php" | ||
], | ||
"psr-4": { | ||
"Magento\\SendFriendGraphQl\\": "" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?xml version="1.0"?> | ||
<!-- | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
--> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> | ||
<module name="Magento_SendFriendGraphQl"/> | ||
</config> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Copyright © Magento, Inc. All rights reserved. | ||
# See COPYING.txt for license details. | ||
|
||
type Mutation { | ||
sendEmailToFriend (input: SendEmailToFriendSenderInput): SendEmailToFriendOutput @resolver(class: "\\Magento\\SendFriendGraphQl\\Model\\Resolver\\SendEmailToFriend") @doc(description:"Recommends Product by Sending Single/Multiple Email") | ||
} | ||
|
||
input SendEmailToFriendSenderInput { | ||
product_id: Int! | ||
sender: Sender! | ||
recipients: [Recipient!]! | ||
} | ||
|
||
type Sender { | ||
name: String! | ||
email: String! | ||
message: String! | ||
} | ||
|
||
type Recipient { | ||
name: String! | ||
email: String! | ||
} | ||
|
||
type SendEmailToFriendOutput { | ||
sender: Sender | ||
recipients: [Recipient] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
use Magento\Framework\Component\ComponentRegistrar; | ||
|
||
ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Magento_SendFriendGraphQl', __DIR__); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.