Skip to content

Commit 2a09bb5

Browse files
committed
ACP2E-3892: [Mainline] Unpopulated pages are cached due to search engine errors
1 parent 501109f commit 2a09bb5

File tree

1 file changed

+39
-26
lines changed
  • app/code/Magento/CatalogGraphQl/Model/Resolver/Category

1 file changed

+39
-26
lines changed

app/code/Magento/CatalogGraphQl/Model/Resolver/Category/Products.php

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ class Products implements ResolverInterface
2222
/**
2323
* @var ProductQueryInterface
2424
*/
25-
private $searchQuery;
25+
private ProductQueryInterface $searchQuery;
2626

2727
/**
2828
* @var SearchCriteriaBuilder
2929
*/
30-
private $searchApiCriteriaBuilder;
30+
private SearchCriteriaBuilder $searchApiCriteriaBuilder;
3131

3232
/**
3333
* @param ProductQueryInterface $searchQuery
@@ -63,34 +63,47 @@ public function resolve(
6363
'eq' => $value['id']
6464
]
6565
];
66-
$searchResult = $this->searchQuery->getResult($args, $info, $context);
66+
try {
67+
$searchResult = $this->searchQuery->getResult($args, $info, $context);
6768

68-
//possible division by 0
69-
if ($searchResult->getPageSize()) {
70-
$maxPages = ceil($searchResult->getTotalCount() / $searchResult->getPageSize());
71-
} else {
72-
$maxPages = 0;
73-
}
69+
//possible division by 0
70+
if ($searchResult->getPageSize()) {
71+
$maxPages = ceil($searchResult->getTotalCount() / $searchResult->getPageSize());
72+
} else {
73+
$maxPages = 0;
74+
}
75+
76+
$currentPage = $searchResult->getCurrentPage();
77+
if ($searchResult->getCurrentPage() > $maxPages && $searchResult->getTotalCount() > 0) {
78+
$currentPage = new GraphQlInputException(
79+
__(
80+
'currentPage value %1 specified is greater than the number of pages available.',
81+
[$maxPages]
82+
)
83+
);
84+
}
7485

75-
$currentPage = $searchResult->getCurrentPage();
76-
if ($searchResult->getCurrentPage() > $maxPages && $searchResult->getTotalCount() > 0) {
77-
$currentPage = new GraphQlInputException(
78-
__(
79-
'currentPage value %1 specified is greater than the number of pages available.',
80-
[$maxPages]
81-
)
82-
);
86+
$data = [
87+
'total_count' => $searchResult->getTotalCount(),
88+
'items' => $searchResult->getProductsSearchResult(),
89+
'page_info' => [
90+
'page_size' => $searchResult->getPageSize(),
91+
'current_page' => $currentPage,
92+
'total_pages' => $maxPages
93+
]
94+
];
95+
} catch (\Throwable) {
96+
$data = [
97+
'total_count' => 0,
98+
'items' => [],
99+
'page_info' => [
100+
'page_size' => 0,
101+
'current_page' => 0,
102+
'total_pages' => 0
103+
]
104+
];
83105
}
84106

85-
$data = [
86-
'total_count' => $searchResult->getTotalCount(),
87-
'items' => $searchResult->getProductsSearchResult(),
88-
'page_info' => [
89-
'page_size' => $searchResult->getPageSize(),
90-
'current_page' => $currentPage,
91-
'total_pages' => $maxPages
92-
]
93-
];
94107
return $data;
95108
}
96109
}

0 commit comments

Comments
 (0)