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

GraphQl-200: Product Compare #338

Merged
merged 2 commits into from
Dec 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\ProductCompareGraphQl\Model\Resolver;

use Magento\Catalog\Model\Product\Compare\AddToList;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;

/**
* Resolver for Add Product to Compare List mutation.
*/
class AddProductToCompareList implements ResolverInterface
{
/**
* @var AddToList
*/
private $addToList;

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

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

if (!isset($args['hashed_id']) || !is_string($args['hashed_id'])) {
throw new GraphQlInputException(__('"hashed_id" value should be specified'));
}

$context->setData('hashed_id', $args['hashed_id']);
$result = ['result' => false, 'compareProducts' => []];

if (!empty($args['input']['ids']) && is_array($args['input']['ids'])) {
$customerId = $context->getUserId();

foreach ($args['input']['ids'] as $id) {
$this->addToList->execute($customerId, $args['hashed_id'], (int)$id);
}

$result = ['result' => true, 'compareProductsList' => []];
}

return $result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\ProductCompareGraphQl\Model\Resolver;

use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;

/**
* CompareProducts field('s) resolver.
*/
class CompareProductsList implements ResolverInterface
{
/**
* @inheritdoc
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
if (!isset($args['hashed_id']) || !is_string($args['hashed_id'])) {
throw new GraphQlInputException(__('"hashed_id" value should be specified'));
}

$context->setData('hashed_id', $args['hashed_id']);

return [];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\ProductCompareGraphQl\Model\Resolver;

use Magento\Catalog\Model\Product\Compare\CreateList;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;

/**
* Create new compare list.
*/
class CreateCompareList implements ResolverInterface
{
/**
* @var CreateList
*/
private $createList;

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

/**
* @inheritdoc
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
$customerId = $context->getUserId();

$compareList = $this->createList->execute($customerId);

return ['hashed_id' => $compareList->getHashedId()];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\ProductCompareGraphQl\Model\Resolver;

use Magento\Catalog\Model\Product\Compare\ItemsFromListProvider;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;

/**
* CompareProducts field resolver.
*/
class GetCompareListItems implements ResolverInterface
{
/**
* @var ItemsFromListProvider
*/
private $itemsFromListProvider;

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

/**
* @inheritdoc
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
$items = $this->itemsFromListProvider->get($context->getUserId(), $context->getData('hashed_id'));

return $items;
}
}
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\ProductCompareGraphQl\Model\Resolver;

use Magento\Catalog\Model\Product\Compare\RemoveFromList;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;

/**
* Resolver for Remove Product(products) from Compare List.
*/
class RemoveProductFromCompareList implements ResolverInterface
{
/**
* @var RemoveFromList
*/
private $removeFromList;

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

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

if (!isset($args['hashed_id']) || !is_string($args['hashed_id'])) {
throw new GraphQlInputException(__('"hashed_id" value should be specified'));
}

$context->setData('hashed_id', $args['hashed_id']);
$result = ['result' => false, "compareProducts" => []];

if (!empty($args['input']['ids']) && is_array($args['input']['ids'])) {
$customerId = $context->getUserId();

foreach ($args['input']['ids'] as $id) {
$this->removeFromList->execute($customerId, $args['hashed_id'], (int)$id);
}

$result = ['result' => true, "compareProductsList" => []];
}

return $result;
}
}
4 changes: 4 additions & 0 deletions app/code/Magento/ProductCompareGraphQl/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# ProductCompareGraphQl

**ProductCompareGraphQl** provides type and resolver information for the GraphQl module
to generate Product Compare information endpoints. Also provides endpoints for modifying a product comparison.
22 changes: 22 additions & 0 deletions app/code/Magento/ProductCompareGraphQl/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "magento/module-product-compare-graph-ql",
"description": "N/A",
"type": "magento2-module",
"require": {
"php": "~7.1.3||~7.2.0",
"magento/module-catalog": "*",
"magento/framework": "*"
},
"license": [
"OSL-3.0",
"AFL-3.0"
],
"autoload": {
"files": [
"registration.php"
],
"psr-4": {
"Magento\\ProductCompareGraphQl\\": ""
}
}
}
14 changes: 14 additions & 0 deletions app/code/Magento/ProductCompareGraphQl/etc/module.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?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_ProductCompareGraphQl">
<sequence>
<module name="Magento_Catalog"/>
</sequence>
</module>
</config>
43 changes: 43 additions & 0 deletions app/code/Magento/ProductCompareGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Copyright © Magento, Inc. All rights reserved.
# See COPYING.txt for license details.

type Query {
compareProductsList(hashed_id: String): CompareProductsList @resolver(class: "Magento\\ProductCompareGraphQl\\Model\\Resolver\\CompareProductsList") @doc(description: "Get compare products list")
}

type CompareProductsList {
items: [CompareItem] @resolver(class: "Magento\\ProductCompareGraphQl\\Model\\Resolver\\GetCompareListItems") @doc (description: "Compare products list which contains items as products")
}

type CompareItem {
item_id: Int
product: ProductInterface
}

type Mutation {
addProductToCompareList(hashed_id: String, input: addProductToCompareListInput): addProductToCompareListOutput @resolver (class: "Magento\\ProductCompareGraphQl\\Model\\Resolver\\AddProductToCompareList") @doc(description: "Add product to compare list")
removeProductFromCompareList(hashed_id: String, input: removeProductFromCompareListInput): removeProductFromCompareListOutput @resolver(class: "Magento\\ProductCompareGraphQl\\Model\\Resolver\\RemoveProductFromCompareList") @doc(description: "Remove product from compare list")
createCompareList: CreateCompareListOutput @resolver(class: "Magento\\ProductCompareGraphQl\\Model\\Resolver\\CreateCompareList") @doc(description: "Create compare list to add product to this list")
}

input addProductToCompareListInput {
ids: [Int!]
}

type addProductToCompareListOutput {
result: Boolean!
compareProductsList: CompareProductsList
}

input removeProductFromCompareListInput {
ids: [Int!]
}

type removeProductFromCompareListOutput {
result: Boolean!
compareProductsList: CompareProductsList
}

type CreateCompareListOutput {
hashed_id: String
}
9 changes: 9 additions & 0 deletions app/code/Magento/ProductCompareGraphQl/registration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

use \Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Magento_ProductCompareGraphQl', __DIR__);
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@
"magento/module-paypal-graph-ql": "*",
"magento/module-persistent": "*",
"magento/module-product-alert": "*",
"magento/module-product-compare-graph-ql": "*",
"magento/module-product-video": "*",
"magento/module-quote": "*",
"magento/module-quote-analytics": "*",
Expand Down
Loading