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
GraphQl-93: Implement support for variables in query #259
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
958a0ac
GraphQl-93: Implement support for variables in query
Aquarvin a3544dc
Merge remote-tracking branch 'origin/2.3-develop' into graphql-93
6f585f4
GraphQl-93: Implement support for variables in query
naydav 0985244
GraphQl-93: Implement support for variables in query
naydav b3c194f
GraphQl-93: Implement support for variables in query
naydav 2e74cc1
GraphQl-93: Implement support for variables in query
naydav File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
90 changes: 90 additions & 0 deletions
90
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,90 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\GraphQl; | ||
|
||
use Magento\Catalog\Api\Data\ProductInterface; | ||
use Magento\TestFramework\TestCase\GraphQlAbstract; | ||
use Magento\TestFramework\ObjectManager; | ||
use Magento\Catalog\Api\ProductRepositoryInterface; | ||
|
||
class VariablesSupportQueryTest extends GraphQlAbstract | ||
{ | ||
/** | ||
* @var ObjectManager | ||
*/ | ||
private $objectManager; | ||
|
||
/** | ||
* @var ProductRepositoryInterface | ||
*/ | ||
private $productRepository; | ||
|
||
protected function setUp() | ||
{ | ||
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); | ||
$this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class); | ||
} | ||
|
||
/** | ||
* Tests that Introspection is disabled when not in developer mode | ||
* | ||
* @magentoApiDataFixture Magento/Catalog/_files/product_simple_with_all_fields.php | ||
* @SuppressWarnings(PHPMD.ExcessiveMethodLength) | ||
*/ | ||
public function testQueryObjectVariablesSupport() | ||
{ | ||
$productSku = 'simple'; | ||
|
||
$query | ||
= <<<'QUERY' | ||
query GetProductsQuery($page: Int, $filterInput: ProductFilterInput){ | ||
products( | ||
pageSize: 10 | ||
currentPage: $page | ||
filter: $filterInput | ||
sort: {} | ||
) { | ||
items { | ||
name | ||
} | ||
} | ||
} | ||
QUERY; | ||
$variables = [ | ||
'page' => 1, | ||
'filterInput' => [ | ||
'sku' => [ | ||
'like' => '%simple%' | ||
] | ||
] | ||
]; | ||
|
||
$response = $this->graphQlQuery($query, $variables); | ||
/** @var \Magento\Catalog\Model\Product $product */ | ||
$product = $this->productRepository->get($productSku, false, null, true); | ||
|
||
$this->assertArrayHasKey('products', $response); | ||
$this->assertArrayHasKey('items', $response['products']); | ||
$this->assertEquals(1, count($response['products']['items'])); | ||
$this->assertArrayHasKey(0, $response['products']['items']); | ||
$this->assertFields($product, $response['products']['items'][0]); | ||
} | ||
|
||
/** | ||
* @param ProductInterface $product | ||
* @param array $actualResponse | ||
*/ | ||
private function assertFields($product, $actualResponse) | ||
{ | ||
$assertionMap = [ | ||
['response_field' => 'name', 'expected_value' => $product->getName()], | ||
]; | ||
|
||
$this->assertResponseFields($actualResponse, $assertionMap); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -81,18 +81,24 @@ public function getConfigElement(string $configElementName) : ConfigElementInter | |
} | ||
|
||
/** | ||
* Return all type names declared in a GraphQL schema's configuration. | ||
* Return all type names declared in a GraphQL schema's configuration and their type. | ||
* | ||
* @return string[] | ||
* @return array $types | ||
* name string | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this common style for documenting array structure? Usually I see |
||
* type string | ||
*/ | ||
public function getDeclaredTypeNames() : array | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we rename this method, to |
||
{ | ||
$types = []; | ||
foreach ($this->configData->get(null) as $item) { | ||
if (isset($item['type']) && $item['type'] == 'graphql_type') { | ||
$types[] = $item['name']; | ||
if (isset($item['type'])) { | ||
$types[] = [ | ||
'name' => $item['name'], | ||
'type' => $item['type'], | ||
]; | ||
} | ||
} | ||
|
||
return $types; | ||
} | ||
} |
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
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; | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
lib/internal/Magento/Framework/GraphQl/Config/Element/InputFactory.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,97 @@ | ||||||
<?php | ||||||
/** | ||||||
* Copyright © Magento, Inc. All rights reserved. | ||||||
* See COPYING.txt for license details. | ||||||
*/ | ||||||
declare(strict_types=1); | ||||||
|
||||||
namespace Magento\Framework\GraphQl\Config\Element; | ||||||
|
||||||
use Magento\Framework\GraphQl\Config\ConfigElementFactoryInterface; | ||||||
use Magento\Framework\GraphQl\Config\ConfigElementInterface; | ||||||
use Magento\Framework\ObjectManagerInterface; | ||||||
|
||||||
/** | ||||||
* Factory for config elements of 'input' type. | ||||||
*/ | ||||||
class InputFactory implements ConfigElementFactoryInterface | ||||||
{ | ||||||
/** | ||||||
* @var ObjectManagerInterface | ||||||
*/ | ||||||
private $objectManager; | ||||||
|
||||||
/** | ||||||
* @var ArgumentFactory | ||||||
*/ | ||||||
private $argumentFactory; | ||||||
|
||||||
/** | ||||||
* @var FieldFactory | ||||||
*/ | ||||||
private $fieldFactory; | ||||||
|
||||||
/** | ||||||
* @param ObjectManagerInterface $objectManager | ||||||
* @param ArgumentFactory $argumentFactory | ||||||
* @param FieldFactory $fieldFactory | ||||||
*/ | ||||||
public function __construct( | ||||||
ObjectManagerInterface $objectManager, | ||||||
ArgumentFactory $argumentFactory, | ||||||
FieldFactory $fieldFactory | ||||||
) { | ||||||
$this->objectManager = $objectManager; | ||||||
$this->argumentFactory = $argumentFactory; | ||||||
$this->fieldFactory = $fieldFactory; | ||||||
} | ||||||
|
||||||
/** | ||||||
* Instantiate an object representing 'input' GraphQL config element. | ||||||
* | ||||||
* @param array $data | ||||||
* @return ConfigElementInterface | ||||||
*/ | ||||||
public function createFromConfigData(array $data): ConfigElementInterface | ||||||
{ | ||||||
$fields = []; | ||||||
$data['fields'] = isset($data['fields']) ? $data['fields'] : []; | ||||||
foreach ($data['fields'] as $field) { | ||||||
$arguments = []; | ||||||
foreach ($field['arguments'] as $argument) { | ||||||
$arguments[$argument['name']] = $this->argumentFactory->createFromConfigData($argument); | ||||||
} | ||||||
$fields[$field['name']] = $this->fieldFactory->createFromConfigData( | ||||||
$field, | ||||||
$arguments | ||||||
); | ||||||
} | ||||||
return $this->create( | ||||||
$data, | ||||||
$fields | ||||||
); | ||||||
} | ||||||
|
||||||
/** | ||||||
* Create type object based off array of configured GraphQL InputType data. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
* | ||||||
* Type data must contain name and the type's fields. Optional data includes description. | ||||||
* | ||||||
* @param array $typeData | ||||||
* @param array $fields | ||||||
* @return Input | ||||||
*/ | ||||||
private function create( | ||||||
array $typeData, | ||||||
array $fields | ||||||
): Input { | ||||||
return $this->objectManager->create( | ||||||
Input::class, | ||||||
[ | ||||||
'name' => $typeData['name'], | ||||||
'fields' => $fields, | ||||||
'description' => isset($typeData['description']) ? $typeData['description'] : '' | ||||||
] | ||||||
); | ||||||
} | ||||||
} |
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
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -8,7 +8,7 @@ | |||||
namespace Magento\Framework\GraphQl\Config\Element; | ||||||
|
||||||
/** | ||||||
* Describes all the configured data of an Output or Input type in GraphQL. | ||||||
* Class representing 'type' GraphQL config element. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
*/ | ||||||
class Type implements TypeInterface | ||||||
{ | ||||||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have enum used here?