-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENGCOM-8105: MC-36456: union implementation #29426
- Loading branch information
Showing
30 changed files
with
580 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
dev/tests/api-functional/_files/Magento/TestModuleGraphQlQuery/Model/Resolver/TestUnion.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\TestModuleGraphQlQuery\Model\Resolver; | ||
|
||
use Magento\Framework\GraphQl\Config\Element\Field; | ||
use Magento\Framework\GraphQl\Query\ResolverInterface; | ||
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; | ||
|
||
/** | ||
* Resolver for Union type TestUnion | ||
*/ | ||
class TestUnion implements ResolverInterface | ||
{ | ||
/** | ||
* @inheritDoc | ||
*/ | ||
public function resolve( | ||
Field $field, | ||
$context, | ||
ResolveInfo $info, | ||
array $value = null, | ||
array $args = null | ||
) { | ||
return [ | ||
'custom_name1' => 'custom_name1_value', | ||
'custom_name2' => 'custom_name2_value', | ||
]; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...api-functional/_files/Magento/TestModuleGraphQlQuery/Model/Resolver/UnionTypeResolver.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\TestModuleGraphQlQuery\Model\Resolver; | ||
|
||
use Magento\Framework\GraphQl\Query\Resolver\TypeResolverInterface; | ||
|
||
/** | ||
* Type Resolver for union | ||
*/ | ||
class UnionTypeResolver implements TypeResolverInterface | ||
{ | ||
/** | ||
* @inheritDoc | ||
*/ | ||
public function resolveType(array $data): string | ||
{ | ||
if (!empty($data)) { | ||
return 'TypeCustom1'; | ||
} | ||
return ''; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
lib/internal/Magento/Framework/GraphQl/Config/Element/UnionFactory.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Framework\GraphQl\Config\Element; | ||
|
||
use Magento\Framework\GraphQl\Config\ConfigElementFactoryInterface; | ||
use Magento\Framework\GraphQl\Config\ConfigElementInterface; | ||
use Magento\Framework\ObjectManagerInterface; | ||
|
||
/** | ||
* Factory for config elements of 'union' type. | ||
*/ | ||
class UnionFactory implements ConfigElementFactoryInterface | ||
{ | ||
/** | ||
* @var ObjectManagerInterface | ||
*/ | ||
private $objectManager; | ||
|
||
/** | ||
* @param ObjectManagerInterface $objectManager | ||
*/ | ||
public function __construct( | ||
ObjectManagerInterface $objectManager | ||
) { | ||
$this->objectManager = $objectManager; | ||
} | ||
|
||
/** | ||
* Instantiate an object representing 'union' GraphQL config element. | ||
* | ||
* @param array $data | ||
* @return ConfigElementInterface | ||
*/ | ||
public function createFromConfigData(array $data): ConfigElementInterface | ||
{ | ||
return $this->create($data, $data['types'] ?? []); | ||
} | ||
|
||
/** | ||
* Create union object based off array of configured GraphQL. | ||
* | ||
* Union data must contain name, type resolver, and possible concrete types definitions | ||
* The type resolver should point to an implementation of the TypeResolverInterface | ||
* that decides what concrete GraphQL type to output. Description is the only optional field. | ||
* | ||
* @param array $unionData | ||
* @param array $types | ||
* @return UnionType | ||
*/ | ||
public function create( | ||
array $unionData, | ||
array $types | ||
) : UnionType { | ||
return $this->objectManager->create( | ||
UnionType::class, | ||
[ | ||
'name' => $unionData['name'], | ||
'typeResolver' => $unionData['typeResolver'], | ||
'types' => $types, | ||
'description' => isset($unionData['description']) ? $unionData['description'] : '' | ||
] | ||
); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
lib/internal/Magento/Framework/GraphQl/Config/Element/UnionInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Framework\GraphQl\Config\Element; | ||
|
||
use Magento\Framework\GraphQl\Config\ConfigElementInterface; | ||
|
||
/** | ||
* Defines the contract for the union configuration data type. | ||
*/ | ||
interface UnionInterface extends ConfigElementInterface | ||
{ | ||
/** | ||
* Get a list of fields that make up the possible return or input values of a type. | ||
* | ||
* @return Type[] | ||
*/ | ||
public function getTypes(): array; | ||
} |
92 changes: 92 additions & 0 deletions
92
lib/internal/Magento/Framework/GraphQl/Config/Element/UnionType.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Framework\GraphQl\Config\Element; | ||
|
||
/** | ||
* Class representing 'union' GraphQL config element. | ||
*/ | ||
class UnionType implements UnionInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $name; | ||
|
||
/** | ||
* @var string[] | ||
*/ | ||
private $types; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $typeResolver; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $description; | ||
|
||
/** | ||
* @param string $name | ||
* @param string $typeResolver | ||
* @param string[] $types | ||
* @param string $description | ||
*/ | ||
public function __construct( | ||
string $name, | ||
string $typeResolver, | ||
array $types, | ||
string $description | ||
) { | ||
$this->name = $name; | ||
$this->types = $types; | ||
$this->typeResolver = $typeResolver; | ||
$this->description = $description; | ||
} | ||
|
||
/** | ||
* Get the type name. | ||
* | ||
* @return string | ||
*/ | ||
public function getName(): string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
/** | ||
* Get a list of fields that make up the possible return or input values of a type. | ||
* | ||
* @return string[] | ||
*/ | ||
public function getTypes(): array | ||
{ | ||
return $this->types; | ||
} | ||
|
||
/** | ||
* Return the name of the resolver class that determines the concrete type to display in the result. | ||
* | ||
* @return string | ||
*/ | ||
public function getTypeResolver(): string | ||
{ | ||
return $this->typeResolver; | ||
} | ||
|
||
/** | ||
* Get a human-readable description of the type. | ||
* | ||
* @return string | ||
*/ | ||
public function getDescription(): string | ||
{ | ||
return $this->description; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.