Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

5343 Updated wishlist resolver and schema to support pagination #32

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
47 changes: 36 additions & 11 deletions src/Model/Resolver/WishlistItemsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ public function resolve(

/** @var Wishlist $wishlist */
$wishlist = $value['model'];
$wishlistItems = $this->getWishListItems($wishlist);
$wishlistCollection = $this->getWishlistCollection($wishlist, $args);
$wishlistItems = $wishlistCollection->getItems();
$itemProductIds = [];

foreach ($wishlistItems as $item) {
Expand Down Expand Up @@ -225,13 +226,20 @@ public function resolve(
'buy_request' => $buyRequestOption->getValue() ?? '',
'description' => $wishlistItem->getDescription(),
'added_at' => $wishlistItem->getAddedAt(),
'model' => $wishlistItem,
'model' => $product,
'product' => $itemProduct,
'options' => $options
];
}

return $data;
return [
'items' => $data,
'page_info' => [
'current_page' => $wishlistCollection->getCurPage(),
'page_size' => $wishlistCollection->getPageSize(),
'total_pages' => $wishlistCollection->getLastPageNumber()
]
];
}

/**
Expand Down Expand Up @@ -313,30 +321,39 @@ protected function getWishlistProducts(
$collection = $this->collectionFactory->create();
$collection->addIdFilter(array_values($itemProductIds));

$itemsNode = array_filter(iterator_to_array($info->fieldNodes), function ($node) {
return $node->name->value === 'items_v2';
})[0];
Comment on lines +324 to +326

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why like that? Do we really need to get the field out here?
Maybe move it into getFieldsFromProductInfo where semantically it can fit?


$this->collectionProcessor->process(
$collection,
$this->searchCriteriaBuilder->create(),
$this->getFieldsFromProductInfo($info, 'items/product')
$this->getFieldsFromProductInfo($itemsNode, 'items/product')
);

$items = $collection->getItems();

return $this->productPostProcessor->process(
$items,
'items/product',
$info
$itemsNode
);
}

/**
* Get wish-list items
* Get wishlist items collection
*
* @param Wishlist $wishlist
* @return Item[]
* @param array $args
* @return \Magento\Wishlist\Model\ResourceModel\Item\Collection
*/
protected function getWishListItems(
Wishlist $wishlist
): array {
protected function getWishlistCollection(
Wishlist $wishlist,
array $args
): \Magento\Wishlist\Model\ResourceModel\Item\Collection {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Import collection in head of file

$currentPage = $args['currentPage'] ?? 1;
$pageSize = $args['pageSize'] ?? 20;

/** @var WishlistItemCollection $collection */
$collection = $this->wishlistItemsFactory->create();
$collection
Expand All @@ -346,7 +363,15 @@ protected function getWishListItems(
}, $this->storeManager->getStores()))
->setVisibilityFilter();

return $collection->getItems();
if ($currentPage > 0) {
$collection->setCurPage($currentPage);
}

if ($pageSize > 0) {
$collection->setPageSize($pageSize);
}

return $collection;
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@
type="ScandiPWA\WishlistGraphQl\Model\Resolver\WishlistResolver"/>
<preference for="Magento\WishlistGraphQl\Model\Resolver\WishlistItemsResolver"
type="ScandiPWA\WishlistGraphQl\Model\Resolver\WishlistItemsResolver"/>
<preference for="Magento\WishlistGraphQl\Model\Resolver\WishlistItems"
type="ScandiPWA\WishlistGraphQl\Model\Resolver\WishlistItemsResolver"/>
Comment on lines +17 to +18

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How looks the original file? Do we really need to override it fully without extending it?
At all, what for this preference?

</config>
14 changes: 9 additions & 5 deletions src/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type Mutation {
}

type Query {
s_wishlist(sharing_code: ID): WishlistOutput @resolver(class: "\\ScandiPWA\\WishlistGraphQl\\Model\\Resolver\\WishlistResolver")
s_wishlist(sharing_code: ID): Wishlist @resolver(class: "\\ScandiPWA\\WishlistGraphQl\\Model\\Resolver\\WishlistResolver")
}

input WishlistItemInput {
Expand All @@ -36,16 +36,20 @@ input ShareWishlistInput {
message: String @doc(description: "Sharing message")
}

extend type WishlistOutput {
extend type Wishlist {
id: ID @resolver(class: "\\ScandiPWA\\WishlistGraphQl\\Model\\Resolver\\Wishlist\\IdResolver")
creators_name: String @resolver(class: "\\ScandiPWA\\WishlistGraphQl\\Model\\Resolver\\Wishlist\\CreatorResolver") @doc(description: "Name of wishlist creator")
}

extend type WishlistItems {
items: [WishlistItem] @doc(description: "A list of items in the wish list.")
}

extend type WishlistItem {
sku: ID @doc(description: "The wish list item's SKU")
price: Float @doc(description: "Product price based on selected options")
price_without_tax: Float @doc(description: "Product price without tax based on selected options")
buy_request: String @doc(description: "Configurations to place order with selected options")
price: Float @doc(description: "Product price based on selected options")
price_without_tax: Float @doc(description: "Product price without tax based on selected options")
buy_request: String @doc(description: "Configurations to place order with selected options")
options: [WishlistItemOption]
}

Expand Down