This repository has been archived by the owner on Dec 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENGCOM-3931: GraphQl-93: Implement support for variables in query #259
- Loading branch information
Showing
23 changed files
with
513 additions
and
313 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
dev/tests/api-functional/testsuite/Magento/GraphQl/VariablesSupportQueryTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\GraphQl; | ||
|
||
use Magento\TestFramework\Helper\Bootstrap; | ||
use Magento\TestFramework\TestCase\GraphQlAbstract; | ||
use Magento\Catalog\Api\ProductRepositoryInterface; | ||
|
||
class VariablesSupportQueryTest extends GraphQlAbstract | ||
{ | ||
/** | ||
* @var ProductRepositoryInterface | ||
*/ | ||
private $productRepository; | ||
|
||
protected function setUp() | ||
{ | ||
$this->productRepository = Bootstrap::getObjectManager()->get(ProductRepositoryInterface::class); | ||
} | ||
|
||
/** | ||
* @magentoApiDataFixture Magento/Catalog/_files/products_list.php | ||
*/ | ||
public function testQueryObjectVariablesSupport() | ||
{ | ||
$productSku = 'simple-249'; | ||
$minPrice = 153; | ||
|
||
$query | ||
= <<<'QUERY' | ||
query GetProductsQuery($pageSize: Int, $filterInput: ProductFilterInput, $priceSort: SortEnum) { | ||
products( | ||
pageSize: $pageSize | ||
filter: $filterInput | ||
sort: {price: $priceSort} | ||
) { | ||
items { | ||
sku | ||
price { | ||
minimalPrice { | ||
amount { | ||
value | ||
currency | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
QUERY; | ||
|
||
$variables = [ | ||
'pageSize' => 1, | ||
'priceSort' => 'ASC', | ||
'filterInput' => [ | ||
'min_price' => [ | ||
'gt' => 150, | ||
], | ||
], | ||
]; | ||
|
||
$response = $this->graphQlQuery($query, $variables); | ||
/** @var \Magento\Catalog\Model\Product $product */ | ||
$product = $this->productRepository->get($productSku, false, null, true); | ||
|
||
self::assertArrayHasKey('products', $response); | ||
self::assertArrayHasKey('items', $response['products']); | ||
self::assertEquals(1, count($response['products']['items'])); | ||
self::assertArrayHasKey(0, $response['products']['items']); | ||
self::assertEquals($product->getSku(), $response['products']['items'][0]['sku']); | ||
self::assertEquals( | ||
$minPrice, | ||
$response['products']['items'][0]['price']['minimalPrice']['amount']['value'] | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
lib/internal/Magento/Framework/GraphQl/Config/Element/FieldsFactory.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Framework\GraphQl\Config\Element; | ||
|
||
/** | ||
* Fields object factory | ||
*/ | ||
class FieldsFactory | ||
{ | ||
/** | ||
* @var ArgumentFactory | ||
*/ | ||
private $argumentFactory; | ||
|
||
/** | ||
* @var FieldFactory | ||
*/ | ||
private $fieldFactory; | ||
|
||
/** | ||
* @param ArgumentFactory $argumentFactory | ||
* @param FieldFactory $fieldFactory | ||
*/ | ||
public function __construct( | ||
ArgumentFactory $argumentFactory, | ||
FieldFactory $fieldFactory | ||
) { | ||
$this->argumentFactory = $argumentFactory; | ||
$this->fieldFactory = $fieldFactory; | ||
} | ||
|
||
/** | ||
* Create a fields object from a configured array with optional arguments. | ||
* | ||
* Field data must contain name and type. Other values are optional and include required, itemType, description, | ||
* and resolver. Arguments array must be in the format of [$argumentData['name'] => $argumentData]. | ||
* | ||
* @param array $fieldsData | ||
* @return Field[] | ||
*/ | ||
public function createFromConfigData( | ||
array $fieldsData | ||
) : array { | ||
$fields = []; | ||
foreach ($fieldsData as $fieldData) { | ||
$arguments = []; | ||
foreach ($fieldData['arguments'] as $argumentData) { | ||
$arguments[$argumentData['name']] = $this->argumentFactory->createFromConfigData($argumentData); | ||
} | ||
$fields[$fieldData['name']] = $this->fieldFactory->createFromConfigData( | ||
$fieldData, | ||
$arguments | ||
); | ||
} | ||
return $fields; | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
lib/internal/Magento/Framework/GraphQl/Config/Element/Input.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Framework\GraphQl\Config\Element; | ||
|
||
/** | ||
* Class representing 'input' GraphQL config element. | ||
*/ | ||
class Input implements TypeInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $name; | ||
|
||
/** | ||
* @var Field[] | ||
*/ | ||
private $fields; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $description; | ||
|
||
/** | ||
* @param string $name | ||
* @param Field[] $fields | ||
* @param string $description | ||
*/ | ||
public function __construct( | ||
string $name, | ||
array $fields, | ||
string $description | ||
) { | ||
$this->name = $name; | ||
$this->fields = $fields; | ||
$this->description = $description; | ||
} | ||
|
||
/** | ||
* Get the type name. | ||
* | ||
* @return string | ||
*/ | ||
public function getName(): string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
/** | ||
* Get a list of fields that make up the possible return or input values of a type. | ||
* | ||
* @return Field[] | ||
*/ | ||
public function getFields(): array | ||
{ | ||
return $this->fields; | ||
} | ||
|
||
/** | ||
* Get a human-readable description of the type. | ||
* | ||
* @return string | ||
*/ | ||
public function getDescription(): string | ||
{ | ||
return $this->description; | ||
} | ||
} |
Oops, something went wrong.