Skip to content

Commit cfcbcd8

Browse files
committed
WIP - Add proof of bug for union types
The newly supported union is the cause of this issue in the parseTypes function. The regex is finding the type of union CompanyStructureEntity but its also grabbing the item below it and including it as part of its schema content, which means CompanyStructureEntity contains the schema for itself as well as CompanyStructureItem , this means I never have a $knownTypes['CompanyStructureItem'] to work with which means it is a missing dependency. ``` <?php use Magento\Framework\App\Bootstrap; require __DIR__ . '/app/bootstrap.php'; ​ $bootstrap = Bootstrap::create(BP, $_SERVER); $obj = $bootstrap->getObjectManager(); /** @var Magento\Framework\GraphQlSchemaStitching\GraphQlReader $graphqlReader */ $graphqlReader = $obj->get(Magento\Framework\GraphQlSchemaStitching\GraphQlReader::class); $reflector = new ReflectionObject($graphqlReader); $method = $reflector->getMethod('parseTypes'); $method->setAccessible(true); ​ $broken = 'type IsCompanyEmailAvailableOutput @doc(description: "Contains the response of a company email validation query") { is_email_available: Boolean! @doc(description: "A value of `true` indicates the email address can be used to create a company") } ​ union CompanyStructureEntity @typeResolver(class: "Magento\\CompanyGraphQl\\Model\\Resolver\\StructureEntityTypeResolver") = CompanyTeam | Customer ​ type CompanyStructureItem @doc(description: "Defines an individual node in the company structure") { id: ID! @doc(description: "The unique ID for a `CompanyStructureItem` object") parent_id: ID @doc(description: "The ID of the parent item in the company hierarchy") entity: CompanyStructureEntity @doc(description: "A union of `CompanyTeam` and `Customer` objects") } ​ type CompanyStructure @doc(description: "Contains an array of the individual nodes that comprise the company structure") { items: [CompanyStructureItem] @doc(description: "An array of elements in a company structure") }'; ​ $result = $method->invoke($graphqlReader, $broken); ​ echo "Got the following types" . PHP_EOL; echo implode(PHP_EOL, array_keys($result)) . PHP_EOL; ​ echo "The values for the union actually contains CompanyStructureItem which is why the initial approach works any mine does not" . PHP_EOL; echo $result['CompanyStructureEntity'] . PHP_EOL; ```
1 parent 15bad09 commit cfcbcd8

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,26 @@ public function read($scope = null) : array
9696
foreach ($schemaFiles as $partialSchemaContent) {
9797
$partialSchemaTypes = $this->parseTypes($partialSchemaContent);
9898

99+
/**
100+
* TODO fix this
101+
* There is a bug in parseTypes where the union type is also containing the information for the type below
102+
* in this case that meant that we were missing the type directly below CompanyStructureEntity
103+
*
104+
* This means that we cannot find CompanyStructureItem later in getTypesToUse
105+
*
106+
* Manually split them out in a proof of concept hack, while we review the regex
107+
*/
108+
if (isset($partialSchemaTypes['CompanyStructureEntity'])) {
109+
if (strpos($partialSchemaTypes['CompanyStructureEntity'], 'type CompanyStructureItem') !== false) {
110+
$lines = explode(PHP_EOL . PHP_EOL, $partialSchemaTypes['CompanyStructureEntity']);
111+
if (isset($lines[0], $lines[1]) && count($lines) === 2) {
112+
$partialSchemaTypes['CompanyStructureEntity'] = $lines[0];
113+
$partialSchemaTypes['CompanyStructureItem'] = $lines[1];
114+
}
115+
unset($lines);
116+
}
117+
}
118+
99119
// Filter out duplicated ones and save them into a list to be retried
100120
$tmpTypes = $knownTypes;
101121
foreach ($partialSchemaTypes as $intendedKey => $partialSchemaType) {

0 commit comments

Comments
 (0)