Skip to content

Commit c8eb115

Browse files
author
David Verholen
committed
GraphQL-43: introduce wishlist graphql module
1 parent fcf9eed commit c8eb115

12 files changed

+365
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
declare(strict_types=1);
3+
/**
4+
* WishlistItemsProductsResolver
5+
*
6+
* @copyright Copyright © 2018 brandung GmbH & Co. KG. All rights reserved.
7+
* @author david.verholen@brandung.de
8+
*/
9+
10+
namespace Magento\WishlistGraphQl\Model\Resolver;
11+
12+
use Magento\Framework\GraphQl\Config\Element\Field;
13+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
14+
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
15+
use Magento\Framework\GraphQl\Query\Resolver\Value;
16+
use Magento\Framework\GraphQl\Query\ResolverInterface;
17+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
18+
use Magento\WishlistGraphQl\Model\WishlistItemsProductDataProvider;
19+
20+
class WishlistItemsProductsResolver implements ResolverInterface
21+
{
22+
/**
23+
* @var WishlistItemsProductDataProvider
24+
*/
25+
private $productDataProvider;
26+
27+
public function __construct(WishlistItemsProductDataProvider $productDataProvider)
28+
{
29+
$this->productDataProvider = $productDataProvider;
30+
}
31+
32+
33+
/**
34+
* Fetches the data from persistence models and format it according to the GraphQL schema.
35+
*
36+
* @param \Magento\Framework\GraphQl\Config\Element\Field $field
37+
* @param ContextInterface $context
38+
* @param ResolveInfo $info
39+
* @param array|null $value
40+
* @param array|null $args
41+
* @throws \Exception
42+
* @return mixed|Value
43+
*/
44+
public function resolve(
45+
Field $field,
46+
$context,
47+
ResolveInfo $info,
48+
array $value = null,
49+
array $args = null
50+
) {
51+
if (!isset($value['product_id'])) {
52+
throw new GraphQlInputException(
53+
__('Missing key %1 in wishlist item data', ['product_id'])
54+
);
55+
}
56+
return $this->productDataProvider->getProductDataById($value['product_id']);
57+
}
58+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
declare(strict_types=1);
3+
/**
4+
* WishlistItemTypeResolver
5+
*
6+
* @copyright Copyright © 2018 brandung GmbH & Co. KG. All rights reserved.
7+
* @author david.verholen@brandung.de
8+
*/
9+
10+
namespace Magento\WishlistGraphQl\Model\Resolver;
11+
12+
use Magento\Framework\GraphQl\Config\Element\Field;
13+
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
14+
use Magento\Framework\GraphQl\Query\Resolver\Value;
15+
use Magento\Framework\GraphQl\Query\ResolverInterface;
16+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
17+
use Magento\Wishlist\Model\Item;
18+
use Magento\WishlistGraphQl\Model\WishlistItemsDataProvider;
19+
20+
class WishlistItemsResolver implements ResolverInterface
21+
{
22+
/**
23+
* @var WishlistItemsDataProvider
24+
*/
25+
private $wishlistItemsDataProvider;
26+
27+
public function __construct(WishlistItemsDataProvider $wishlistItemsDataProvider)
28+
{
29+
$this->wishlistItemsDataProvider = $wishlistItemsDataProvider;
30+
}
31+
32+
/**
33+
* Fetches the data from persistence models and format it according to the GraphQL schema.
34+
*
35+
* @param \Magento\Framework\GraphQl\Config\Element\Field $field
36+
* @param ContextInterface $context
37+
* @param ResolveInfo $info
38+
* @param array|null $value
39+
* @param array|null $args
40+
* @throws \Exception
41+
* @return mixed|Value
42+
*/
43+
public function resolve(
44+
Field $field,
45+
$context,
46+
ResolveInfo $info,
47+
array $value = null,
48+
array $args = null
49+
) {
50+
return array_map(function (Item $wishlistItem) {
51+
return [
52+
'id' => $wishlistItem->getId(),
53+
'qty' => $wishlistItem->getData('qty'),
54+
'description' => (string)$wishlistItem->getDescription(),
55+
'added_at' => $wishlistItem->getAddedAt(),
56+
'product_id' => (int)$wishlistItem->getProductId()
57+
];
58+
}, $this->wishlistItemsDataProvider->getWishlistItemsForCustomer($context->getUserId()));
59+
}
60+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
declare(strict_types=1);
3+
/**
4+
* WishlistItemTypeResolver
5+
*
6+
* @copyright Copyright © 2018 brandung GmbH & Co. KG. All rights reserved.
7+
* @author david.verholen@brandung.de
8+
*/
9+
10+
namespace Magento\WishlistGraphQl\Model\Resolver;
11+
12+
use Magento\Framework\GraphQl\Config\Element\Field;
13+
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
14+
use Magento\Framework\GraphQl\Query\Resolver\Value;
15+
use Magento\Framework\GraphQl\Query\ResolverInterface;
16+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
17+
use Magento\WishlistGraphQl\Model\WishlistDataProvider;
18+
19+
class WishlistResolver implements ResolverInterface
20+
{
21+
/**
22+
* @var WishlistDataProvider
23+
*/
24+
private $wishlistDataProvider;
25+
26+
public function __construct(WishlistDataProvider $wishlistDataProvider)
27+
{
28+
$this->wishlistDataProvider = $wishlistDataProvider;
29+
}
30+
31+
/**
32+
* Fetches the data from persistence models and format it according to the GraphQL schema.
33+
*
34+
* @param \Magento\Framework\GraphQl\Config\Element\Field $field
35+
* @param ContextInterface $context
36+
* @param ResolveInfo $info
37+
* @param array|null $value
38+
* @param array|null $args
39+
* @throws \Exception
40+
* @return mixed|Value
41+
*/
42+
public function resolve(
43+
Field $field,
44+
$context,
45+
ResolveInfo $info,
46+
array $value = null,
47+
array $args = null
48+
) {
49+
$wishlist = $this->wishlistDataProvider->getWishlistForCustomer($context->getUserId());
50+
return [
51+
'sharing_code' => $wishlist->getSharingCode(),
52+
'updated_at' => $wishlist->getUpdatedAt()
53+
];
54+
}
55+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
declare(strict_types=1);
3+
/**
4+
* WishlistItemTypeResolver
5+
*
6+
* @copyright Copyright © 2018 brandung GmbH & Co. KG. All rights reserved.
7+
* @author david.verholen@brandung.de
8+
*/
9+
10+
namespace Magento\WishlistGraphQl\Model;
11+
12+
use Magento\Wishlist\Model\ResourceModel\Wishlist;
13+
use Magento\Wishlist\Model\WishlistFactory;
14+
15+
class WishlistDataProvider
16+
{
17+
/**
18+
* @var Wishlist
19+
*/
20+
private $wishlistResource;
21+
/**
22+
* @var WishlistFactory
23+
*/
24+
private $wishlistFactory;
25+
26+
public function __construct(Wishlist $wishlistResource, WishlistFactory $wishlistFactory)
27+
{
28+
$this->wishlistResource = $wishlistResource;
29+
$this->wishlistFactory = $wishlistFactory;
30+
}
31+
32+
public function getWishlistForCustomer(int $customerId): \Magento\Wishlist\Model\Wishlist
33+
{
34+
/** @var \Magento\Wishlist\Model\Wishlist $wishlist */
35+
$wishlist = $this->wishlistFactory->create();
36+
$this->wishlistResource->load($wishlist, $customerId, 'customer_id');
37+
return $wishlist;
38+
}
39+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
declare(strict_types=1);
3+
/**
4+
* WishlistItemTypeResolver
5+
*
6+
* @copyright Copyright © 2018 brandung GmbH & Co. KG. All rights reserved.
7+
* @author david.verholen@brandung.de
8+
*/
9+
10+
namespace Magento\WishlistGraphQl\Model;
11+
12+
use Magento\Store\Api\Data\StoreInterface;
13+
use Magento\Store\Model\StoreManagerInterface;
14+
use Magento\Wishlist\Model\Item;
15+
use Magento\Wishlist\Model\ResourceModel\Item\CollectionFactory as WishlistItemCollectionFactory;
16+
17+
class WishlistItemsDataProvider
18+
{
19+
20+
/**
21+
* @var WishlistItemCollectionFactory
22+
*/
23+
private $wishlistItemCollectionFactory;
24+
/**
25+
* @var StoreManagerInterface
26+
*/
27+
private $storeManager;
28+
29+
public function __construct(
30+
WishlistItemCollectionFactory $wishlistItemCollectionFactory,
31+
StoreManagerInterface $storeManager
32+
) {
33+
$this->wishlistItemCollectionFactory = $wishlistItemCollectionFactory;
34+
$this->storeManager = $storeManager;
35+
}
36+
37+
/**
38+
* @param int $customerId
39+
* @return Item[]
40+
*/
41+
public function getWishlistItemsForCustomer(int $customerId): array
42+
{
43+
$wishlistItemCollection = $this->wishlistItemCollectionFactory->create();
44+
$wishlistItemCollection->addCustomerIdFilter($customerId);
45+
$wishlistItemCollection->addStoreFilter(array_map(function (StoreInterface $store) {
46+
return $store->getId();
47+
}, $this->storeManager->getStores()));
48+
$wishlistItemCollection->setVisibilityFilter();
49+
$wishlistItemCollection->load();
50+
return $wishlistItemCollection->getItems();
51+
}
52+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
declare(strict_types=1);
3+
/**
4+
* WishlistItemsProductsResolver
5+
*
6+
* @copyright Copyright © 2018 brandung GmbH & Co. KG. All rights reserved.
7+
* @author david.verholen@brandung.de
8+
*/
9+
10+
namespace Magento\WishlistGraphQl\Model;
11+
12+
use Magento\Catalog\Api\ProductRepositoryInterface;
13+
14+
class WishlistItemsProductDataProvider
15+
{
16+
/**
17+
* @var ProductRepositoryInterface
18+
*/
19+
private $productRepository;
20+
21+
public function __construct(ProductRepositoryInterface $productRepository)
22+
{
23+
$this->productRepository = $productRepository;
24+
}
25+
26+
public function getProductDataById(int $productId) {
27+
$product = $this->productRepository->getById($productId);
28+
$productData = $product->toArray();
29+
$productData['model'] = $product;
30+
return $productData;
31+
}
32+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# WishlistGraphQl
2+
3+
**WishlistGraphQl** provides type information for the GraphQl module
4+
to generate wishlist fields.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "magento/module-wishlist-graph-ql",
3+
"description": "N/A",
4+
"type": "magento2-module",
5+
"require": {
6+
"php": "~7.1.3||~7.2.0",
7+
"magento/framework": "*",
8+
"magento/module-catalog": "*"
9+
},
10+
"suggest": {
11+
"magento/module-wishlist": "*"
12+
},
13+
"license": [
14+
"OSL-3.0",
15+
"AFL-3.0"
16+
],
17+
"autoload": {
18+
"files": [
19+
"registration.php"
20+
],
21+
"psr-4": {
22+
"Magento\\WishlistGraphQl\\": ""
23+
}
24+
}
25+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
9+
<module name="Magento_WishlistGraphQl" />
10+
</config>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright © Magento, Inc. All rights reserved.
2+
# See COPYING.txt for license details.
3+
4+
type Query {
5+
wishlist: WishlistOutput @resolver(class: "\\Magento\\WishlistGraphQl\\Model\\Resolver\\WishlistResolver") @doc(description: "todo")
6+
}
7+
8+
type WishlistOutput {
9+
items: [WishlistItem] @resolver(class: "\\Magento\\WishlistGraphQl\\Model\\Resolver\\WishlistItemsResolver") @doc(description: "todo"),
10+
sharing_code: String! @doc(description: "todo"),
11+
updated_at: String! @doc(description: "todo")
12+
}
13+
14+
type WishlistItem {
15+
id: Int! @doc(description: "todo")
16+
qty: Float! @doc(description: "todo"),
17+
description: String! @doc(description: "todo"),
18+
added_at: String! @doc(description: "todo"),
19+
product: ProductInterface @resolver(class: "\\Magento\\WishlistGraphQl\\Model\\Resolver\\WishlistItemsProductsResolver")
20+
}

0 commit comments

Comments
 (0)