Skip to content

Commit 19ba821

Browse files
committed
MC-36456: union implementation
1 parent 24b3dfe commit 19ba821

File tree

12 files changed

+33
-28
lines changed

12 files changed

+33
-28
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function __construct(
3131
}
3232

3333
/**
34-
* Instantiate an object representing 'interface' GraphQL config element.
34+
* Instantiate an object representing 'union' GraphQL config element.
3535
*
3636
* @param array $data
3737
* @return ConfigElementInterface
@@ -42,11 +42,11 @@ public function createFromConfigData(array $data): ConfigElementInterface
4242
}
4343

4444
/**
45-
* Create interface object based off array of configured GraphQL Output/InputInterface.
45+
* Create union object based off array of configured GraphQL.
4646
*
47-
* Interface data must contain name, type resolver, and field definitions. The type resolver should point to an
48-
* implementation of the TypeResolverInterface that decides what concrete GraphQL type to output. Description is
49-
* the only optional field.
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.
5050
*
5151
* @param array $unionData
5252
* @param array $types

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use Magento\Framework\GraphQl\Config\ConfigElementInterface;
1111

1212
/**
13-
* Defines contracts for return type data as GraphQL objects.
13+
* Defines the contract for the union configuration data type.
1414
*/
1515
interface UnionInterface extends ConfigElementInterface
1616
{

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,7 @@ public function __construct(ObjectManagerInterface $objectManager)
3838
public function format(ConfigElementInterface $configElement, OutputTypeInterface $outputType) : array
3939
{
4040
$config = [];
41-
if ($configElement instanceof InterfaceType) {
42-
$typeResolver = $this->objectManager->create($configElement->getTypeResolver());
43-
$config['resolveType'] = function ($value) use ($typeResolver) {
44-
return $typeResolver->resolveType($value);
45-
};
46-
} elseif ($configElement instanceof UnionType) {
41+
if ($configElement instanceof InterfaceType || $configElement instanceof UnionType) {
4742
$typeResolver = $this->objectManager->create($configElement->getTypeResolver());
4843
$config['resolveType'] = function ($value) use ($typeResolver) {
4944
return $typeResolver->resolveType($value);

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,15 @@ public function __construct(array $formatters)
3333
*/
3434
public function format(ConfigElementInterface $configElement, OutputTypeInterface $outputType) : array
3535
{
36-
$config = [
36+
$defaultConfig = [
3737
'name' => $configElement->getName(),
3838
'description' => $configElement->getDescription()
3939
];
40+
$formattedConfig = [];
4041
foreach ($this->formatters as $formatter) {
41-
$config = array_merge($config, $formatter->format($configElement, $outputType));
42+
$formattedConfig[] = $formatter->format($configElement, $outputType);
4243
}
4344

44-
return $config;
45+
return array_merge($defaultConfig,...$formattedConfig);
4546
}
4647
}

lib/internal/Magento/Framework/GraphQl/Schema/Type/Output/OutputMapper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use Magento\Framework\Phrase;
1414

1515
/**
16-
* Map type names to their output type/interface/enum classes.
16+
* Map type names to their output type/interface/union/enum classes.
1717
*/
1818
class OutputMapper
1919
{

lib/internal/Magento/Framework/GraphQl/Schema/Type/Output/OutputUnionObject.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use Magento\Framework\GraphQl\Schema\Type\UnionType;
1212

1313
/**
14-
* 'union' type compatible with GraphQL schema generator.
14+
* The 'union' type compatible with GraphQL schema generator.
1515
*/
1616
class OutputUnionObject extends UnionType
1717
{

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\Framework\Config\FileResolverInterface;
1212
use Magento\Framework\Config\ReaderInterface;
1313
use Magento\Framework\GraphQlSchemaStitching\GraphQlReader\TypeMetaReaderInterface as TypeReaderComposite;
14+
use Magento\Framework\GraphQlSchemaStitching\GraphQlReader\Reader\InterfaceType;
1415

1516
/**
1617
* Reads *.graphqls files from modules and combines the results as array to be used with a library to configure objects
@@ -21,10 +22,9 @@ class GraphQlReader implements ReaderInterface
2122

2223
public const GRAPHQL_SCHEMA_FILE = 'schema.graphqls';
2324

25+
/** @deprecated */
2426
public const GRAPHQL_INTERFACE = 'graphql_interface';
2527

26-
public const GRAPHQL_UNION = 'graphql_union';
27-
2828
/**
2929
* File locator
3030
*
@@ -180,7 +180,7 @@ private function parseTypes(string $graphQlSchemaContent) : array
180180
private function copyInterfaceFieldsToConcreteTypes(array $source): array
181181
{
182182
foreach ($source as $interface) {
183-
if ($interface['type'] ?? '' == 'graphql_interface') {
183+
if ($interface['type'] ?? '' == InterfaceType::GRAPHQL_INTERFACE) {
184184
foreach ($source as $typeName => $type) {
185185
if (isset($type['implements'])
186186
&& isset($type['implements'][$interface['name']])
@@ -332,7 +332,7 @@ private function addModuleNameToTypes(array $source, string $filePath): array
332332
{
333333
foreach ($source as $typeName => $type) {
334334
if ((!isset($type['module']))
335-
&& (($type['type'] ?? '' === self::GRAPHQL_INTERFACE && isset($type['typeResolver']))
335+
&& (($type['type'] ?? '' === InterfaceType::GRAPHQL_INTERFACE && isset($type['typeResolver']))
336336
|| isset($type['implements'])
337337
)
338338
) {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
class EnumType implements TypeMetaReaderInterface
1717
{
18+
public const GRAPHQL_ENUM = 'graphql_enum';
19+
1820
/**
1921
* @var DocReader
2022
*/
@@ -37,7 +39,7 @@ public function read(\GraphQL\Type\Definition\Type $typeMeta) : array
3739
if ($typeMeta instanceof \GraphQL\Type\Definition\EnumType) {
3840
$result = [
3941
'name' => $typeMeta->name,
40-
'type' => 'graphql_enum',
42+
'type' => self::GRAPHQL_ENUM,
4143
'items' => [] // Populated later
4244
];
4345
foreach ($typeMeta->getValues() as $enumValueMeta) {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
*/
1818
class InputObjectType implements TypeMetaReaderInterface
1919
{
20+
public const GRAPHQL_INPUT = 'graphql_input';
21+
2022
/**
2123
* @var TypeMetaWrapperReader
2224
*/
@@ -57,7 +59,7 @@ public function read(\GraphQL\Type\Definition\Type $typeMeta) : array
5759
$typeName = $typeMeta->name;
5860
$result = [
5961
'name' => $typeName,
60-
'type' => 'graphql_input',
62+
'type' => self::GRAPHQL_INPUT,
6163
'fields' => [] // Populated later
6264
];
6365
$fields = $typeMeta->getFields();

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
*/
1818
class InterfaceType implements TypeMetaReaderInterface
1919
{
20+
public const GRAPHQL_INTERFACE = 'graphql_interface';
21+
2022
/**
2123
* @var FieldMetaReader
2224
*/
@@ -57,7 +59,7 @@ public function read(\GraphQL\Type\Definition\Type $typeMeta) : array
5759
$typeName = $typeMeta->name;
5860
$result = [
5961
'name' => $typeName,
60-
'type' => 'graphql_interface',
62+
'type' => self::GRAPHQL_INTERFACE,
6163
'fields' => []
6264
];
6365

0 commit comments

Comments
 (0)