Skip to content

Commit b617d6f

Browse files
committed
update graphql
1 parent 5a9b99d commit b617d6f

File tree

4 files changed

+193
-1
lines changed

4 files changed

+193
-1
lines changed

Model/Resolver/Sellers.php

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
use Magento\Framework\GraphQl\Query\Resolver\Argument\SearchCriteria\Builder as SearchCriteriaBuilder;
3030
use Magento\Framework\GraphQl\Query\ResolverInterface;
3131
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
32+
use Magento\Framework\Exception\InputException;
33+
use Magento\Search\Model\Query;
34+
use Magento\Store\Api\Data\StoreInterface;
35+
use Magento\Store\Model\ScopeInterface;
36+
use Magento\Framework\GraphQl\Query\Resolver\Argument\SearchCriteria\ArgumentApplier\Filter;
3237

3338
/**
3439
* Class Sellers
@@ -43,22 +48,35 @@ class Sellers extends AbstractSellerQuery implements ResolverInterface
4348
*/
4449
private $sellers;
4550

51+
/**
52+
* @var string
53+
*/
54+
private const SPECIAL_CHARACTERS = '-+~/\\<>\'":*$#@()!,.?`=%&^';
55+
56+
/**
57+
* @var ScopeConfigInterface
58+
*/
59+
private $scopeConfig;
60+
4661
/**
4762
* Sellers constructor.
4863
* @param SearchCriteriaBuilder $searchCriteriaBuilder
4964
* @param SellersFrontendRepositoryInterface $seller
5065
* @param SellerProductsRepositoryInterface $productSeller
5166
* @param ProductRepositoryInterface $productRepository
5267
* @param Products\Query\SellerQueryInterface $sellers
68+
* @param ScopeConfigInterface $scopeConfig
5369
*/
5470
public function __construct(
5571
SearchCriteriaBuilder $searchCriteriaBuilder,
5672
SellersFrontendRepositoryInterface $seller,
5773
SellerProductsRepositoryInterface $productSeller,
5874
ProductRepositoryInterface $productRepository,
59-
Products\Query\SellerQueryInterface $sellers
75+
Products\Query\SellerQueryInterface $sellers,
76+
ScopeConfigInterface $scopeConfig
6077
) {
6178
$this->sellers = $sellers;
79+
$this->scopeConfig = $scopeConfig;
6280
parent::__construct($searchCriteriaBuilder, $seller, $productSeller, $productRepository);
6381
}
6482

@@ -78,6 +96,8 @@ public function resolve(
7896
if ($args['pageSize'] < 1) {
7997
throw new GraphQlInputException(__('pageSize value must be greater than 0.'));
8098
}
99+
$store = $context->getExtensionAttributes()->getStore();
100+
$args[Filter::ARGUMENT_NAME] = $this->formatMatchFilters($args['filters'], $store);
81101
$searchCriteria = $this->searchCriteriaBuilder->build('lof_marketplace_seller', $args);
82102
$searchCriteria->setCurrentPage($args['currentPage']);
83103
$searchCriteria->setPageSize($args['pageSize']);
@@ -89,4 +109,42 @@ public function resolve(
89109
'items' => $searchResult->getItems(),
90110
];
91111
}
112+
113+
/**
114+
* Format match filters to behave like fuzzy match
115+
*
116+
* @param array $filters
117+
* @param StoreInterface $store
118+
* @return array
119+
* @throws InputException
120+
*/
121+
private function formatMatchFilters(array $filters, StoreInterface $store): array
122+
{
123+
$minQueryLength = $this->scopeConfig->getValue(
124+
Query::XML_PATH_MIN_QUERY_LENGTH,
125+
ScopeInterface::SCOPE_STORE,
126+
$store
127+
);
128+
$availableMatchFilters = ["store_id"];
129+
foreach ($filters as $filter => $condition) {
130+
$conditionType = current(array_keys($condition));
131+
$tmpminQueryLength = $minQueryLength;
132+
if(in_array($filter, $availableMatchFilters)){
133+
$tmpminQueryLength = 1;
134+
}
135+
if ($conditionType === 'match') {
136+
$searchValue = trim(str_replace(self::SPECIAL_CHARACTERS, '', $condition[$conditionType]));
137+
$matchLength = strlen($searchValue);
138+
if ($matchLength < $tmpminQueryLength) {
139+
throw new InputException(__('Invalid match filter. Minimum length is %1.', $tmpminQueryLength));
140+
}
141+
unset($filters[$filter]['match']);
142+
if($filter == "store_id"){
143+
$searchValue = (int)$searchValue;
144+
}
145+
$filters[$filter]['like'] = '%' . $searchValue . '%';
146+
}
147+
}
148+
return $filters;
149+
}
92150
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/**
3+
* Landofcoder
4+
*
5+
* NOTICE OF LICENSE
6+
*
7+
* This source file is subject to the Landofcoder.com license that is
8+
* available through the world-wide-web at this URL:
9+
* https://landofcoder.com/terms
10+
*
11+
* DISCLAIMER
12+
*
13+
* Do not edit or add to this file if you wish to upgrade this extension to newer
14+
* version in the future.
15+
*
16+
* @category Landofcoder
17+
* @package Lof_MarketplaceGraphQl
18+
* @copyright Copyright (c) 2021 Landofcoder (https://www.landofcoder.com/)
19+
* @license https://landofcoder.com/terms
20+
*/
21+
22+
namespace Lof\MarketplaceGraphQl\Model\Resolver\Sellers;
23+
24+
25+
use Magento\Framework\GraphQl\Query\Resolver\IdentityInterface;
26+
27+
class Identity implements IdentityInterface
28+
{
29+
30+
private $cacheTag = \Magento\Framework\App\Config::CACHE_TAG;
31+
32+
/**
33+
* @param array $resolvedData
34+
* @return string[]
35+
*/
36+
public function getIdentities(array $resolvedData)
37+
{
38+
$ids = empty($resolvedData['seller_id']) ?
39+
[] : [$this->cacheTag, sprintf('%s_%s', $this->cacheTag, $resolvedData['seller_id'])];
40+
41+
return $ids;
42+
}
43+
}
44+
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
/**
3+
* Landofcoder
4+
*
5+
* NOTICE OF LICENSE
6+
*
7+
* This source file is subject to the Landofcoder.com license that is
8+
* available through the world-wide-web at this URL:
9+
* https://landofcoder.com/terms
10+
*
11+
* DISCLAIMER
12+
*
13+
* Do not edit or add to this file if you wish to upgrade this extension to newer
14+
* version in the future.
15+
*
16+
* @category Landofcoder
17+
* @package Lof_MarketplaceGraphQl
18+
* @copyright Copyright (c) 2021 Landofcoder (https://www.landofcoder.com/)
19+
* @license https://landofcoder.com/terms
20+
*/
21+
22+
namespace Lof\MarketplaceGraphQl\Model\Resolver\Sellers;
23+
24+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
25+
use Magento\Framework\GraphQl\Config\Element\Field;
26+
use Magento\Framework\GraphQl\Query\ResolverInterface;
27+
28+
/**
29+
* Retrieves the sort fields data
30+
*/
31+
class SortFields implements ResolverInterface
32+
{
33+
/**
34+
* @inheritdoc
35+
*/
36+
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
37+
{
38+
$sortFieldsOptions = [
39+
['label' => "position", 'value' => "position"],
40+
['label' => "creation_time", 'value' => "creation_time"],
41+
['label' => "update_time", 'value' => "update_time"],
42+
['label' => "total_sold", 'value' => "total_sold"],
43+
['label' => "product_count", 'value' => "product_count"],
44+
['label' => "name", 'value' => "name"],
45+
['label' => "shop_title", 'value' => "shop_title"],
46+
['label' => "sale", 'value' => "sale"],
47+
['label' => "country_id", 'value' => "country_id"],
48+
['label' => "region", 'value' => "region"],
49+
['label' => "status", 'value' => "status"]
50+
];
51+
52+
$data = [
53+
'default' => "position",
54+
'options' => $sortFieldsOptions,
55+
];
56+
57+
return $data;
58+
}
59+
}

etc/schema.graphqls

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ type Query {
5050
filter: SellerFilterInput @doc(description: "Identifies which question attributes to search for and return."),
5151
pageSize: Int = 5 @doc(description: "Specifies the maximum number of results to return at once. This attribute is optional."),
5252
currentPage: Int = 1 @doc(description: "Specifies which page of results to return. The default value is 1."),
53+
sort: SellerSortInput @doc(description: "Specifies which attributes to sort on, and whether to return the results in ascending or descending order.")
5354
):Sellers @resolver(class: "\\Lof\\MarketplaceGraphQl\\Model\\Resolver\\Sellers") @doc(description: "The seller query searches for sellers that match the criteria specified in the search and filter attributes")
5455
}
5556

@@ -123,6 +124,13 @@ type Seller {
123124
banner_pic: String @doc(description: "Banner Pic")
124125
shop_url: String @doc(description: "Shop Url")
125126
logo_pic: String @doc(description: "Logo Pic")
127+
verify_status: Int @doc(description: "verify_status")
128+
product_count: Int @doc(description: "product_count")
129+
telephone: String @doc(description: "telephone")
130+
creation_time: String @doc(description: "creation_time")
131+
update_time: String @doc(description: "update_time")
132+
country_id: String @doc(description: "country_id")
133+
total_sold: String @doc(description: "total_sold")
126134
store_id: [Int] @doc(description: "Store Id")
127135
seller_rates: SellerRates @doc(description: "Seller Rates")
128136
products: Products @doc(description: "Products")
@@ -142,11 +150,34 @@ type SellerRates {
142150
items: [SellerRate] @doc(description: "An array of seller rates")
143151
}
144152

153+
input SellerSortInput @doc(description: "SellerSortInput specifies the attribute to use for sorting search results and indicates whether the results are sorted in ascending or descending order. It's possible to sort sellers using searchable attributes with enabled 'Use in Filter Options' option")
154+
{
155+
position: SortEnum @doc(description: "Sort by the position (position).")
156+
creation_time: SortEnum @doc(description: "Sort by the creation_time assigned to each seller.")
157+
update_time: SortEnum @doc(description: "Sort by the update_time assigned to each seller.")
158+
total_sold: SortEnum @doc(description: "Sort by the total_sold assigned to each seller.")
159+
product_count: SortEnum @doc(description: "Sort by the product_count assigned to each seller.")
160+
name: SortEnum @doc(description: "Sort by the name assigned to each seller.")
161+
shop_title: SortEnum @doc(description: "Sort by the shop_title assigned to each seller.")
162+
sale: SortEnum @doc(description: "Sort by the sale assigned to each seller.")
163+
country_id: SortEnum @doc(description: "Sort by the country_id assigned to each seller.")
164+
region: SortEnum @doc(description: "Sort by the region assigned to each seller.")
165+
status: SortEnum @doc(description: "Sort by the status assigned to each seller.")
166+
}
167+
145168
input SellerFilterInput {
146169
seller_id: FilterTypeInput @doc(description: "Seller ID")
147170
name: FilterTypeInput @doc(description: "Name")
148171
status: FilterTypeInput @doc(description: "Status")
149172
email: FilterTypeInput @doc(description: "Email")
173+
group_id: FilterTypeInput @doc(description: "group_id")
174+
page_title: FilterTypeInput @doc(description: "page_title")
175+
meta_keywords: FilterTypeInput @doc(description: "meta_keywords")
176+
created_at: FilterMatchTypeInput @doc(description: "created_at")
177+
address: FilterMatchTypeInput @doc(description: "address")
178+
country: FilterTypeInput @doc(description: "country")
179+
city: FilterTypeInput @doc(description: "city")
180+
region: FilterTypeInput @doc(description: "region")
150181
or: SellerFilterInput @doc(description: "The keyword required to perform a logical OR comparison")
151182
}
152183

0 commit comments

Comments
 (0)