Skip to content

Commit 40f9a4b

Browse files
authored
Merge pull request #6081 from magento-engcom/2.4-develop-graphql-prs
[GraphQL] Partners Acceleration Program Contributions - 2.4-develop
2 parents 8e2d385 + 5936296 commit 40f9a4b

File tree

32 files changed

+583
-58
lines changed

32 files changed

+583
-58
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
<arguments>
3030
<argument name="factoryMapByConfigElementType" xsi:type="array">
3131
<item name="graphql_interface" xsi:type="object">Magento\Framework\GraphQl\Config\Element\InterfaceFactory</item>
32+
<item name="graphql_union" xsi:type="object">Magento\Framework\GraphQl\Config\Element\UnionFactory</item>
3233
<item name="graphql_type" xsi:type="object">Magento\Framework\GraphQl\Config\Element\TypeFactory</item>
3334
<item name="graphql_input" xsi:type="object">Magento\Framework\GraphQl\Config\Element\InputFactory</item>
3435
<item name="graphql_enum" xsi:type="object">Magento\Framework\GraphQl\Config\Element\EnumFactory</item>
@@ -64,6 +65,7 @@
6465
<item name="Magento\Framework\GraphQl\Config\Element\Type" xsi:type="string">Magento\Framework\GraphQl\Schema\Type\Output\OutputTypeObject</item>
6566
<item name="Magento\Framework\GraphQl\Config\Element\Input" xsi:type="string">Magento\Framework\GraphQl\Schema\Type\Input\InputObjectType</item>
6667
<item name="Magento\Framework\GraphQl\Config\Element\InterfaceType" xsi:type="string">Magento\Framework\GraphQl\Schema\Type\Output\OutputInterfaceObject</item>
68+
<item name="Magento\Framework\GraphQl\Config\Element\UnionType" xsi:type="string">Magento\Framework\GraphQl\Schema\Type\Output\OutputUnionObject</item>
6769
<item name="Magento\Framework\GraphQl\Config\Element\Enum" xsi:type="string">Magento\Framework\GraphQl\Schema\Type\Enum\Enum</item>
6870
</argument>
6971
</arguments>
@@ -78,13 +80,15 @@
7880
<argument name="formatters" xsi:type="array">
7981
<item name="fields" xsi:type="object">Magento\Framework\GraphQl\Schema\Type\Output\ElementMapper\Formatter\Fields</item>
8082
<item name="interfaces" xsi:type="object">Magento\Framework\GraphQl\Schema\Type\Output\ElementMapper\Formatter\Interfaces</item>
83+
<item name="unions" xsi:type="object">Magento\Framework\GraphQl\Schema\Type\Output\ElementMapper\Formatter\Unions</item>
8184
<item name="resolveType" xsi:type="object">Magento\Framework\GraphQl\Schema\Type\Output\ElementMapper\Formatter\ResolveType</item>
8285
</argument>
8386
</arguments>
8487
</type>
8588
<type name="Magento\Framework\GraphQlSchemaStitching\GraphQlReader\TypeReaderComposite">
8689
<arguments>
8790
<argument name="typeReaders" xsi:type="array">
91+
<item name="union_type" xsi:type="object">Magento\Framework\GraphQlSchemaStitching\GraphQlReader\Reader\UnionType</item>
8892
<item name="enum_type" xsi:type="object">Magento\Framework\GraphQlSchemaStitching\GraphQlReader\Reader\EnumType</item>
8993
<item name="object_type" xsi:type="object">Magento\Framework\GraphQlSchemaStitching\GraphQlReader\Reader\ObjectType</item>
9094
<item name="input_object_type" xsi:type="object">Magento\Framework\GraphQlSchemaStitching\GraphQlReader\Reader\InputObjectType</item>

app/code/Magento/GraphQl/etc/schema.graphqls

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ directive @resolver(class: String="") on QUERY
3030
| OBJECT
3131
| FIELD_DEFINITION
3232
| ARGUMENT_DEFINITION
33-
| INTERFACE
34-
| UNION
3533
| ENUM
3634
| ENUM_VALUE
3735
| INPUT_OBJECT
3836
| INPUT_FIELD_DEFINITION
3937

40-
directive @typeResolver(class: String="") on INTERFACE | OBJECT
38+
directive @typeResolver(class: String="") on UNION
39+
| INTERFACE
40+
| OBJECT
4141

4242
directive @cache(cacheIdentity: String="" cacheable: Boolean=true) on QUERY
4343

dev/tests/api-functional/_files/Magento/TestModuleGraphQlQuery/Model/Resolver/Item.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
use Magento\Framework\GraphQl\Config\Element\Field;
1212
use Magento\Framework\GraphQl\Query\ResolverInterface;
1313

14+
/**
15+
* Resolver for Item
16+
*/
1417
class Item implements ResolverInterface
1518
{
1619
/**
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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\TestModuleGraphQlQuery\Model\Resolver;
9+
10+
use Magento\Framework\GraphQl\Config\Element\Field;
11+
use Magento\Framework\GraphQl\Query\ResolverInterface;
12+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
13+
14+
/**
15+
* Resolver for Union type TestUnion
16+
*/
17+
class TestUnion implements ResolverInterface
18+
{
19+
/**
20+
* @inheritDoc
21+
*/
22+
public function resolve(
23+
Field $field,
24+
$context,
25+
ResolveInfo $info,
26+
array $value = null,
27+
array $args = null
28+
) {
29+
return [
30+
'custom_name1' => 'custom_name1_value',
31+
'custom_name2' => 'custom_name2_value',
32+
];
33+
}
34+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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\TestModuleGraphQlQuery\Model\Resolver;
9+
10+
use Magento\Framework\GraphQl\Query\Resolver\TypeResolverInterface;
11+
12+
/**
13+
* Type Resolver for union
14+
*/
15+
class UnionTypeResolver implements TypeResolverInterface
16+
{
17+
/**
18+
* @inheritDoc
19+
*/
20+
public function resolveType(array $data): string
21+
{
22+
if (!empty($data)) {
23+
return 'TypeCustom1';
24+
}
25+
return '';
26+
}
27+
}

dev/tests/api-functional/_files/Magento/TestModuleGraphQlQuery/etc/schema.graphqls

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
type Query {
55
testItem(id: Int!) : Item @resolver(class: "Magento\\TestModuleGraphQlQuery\\Model\\Resolver\\Item")
6+
testUnion: TestUnion @resolver(class: "Magento\\TestModuleGraphQlQuery\\Model\\Resolver\\TestUnion")
67
}
78

89
type Mutation {
@@ -18,3 +19,14 @@ type MutationItem {
1819
item_id: Int
1920
name: String
2021
}
22+
23+
union TestUnion @doc(description: "some kind of union") @typeResolver(class: "Magento\\TestModuleGraphQlQuery\\Model\\Resolver\\UnionTypeResolver") =
24+
TypeCustom1 | TypeCustom2
25+
26+
type TypeCustom1 {
27+
custom_name1: String
28+
}
29+
30+
type TypeCustom2 {
31+
custom_name2: String
32+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ private function processResponseHeaders(string $headers): array
213213

214214
$headerLines = preg_split('/((\r?\n)|(\r\n?))/', $headers);
215215
foreach ($headerLines as $headerLine) {
216-
$headerParts = preg_split('/:/', $headerLine);
216+
$headerParts = preg_split('/: /', $headerLine, 2);
217217
if (count($headerParts) == 2) {
218218
$headersArray[trim($headerParts[0])] = trim($headerParts[1]);
219219
} elseif (preg_match('/HTTP\/[\.0-9]+/', $headerLine)) {

dev/tests/api-functional/testsuite/Magento/GraphQl/CorsHeadersTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function testCorsHeadersWhenCorsIsEnabled(): void
7676
$this->resourceConfig->saveConfig(Configuration::XML_PATH_CORS_ALLOWED_HEADERS, 'Origin');
7777
$this->resourceConfig->saveConfig(Configuration::XML_PATH_CORS_ALLOW_CREDENTIALS, '1');
7878
$this->resourceConfig->saveConfig(Configuration::XML_PATH_CORS_ALLOWED_METHODS, 'GET,POST');
79-
$this->resourceConfig->saveConfig(Configuration::XML_PATH_CORS_ALLOWED_ORIGINS, 'magento.local');
79+
$this->resourceConfig->saveConfig(Configuration::XML_PATH_CORS_ALLOWED_ORIGINS, 'http://magento.local');
8080
$this->resourceConfig->saveConfig(Configuration::XML_PATH_CORS_MAX_AGE, '86400');
8181
$this->reinitConfig->reinit();
8282

@@ -85,7 +85,7 @@ public function testCorsHeadersWhenCorsIsEnabled(): void
8585
self::assertEquals('Origin', $headers['Access-Control-Allow-Headers']);
8686
self::assertEquals('1', $headers['Access-Control-Allow-Credentials']);
8787
self::assertEquals('GET,POST', $headers['Access-Control-Allow-Methods']);
88-
self::assertEquals('magento.local', $headers['Access-Control-Allow-Origin']);
88+
self::assertEquals('http://magento.local', $headers['Access-Control-Allow-Origin']);
8989
self::assertEquals('86400', $headers['Access-Control-Max-Age']);
9090
}
9191

dev/tests/api-functional/testsuite/Magento/GraphQl/TestModule/GraphQlQueryTest.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use Magento\TestFramework\TestCase\GraphQlAbstract;
1111

1212
/**
13-
* Class GraphQlQueryTest
13+
* Test for basic GraphQl features
1414
*/
1515
class GraphQlQueryTest extends GraphQlAbstract
1616
{
@@ -100,4 +100,29 @@ public function testQueryViaGetRequestWithVariablesReturnsResults()
100100

101101
$this->assertArrayHasKey('testItem', $response);
102102
}
103+
104+
public function testQueryTestUnionResults()
105+
{
106+
$query = <<<QUERY
107+
{
108+
testUnion {
109+
__typename
110+
... on TypeCustom1 {
111+
custom_name1
112+
}
113+
... on TypeCustom2 {
114+
custom_name2
115+
}
116+
}
117+
}
118+
QUERY;
119+
120+
$response = $this->graphQlQuery($query);
121+
122+
$this->assertArrayHasKey('testUnion', $response);
123+
$testUnion = $response['testUnion'];
124+
$this->assertArrayHasKey('custom_name1', $testUnion);
125+
$this->assertEquals('custom_name1_value', $testUnion['custom_name1']);
126+
$this->assertArrayNotHasKey('custom_name2', $testUnion);
127+
}
103128
}

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,15 @@ public function getFields() : array
7373
/**
7474
* Get interfaces the type implements, if any. Return an empty array if none are configured.
7575
*
76-
* @return string[]
76+
* Example return array(
77+
* array(
78+
* 'interface' => 'SomeDefinedTypeInterface',
79+
* 'copyFields' => true
80+
* ),
81+
* ...
82+
* ),
83+
*
84+
* @return array
7785
*/
7886
public function getInterfaces() : array
7987
{
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
use Magento\Framework\GraphQl\Config\ConfigElementFactoryInterface;
11+
use Magento\Framework\GraphQl\Config\ConfigElementInterface;
12+
use Magento\Framework\ObjectManagerInterface;
13+
14+
/**
15+
* Factory for config elements of 'union' type.
16+
*/
17+
class UnionFactory implements ConfigElementFactoryInterface
18+
{
19+
/**
20+
* @var ObjectManagerInterface
21+
*/
22+
private $objectManager;
23+
24+
/**
25+
* @param ObjectManagerInterface $objectManager
26+
*/
27+
public function __construct(
28+
ObjectManagerInterface $objectManager
29+
) {
30+
$this->objectManager = $objectManager;
31+
}
32+
33+
/**
34+
* Instantiate an object representing 'union' GraphQL config element.
35+
*
36+
* @param array $data
37+
* @return ConfigElementInterface
38+
*/
39+
public function createFromConfigData(array $data): ConfigElementInterface
40+
{
41+
return $this->create($data, $data['types'] ?? []);
42+
}
43+
44+
/**
45+
* Create union object based off array of configured GraphQL.
46+
*
47+
* Union data must contain name, type resolver, and possible concrete types definitions
48+
* The type resolver should point to an implementation of the TypeResolverInterface
49+
* that decides what concrete GraphQL type to output. Description is the only optional field.
50+
*
51+
* @param array $unionData
52+
* @param array $types
53+
* @return UnionType
54+
*/
55+
public function create(
56+
array $unionData,
57+
array $types
58+
) : UnionType {
59+
return $this->objectManager->create(
60+
UnionType::class,
61+
[
62+
'name' => $unionData['name'],
63+
'typeResolver' => $unionData['typeResolver'],
64+
'types' => $types,
65+
'description' => isset($unionData['description']) ? $unionData['description'] : ''
66+
]
67+
);
68+
}
69+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
use Magento\Framework\GraphQl\Config\ConfigElementInterface;
11+
12+
/**
13+
* Defines the contract for the union configuration data type.
14+
*/
15+
interface UnionInterface extends ConfigElementInterface
16+
{
17+
/**
18+
* Get a list of fields that make up the possible return or input values of a type.
19+
*
20+
* @return Type[]
21+
*/
22+
public function getTypes(): array;
23+
}

0 commit comments

Comments
 (0)