Skip to content

Commit a5ee110

Browse files
committed
MC-36456: union implementation
1 parent 5f1ff4b commit a5ee110

File tree

6 files changed

+24
-46
lines changed

6 files changed

+24
-46
lines changed

dev/tests/api-functional/_files/Magento/TestModuleGraphQlQuery/Model/Resolver/UnionType1.php renamed to dev/tests/api-functional/_files/Magento/TestModuleGraphQlQuery/Model/Resolver/TestUnion.php

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,12 @@
1010
use Magento\Framework\GraphQl\Config\Element\Field;
1111
use Magento\Framework\GraphQl\Query\ResolverInterface;
1212
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
13-
use Magento\Sales\Model\ResourceModel\Order\CollectionFactoryInterface;
1413

1514
/**
16-
* Resolver for Union Type 1
15+
* Resolver for Union type TestUnion
1716
*/
18-
class UnionType1 implements ResolverInterface
17+
class TestUnion implements ResolverInterface
1918
{
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-
3419
/**
3520
* @inheritDoc
3621
*/

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +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")
6+
testUnion: TestUnion @resolver(class: "Magento\\TestModuleGraphQlQuery\\Model\\Resolver\\TestUnion")
77
}
88

99
type Mutation {
@@ -20,7 +20,7 @@ type MutationItem {
2020
name: String
2121
}
2222

23-
union UnionType1 @doc(description: "some kind of union") @typeResolver(class: "Magento\\TestModuleGraphQlQuery\\Model\\Resolver\\UnionTypeResolver") =
23+
union TestUnion @doc(description: "some kind of union") @typeResolver(class: "Magento\\TestModuleGraphQlQuery\\Model\\Resolver\\UnionTypeResolver") =
2424
TypeCustom1 | TypeCustom2
2525

2626
type TypeCustom1 {

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

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,6 @@ public function testQueryTestUnionResults()
107107

108108
$query = <<<QUERY
109109
{
110-
testItem(id: {$id})
111-
{
112-
item_id
113-
name
114-
}
115110
testUnion {
116111
__typename
117112
... on TypeCustom1 {
@@ -125,12 +120,6 @@ public function testQueryTestUnionResults()
125120
QUERY;
126121

127122
$response = $this->graphQlQuery($query);
128-
$this->assertArrayHasKey('testItem', $response);
129-
$testItem = $response['testItem'];
130-
$this->assertArrayHasKey('item_id', $testItem);
131-
$this->assertArrayHasKey('name', $testItem);
132-
$this->assertEquals(1, $testItem['item_id']);
133-
$this->assertEquals('itemName', $testItem['name']);
134123

135124
$this->assertArrayHasKey('testUnion', $response);
136125
$testUnion = $response['testUnion'];

lib/internal/Magento/Framework/GraphQl/Schema/Type/Output/ElementMapper/Formatter/Fields.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,18 @@ public function __construct(
9292
*/
9393
public function format(ConfigElementInterface $configElement, OutputTypeInterface $outputType): array
9494
{
95-
$typeConfig = [
96-
'fields' => function () use ($configElement, $outputType) {
97-
$fieldsConfig = [];
98-
foreach ($configElement->getFields() as $field) {
99-
$fieldsConfig[$field->getName()] = $this->getFieldConfig($configElement, $outputType, $field);
95+
$typeConfig = [];
96+
if ($configElement instanceof TypeInterface) {
97+
$typeConfig = [
98+
'fields' => function () use ($configElement, $outputType) {
99+
$fieldsConfig = [];
100+
foreach ($configElement->getFields() as $field) {
101+
$fieldsConfig[$field->getName()] = $this->getFieldConfig($configElement, $outputType, $field);
102+
}
103+
return $fieldsConfig;
100104
}
101-
return $fieldsConfig;
102-
}
103-
];
105+
];
106+
}
104107
return $typeConfig;
105108
}
106109

lib/internal/Magento/Framework/GraphQl/Schema/Type/Output/ElementMapper/FormatterComposite.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,6 @@ public function format(ConfigElementInterface $configElement, OutputTypeInterfac
4242
$formattedConfig[] = $formatter->format($configElement, $outputType);
4343
}
4444

45-
return array_merge($defaultConfig, ...$formattedConfig);
45+
return array_merge($defaultConfig, ...$formattedConfig);
4646
}
4747
}

lib/internal/Magento/Framework/GraphQlSchemaStitching/GraphQlReader.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -330,13 +330,14 @@ private static function getModuleNameForRelevantFile(string $file): string
330330
*/
331331
private function addModuleNameToTypes(array $source, string $filePath): array
332332
{
333-
foreach ($source as $typeName => $type) {
334-
if ((!isset($type['module']))
335-
&& (($type['type'] ?? '' === InterfaceType::GRAPHQL_INTERFACE && isset($type['typeResolver']))
336-
|| isset($type['implements'])
337-
)
338-
) {
339-
$source[$typeName]['module'] = self::getModuleNameForRelevantFile($filePath);
333+
foreach ($source as $typeName => $typeDefinition) {
334+
if (!isset($typeDefinition['module'])) {
335+
$hasTypeResolver = (bool)$typeDefinition['typeResolver'] ?? false;
336+
$hasImplements = (bool)$typeDefinition['implements'] ?? false;
337+
$typeDefinition = (bool)$typeDefinition['type'] ?? false;
338+
if ((($typeDefinition === InterfaceType::GRAPHQL_INTERFACE && $hasTypeResolver) || $hasImplements)) {
339+
$source[$typeName]['module'] = self::getModuleNameForRelevantFile($filePath);
340+
}
340341
}
341342
}
342343

0 commit comments

Comments
 (0)