Skip to content

Commit 94f5296

Browse files
committed
magento/graphql-ce#727: currentPage: 0, currentPage: 1 and currentPage: -1 produces the same output for products query when filtering is used
- added currentPage and pageSize validation; - fixed broken pagination; - fixed benchmark.jms that relied on wrong query params.
1 parent 57ffbd9 commit 94f5296

File tree

5 files changed

+75
-5
lines changed

5 files changed

+75
-5
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ public function resolve(
6666
]
6767
];
6868
$searchCriteria = $this->searchCriteriaBuilder->build($field->getName(), $args);
69+
if ($args['currentPage'] < 1) {
70+
throw new GraphQlInputException(__('currentPage value must be greater than 0.'));
71+
}
72+
if ($args['pageSize'] < 1) {
73+
throw new GraphQlInputException(__('pageSize value must be greater than 0.'));
74+
}
6975
$searchCriteria->setCurrentPage($args['currentPage']);
7076
$searchCriteria->setPageSize($args['pageSize']);
7177
$searchResult = $this->filterQuery->getResult($searchCriteria, $info);

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ public function resolve(
7171
array $args = null
7272
) {
7373
$searchCriteria = $this->searchCriteriaBuilder->build($field->getName(), $args);
74+
if ($args['currentPage'] < 1) {
75+
throw new GraphQlInputException(__('currentPage value must be greater than 0.'));
76+
}
77+
if ($args['pageSize'] < 1) {
78+
throw new GraphQlInputException(__('pageSize value must be greater than 0.'));
79+
}
7480
$searchCriteria->setCurrentPage($args['currentPage']);
7581
$searchCriteria->setPageSize($args['pageSize']);
7682
if (!isset($args['search']) && !isset($args['filter'])) {

app/code/Magento/CatalogGraphQl/Model/Resolver/Products/Query/Search.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ private function paginateList(SearchResult $searchResult, SearchCriteriaInterfac
144144
$offset = $length * ($searchCriteria->getCurrentPage() - 1);
145145

146146
if ($searchCriteria->getPageSize()) {
147-
$maxPages = ceil($searchResult->getTotalCount() / $searchCriteria->getPageSize()) - 1;
147+
$maxPages = ceil($searchResult->getTotalCount() / $searchCriteria->getPageSize());
148148
} else {
149149
$maxPages = 0;
150150
}

dev/tests/api-functional/testsuite/Magento/GraphQl/Catalog/ProductSearchTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,6 +1137,64 @@ public function testFilterProductsThatAreOutOfStockWithConfigSettings()
11371137
$this->assertEquals(1, $response['products']['total_count']);
11381138
}
11391139

1140+
/**
1141+
* Verify that invalid current page return an error
1142+
*
1143+
* @magentoApiDataFixture Magento/Catalog/_files/products_with_layered_navigation_attribute.php
1144+
* @expectedException \Exception
1145+
* @expectedExceptionMessage currentPage value must be greater than 0
1146+
*/
1147+
public function testInvalidCurrentPage()
1148+
{
1149+
$query = <<<QUERY
1150+
{
1151+
products (
1152+
filter: {
1153+
sku: {
1154+
like:"simple%"
1155+
}
1156+
}
1157+
pageSize: 4
1158+
currentPage: 0
1159+
) {
1160+
items {
1161+
sku
1162+
}
1163+
}
1164+
}
1165+
QUERY;
1166+
$this->graphQlQuery($query);
1167+
}
1168+
1169+
/**
1170+
* Verify that invalid page size returns an error.
1171+
*
1172+
* @magentoApiDataFixture Magento/Catalog/_files/products_with_layered_navigation_attribute.php
1173+
* @expectedException \Exception
1174+
* @expectedExceptionMessage pageSize value must be greater than 0
1175+
*/
1176+
public function testInvalidPageSize()
1177+
{
1178+
$query = <<<QUERY
1179+
{
1180+
products (
1181+
filter: {
1182+
sku: {
1183+
like:"simple%"
1184+
}
1185+
}
1186+
pageSize: 0
1187+
currentPage: 1
1188+
) {
1189+
items {
1190+
sku
1191+
}
1192+
}
1193+
}
1194+
QUERY;
1195+
$this->graphQlQuery($query);
1196+
}
1197+
11401198
/**
11411199
* Asserts the different fields of items returned after search query is executed
11421200
*

setup/performance-toolkit/benchmark.jmx

Lines changed: 4 additions & 4 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)