Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

Commit 1fd2493

Browse files
authored
Merge pull request #3622 from magento-engcom/graphql-develop-prs
[EngCom] Public Pull Requests - GraphQL
2 parents b954e0f + 979b9b3 commit 1fd2493

File tree

23 files changed

+513
-313
lines changed

23 files changed

+513
-313
lines changed

app/code/Magento/GraphQl/Controller/GraphQl.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,10 @@ public function dispatch(RequestInterface $request) : ResponseInterface
111111
$data = $this->jsonSerializer->unserialize($request->getContent());
112112

113113
$query = isset($data['query']) ? $data['query'] : '';
114-
114+
$variables = isset($data['variables']) ? $data['variables'] : null;
115115
// We have to extract queried field names to avoid instantiation of non necessary fields in webonyx schema
116116
// Temporal coupling is required for performance optimization
117-
$this->queryFields->setQuery($query);
117+
$this->queryFields->setQuery($query, $variables);
118118
$schema = $this->schemaGenerator->generate();
119119

120120
$result = $this->queryProcessor->process(

app/code/Magento/GraphQl/etc/di.xml

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
<argument name="factoryMapByConfigElementType" xsi:type="array">
2828
<item name="graphql_interface" xsi:type="object">Magento\Framework\GraphQl\Config\Element\InterfaceFactory</item>
2929
<item name="graphql_type" xsi:type="object">Magento\Framework\GraphQl\Config\Element\TypeFactory</item>
30-
<item name="graphql_input" xsi:type="object">Magento\Framework\GraphQl\Config\Element\TypeFactory</item>
30+
<item name="graphql_input" xsi:type="object">Magento\Framework\GraphQl\Config\Element\InputFactory</item>
3131
<item name="graphql_enum" xsi:type="object">Magento\Framework\GraphQl\Config\Element\EnumFactory</item>
3232
</argument>
3333
</arguments>
@@ -55,24 +55,16 @@
5555
</argument>
5656
</arguments>
5757
</virtualType>
58-
<type name="Magento\Framework\GraphQl\Schema\Type\Output\OutputFactory">
58+
<type name="Magento\Framework\GraphQl\Schema\Type\TypeRegistry">
5959
<arguments>
60-
<argument name="prototypes" xsi:type="array">
60+
<argument name="configToTypeMap" xsi:type="array">
6161
<item name="Magento\Framework\GraphQl\Config\Element\Type" xsi:type="string">Magento\Framework\GraphQl\Schema\Type\Output\OutputTypeObject</item>
62+
<item name="Magento\Framework\GraphQl\Config\Element\Input" xsi:type="string">Magento\Framework\GraphQl\Schema\Type\Input\InputObjectType</item>
6263
<item name="Magento\Framework\GraphQl\Config\Element\InterfaceType" xsi:type="string">Magento\Framework\GraphQl\Schema\Type\Output\OutputInterfaceObject</item>
6364
<item name="Magento\Framework\GraphQl\Config\Element\Enum" xsi:type="string">Magento\Framework\GraphQl\Schema\Type\Enum\Enum</item>
6465
</argument>
6566
</arguments>
6667
</type>
67-
<type name="Magento\Framework\GraphQl\Schema\Type\Input\InputFactory">
68-
<arguments>
69-
<argument name="prototypes" xsi:type="array">
70-
<item name="Magento\Framework\GraphQl\Config\Element\Type" xsi:type="string">Magento\Framework\GraphQl\Schema\Type\Input\InputObjectType</item>
71-
<item name="Magento\Framework\GraphQl\Config\Element\InterfaceType" xsi:type="string">Magento\Framework\GraphQl\Schema\Type\Input\InputObjectType</item>
72-
<item name="Magento\Framework\GraphQl\Config\Element\Enum" xsi:type="string">Magento\Framework\GraphQl\Schema\Type\Enum\Enum</item>
73-
</argument>
74-
</arguments>
75-
</type>
7668
<type name="Magento\Framework\GraphQl\Schema\Type\Output\ElementMapper">
7769
<arguments>
7870
<argument name="formatter" xsi:type="object">Magento\Framework\GraphQl\Schema\Type\Output\ElementMapper\FormatterComposite</argument>

dev/tests/api-functional/framework/Magento/TestFramework/TestCase/GraphQl/Client.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ public function postQuery(string $query, array $variables = [], string $operatio
5757
$headers = array_merge($headers, ['Accept: application/json', 'Content-Type: application/json']);
5858
$requestArray = [
5959
'query' => $query,
60-
'variables' => empty($variables) ? $variables : null,
61-
'operationName' => empty($operationName) ? $operationName : null
60+
'variables' => !empty($variables) ? $variables : null,
61+
'operationName' => !empty($operationName) ? $operationName : null
6262
];
6363
$postData = $this->json->jsonEncode($requestArray);
6464

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\GraphQl;
9+
10+
use Magento\TestFramework\Helper\Bootstrap;
11+
use Magento\TestFramework\TestCase\GraphQlAbstract;
12+
use Magento\Catalog\Api\ProductRepositoryInterface;
13+
14+
class VariablesSupportQueryTest extends GraphQlAbstract
15+
{
16+
/**
17+
* @var ProductRepositoryInterface
18+
*/
19+
private $productRepository;
20+
21+
protected function setUp()
22+
{
23+
$this->productRepository = Bootstrap::getObjectManager()->get(ProductRepositoryInterface::class);
24+
}
25+
26+
/**
27+
* @magentoApiDataFixture Magento/Catalog/_files/products_list.php
28+
*/
29+
public function testQueryObjectVariablesSupport()
30+
{
31+
$productSku = 'simple-249';
32+
$minPrice = 153;
33+
34+
$query
35+
= <<<'QUERY'
36+
query GetProductsQuery($pageSize: Int, $filterInput: ProductFilterInput, $priceSort: SortEnum) {
37+
products(
38+
pageSize: $pageSize
39+
filter: $filterInput
40+
sort: {price: $priceSort}
41+
) {
42+
items {
43+
sku
44+
price {
45+
minimalPrice {
46+
amount {
47+
value
48+
currency
49+
}
50+
}
51+
}
52+
}
53+
}
54+
}
55+
QUERY;
56+
57+
$variables = [
58+
'pageSize' => 1,
59+
'priceSort' => 'ASC',
60+
'filterInput' => [
61+
'min_price' => [
62+
'gt' => 150,
63+
],
64+
],
65+
];
66+
67+
$response = $this->graphQlQuery($query, $variables);
68+
/** @var \Magento\Catalog\Model\Product $product */
69+
$product = $this->productRepository->get($productSku, false, null, true);
70+
71+
self::assertArrayHasKey('products', $response);
72+
self::assertArrayHasKey('items', $response['products']);
73+
self::assertEquals(1, count($response['products']['items']));
74+
self::assertArrayHasKey(0, $response['products']['items']);
75+
self::assertEquals($product->getSku(), $response['products']['items'][0]['sku']);
76+
self::assertEquals(
77+
$minPrice,
78+
$response['products']['items'][0]['price']['minimalPrice']['amount']['value']
79+
);
80+
}
81+
}

lib/internal/Magento/Framework/GraphQl/Config.php

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,7 @@ public function __construct(
4848
}
4949

5050
/**
51-
* Get a data object with data pertaining to a GraphQL type's structural makeup.
52-
*
53-
* @param string $configElementName
54-
* @return ConfigElementInterface
55-
* @throws \LogicException
56-
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
51+
* @inheritdoc
5752
*/
5853
public function getConfigElement(string $configElementName) : ConfigElementInterface
5954
{
@@ -67,7 +62,7 @@ public function getConfigElement(string $configElementName) : ConfigElementInter
6762
$fieldsInQuery = $this->queryFields->getFieldsUsedInQuery();
6863
if (isset($data['fields'])) {
6964
if (!empty($fieldsInQuery)) {
70-
foreach ($data['fields'] as $fieldName => $fieldConfig) {
65+
foreach (array_keys($data['fields']) as $fieldName) {
7166
if (!isset($fieldsInQuery[$fieldName])) {
7267
unset($data['fields'][$fieldName]);
7368
}
@@ -81,18 +76,20 @@ public function getConfigElement(string $configElementName) : ConfigElementInter
8176
}
8277

8378
/**
84-
* Return all type names declared in a GraphQL schema's configuration.
85-
*
86-
* @return string[]
79+
* @inheritdoc
8780
*/
88-
public function getDeclaredTypeNames() : array
81+
public function getDeclaredTypes() : array
8982
{
9083
$types = [];
9184
foreach ($this->configData->get(null) as $item) {
92-
if (isset($item['type']) && $item['type'] == 'graphql_type') {
93-
$types[] = $item['name'];
85+
if (isset($item['type'])) {
86+
$types[] = [
87+
'name' => $item['name'],
88+
'type' => $item['type'],
89+
];
9490
}
9591
}
92+
9693
return $types;
9794
}
9895
}

lib/internal/Magento/Framework/GraphQl/Config/Element/Enum.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class Enum implements ConfigElementInterface
3737
public function __construct(
3838
string $name,
3939
array $values,
40-
string $description = ""
40+
string $description
4141
) {
4242
$this->name = $name;
4343
$this->values = $values;
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Framework\GraphQl\Config\Element;
9+
10+
/**
11+
* Fields object factory
12+
*/
13+
class FieldsFactory
14+
{
15+
/**
16+
* @var ArgumentFactory
17+
*/
18+
private $argumentFactory;
19+
20+
/**
21+
* @var FieldFactory
22+
*/
23+
private $fieldFactory;
24+
25+
/**
26+
* @param ArgumentFactory $argumentFactory
27+
* @param FieldFactory $fieldFactory
28+
*/
29+
public function __construct(
30+
ArgumentFactory $argumentFactory,
31+
FieldFactory $fieldFactory
32+
) {
33+
$this->argumentFactory = $argumentFactory;
34+
$this->fieldFactory = $fieldFactory;
35+
}
36+
37+
/**
38+
* Create a fields object from a configured array with optional arguments.
39+
*
40+
* Field data must contain name and type. Other values are optional and include required, itemType, description,
41+
* and resolver. Arguments array must be in the format of [$argumentData['name'] => $argumentData].
42+
*
43+
* @param array $fieldsData
44+
* @return Field[]
45+
*/
46+
public function createFromConfigData(
47+
array $fieldsData
48+
) : array {
49+
$fields = [];
50+
foreach ($fieldsData as $fieldData) {
51+
$arguments = [];
52+
foreach ($fieldData['arguments'] as $argumentData) {
53+
$arguments[$argumentData['name']] = $this->argumentFactory->createFromConfigData($argumentData);
54+
}
55+
$fields[$fieldData['name']] = $this->fieldFactory->createFromConfigData(
56+
$fieldData,
57+
$arguments
58+
);
59+
}
60+
return $fields;
61+
}
62+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Framework\GraphQl\Config\Element;
9+
10+
/**
11+
* Class representing 'input' GraphQL config element.
12+
*/
13+
class Input implements TypeInterface
14+
{
15+
/**
16+
* @var string
17+
*/
18+
private $name;
19+
20+
/**
21+
* @var Field[]
22+
*/
23+
private $fields;
24+
25+
/**
26+
* @var string
27+
*/
28+
private $description;
29+
30+
/**
31+
* @param string $name
32+
* @param Field[] $fields
33+
* @param string $description
34+
*/
35+
public function __construct(
36+
string $name,
37+
array $fields,
38+
string $description
39+
) {
40+
$this->name = $name;
41+
$this->fields = $fields;
42+
$this->description = $description;
43+
}
44+
45+
/**
46+
* Get the type name.
47+
*
48+
* @return string
49+
*/
50+
public function getName(): string
51+
{
52+
return $this->name;
53+
}
54+
55+
/**
56+
* Get a list of fields that make up the possible return or input values of a type.
57+
*
58+
* @return Field[]
59+
*/
60+
public function getFields(): array
61+
{
62+
return $this->fields;
63+
}
64+
65+
/**
66+
* Get a human-readable description of the type.
67+
*
68+
* @return string
69+
*/
70+
public function getDescription(): string
71+
{
72+
return $this->description;
73+
}
74+
}

0 commit comments

Comments
 (0)