Skip to content

Commit 35c9d62

Browse files
committed
MC-36456: union implementation
1 parent c531be9 commit 35c9d62

File tree

5 files changed

+139
-0
lines changed

5 files changed

+139
-0
lines changed

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: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
use Magento\Sales\Model\ResourceModel\Order\CollectionFactoryInterface;
14+
15+
/**
16+
* Resolver for Union Type 1
17+
*/
18+
class UnionType1 implements ResolverInterface
19+
{
20+
/**
21+
* @var CollectionFactoryInterface
22+
*/
23+
private $collectionFactory;
24+
25+
/**
26+
* @param CollectionFactoryInterface $collectionFactory
27+
*/
28+
public function __construct(
29+
CollectionFactoryInterface $collectionFactory
30+
) {
31+
$this->collectionFactory = $collectionFactory;
32+
}
33+
34+
/**
35+
* @inheritDoc
36+
*/
37+
public function resolve(
38+
Field $field,
39+
$context,
40+
ResolveInfo $info,
41+
array $value = null,
42+
array $args = null
43+
) {
44+
return [
45+
'custom_name1' => 'custom_name1_value',
46+
'custom_name2' => 'custom_name2_value',
47+
];
48+
}
49+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
28+
}

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: UnionType1 @resolver(class: "Magento\\TestModuleGraphQlQuery\\Model\\Resolver\\UnionType1")
67
}
78

89
type Mutation {
@@ -18,3 +19,14 @@ type MutationItem {
1819
item_id: Int
1920
name: String
2021
}
22+
23+
union UnionType1 @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: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ public function testQueryTestModuleReturnsResults()
2525
item_id
2626
name
2727
}
28+
testUnion {
29+
__typename
30+
... on TypeCustom1 {
31+
custom_name1
32+
}
33+
... on TypeCustom2 {
34+
custom_name2
35+
}
36+
}
2837
}
2938
QUERY;
3039

@@ -100,4 +109,42 @@ public function testQueryViaGetRequestWithVariablesReturnsResults()
100109

101110
$this->assertArrayHasKey('testItem', $response);
102111
}
112+
113+
public function testQueryTestUnionResults()
114+
{
115+
$id = 1;
116+
117+
$query = <<<QUERY
118+
{
119+
testItem(id: {$id})
120+
{
121+
item_id
122+
name
123+
}
124+
testUnion {
125+
__typename
126+
... on TypeCustom1 {
127+
custom_name1
128+
}
129+
... on TypeCustom2 {
130+
custom_name2
131+
}
132+
}
133+
}
134+
QUERY;
135+
136+
$response = $this->graphQlQuery($query);
137+
$this->assertArrayHasKey('testItem', $response);
138+
$testItem = $response['testItem'];
139+
$this->assertArrayHasKey('item_id', $testItem);
140+
$this->assertArrayHasKey('name', $testItem);
141+
$this->assertEquals(1, $testItem['item_id']);
142+
$this->assertEquals('itemName', $testItem['name']);
143+
144+
$this->assertArrayHasKey('testUnion', $response);
145+
$testUnion = $response['testUnion'];
146+
$this->assertArrayHasKey('custom_name1', $testUnion);
147+
$this->assertEquals('custom_name1_value', $testUnion['custom_name1']);
148+
$this->assertArrayNotHasKey('custom_name2', $testUnion);
149+
}
103150
}

0 commit comments

Comments
 (0)