Skip to content

Commit 9c04475

Browse files
authored
ENGCOM-8105: MC-36456: union implementation #29426
2 parents d1ff2ca + d3c5778 commit 9c04475

File tree

30 files changed

+580
-55
lines changed

30 files changed

+580
-55
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/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+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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 'union' GraphQL config element.
12+
*/
13+
class UnionType implements UnionInterface
14+
{
15+
/**
16+
* @var string
17+
*/
18+
private $name;
19+
20+
/**
21+
* @var string[]
22+
*/
23+
private $types;
24+
25+
/**
26+
* @var string
27+
*/
28+
private $typeResolver;
29+
30+
/**
31+
* @var string
32+
*/
33+
private $description;
34+
35+
/**
36+
* @param string $name
37+
* @param string $typeResolver
38+
* @param string[] $types
39+
* @param string $description
40+
*/
41+
public function __construct(
42+
string $name,
43+
string $typeResolver,
44+
array $types,
45+
string $description
46+
) {
47+
$this->name = $name;
48+
$this->types = $types;
49+
$this->typeResolver = $typeResolver;
50+
$this->description = $description;
51+
}
52+
53+
/**
54+
* Get the type name.
55+
*
56+
* @return string
57+
*/
58+
public function getName(): string
59+
{
60+
return $this->name;
61+
}
62+
63+
/**
64+
* Get a list of fields that make up the possible return or input values of a type.
65+
*
66+
* @return string[]
67+
*/
68+
public function getTypes(): array
69+
{
70+
return $this->types;
71+
}
72+
73+
/**
74+
* Return the name of the resolver class that determines the concrete type to display in the result.
75+
*
76+
* @return string
77+
*/
78+
public function getTypeResolver(): string
79+
{
80+
return $this->typeResolver;
81+
}
82+
83+
/**
84+
* Get a human-readable description of the type.
85+
*
86+
* @return string
87+
*/
88+
public function getDescription(): string
89+
{
90+
return $this->description;
91+
}
92+
}

lib/internal/Magento/Framework/GraphQl/Query/Resolver/Argument/SearchCriteria/ArgumentApplier/Filter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function __construct(
5151
}
5252

5353
/**
54-
* {@inheritdoc}
54+
* @inheritDoc
5555
*/
5656
public function applyArgument(
5757
SearchCriteriaInterface $searchCriteria,

0 commit comments

Comments
 (0)