Skip to content

Commit

Permalink
Remove the aliases level
Browse files Browse the repository at this point in the history
  • Loading branch information
zephyx committed Dec 4, 2024
1 parent e6ac780 commit 2e34239
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 134 deletions.
64 changes: 28 additions & 36 deletions src/Type/Definition/ResolveInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public function getFieldSelection(int $depth = 0): array
/**
* Returns names of all fields selected in query for `$this->fieldName` up to `$depth` levels.
*
* For each field there is a key "aliases" that lists all aliases of this field,
* For each field we create an array containing all aliases of this field,
* or the original field name if a field is not aliased.
*
* For each of those aliases, you can find the following keys:
Expand All @@ -244,48 +244,40 @@ public function getFieldSelection(int $depth = 0): array
* this method will return:
* [
* 'id' => [
* 'aliases' => [
* 'id' => [
* 'args' => []
* ]
* 'id' => [
* 'args' => []
* ]
* ],
* 'nested' => [
* 'aliases' => [
* 'nested' => [
* 'args' => [],
* 'fields' => [
* 'nested' => [
* 'args' => [],
* 'fields' => [
* 'nested1' => [
* 'nested1' => [
* 'aliases' => [
* 'nested1' => [
* 'args' => [
* 'myArg' => 1
* ]
* ],
* 'nested1Bis' => [
* 'args' => []
* ]
* ]
* ]
* 'args' => [
* 'myArg' => 1
* ]
* ],
* 'nested1Bis' => [
* 'args' => []
* ]
* ]
* ],
* 'alias1' => [
* 'args' => [],
* 'fields' => [
* ]
* ],
* 'alias1' => [
* 'args' => [],
* 'fields' => [
* 'nested1' => [
* 'nested1' => [
* 'aliases' => [
* 'nested1' => [
* 'args' => [
* 'myArg' => 2,
* 'mySecondAg' => "test"
* ]
* ]
* ]
* 'args' => [
* 'myArg' => 2,
* 'mySecondAg' => "test"
* ]
* ]
* ]
* ]
* ]
* ]
* ]
* ]
* ]
*
* Warning: this method it is a naive implementation which does not take into account
Expand Down Expand Up @@ -419,10 +411,10 @@ private function foldSelectionWithAlias(SelectionSetNode $selectionSet, int $des
if ($fieldType instanceof WrappingType) {
$fieldType = $fieldType->getInnermostType();
}
$fields[$fieldName]['aliases'][$aliasName]['args'] = Values::getArgumentValues($fieldDef, $selectionNode, $this->variableValues);
$fields[$fieldName][$aliasName]['args'] = Values::getArgumentValues($fieldDef, $selectionNode, $this->variableValues);

if ($descend > 0 && $selectionNode->selectionSet !== null) {
$fields[$fieldName]['aliases'][$aliasName]['fields'] = $this->foldSelectionWithAlias($selectionNode->selectionSet, $descend - 1, $fieldType);
$fields[$fieldName][$aliasName]['fields'] = $this->foldSelectionWithAlias($selectionNode->selectionSet, $descend - 1, $fieldType);
}
} elseif ($selectionNode instanceof FragmentSpreadNode) {
$spreadName = $selectionNode->name->value;
Expand Down
172 changes: 74 additions & 98 deletions tests/Type/ResolveInfoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -660,12 +660,10 @@ public function testFieldSelectionWithAlias(): void
case 'NoAlias':
self::assertSame([
'level2' => [
'aliases' => [
'level2' => [
'args' => [
'width' => 1,
'height' => 1,
],
'level2' => [
'args' => [
'width' => 1,
'height' => 1,
],
],
],
Expand All @@ -674,18 +672,16 @@ public function testFieldSelectionWithAlias(): void
case 'NoAliasFirst':
self::assertSame([
'level2' => [
'aliases' => [
'level2' => [
'args' => [
'width' => 1,
'height' => 1,
],
'level2' => [
'args' => [
'width' => 1,
'height' => 1,
],
'level1000' => [
'args' => [
'width' => 2,
'height' => 20,
],
],
'level1000' => [
'args' => [
'width' => 2,
'height' => 20,
],
],
],
Expand All @@ -694,18 +690,16 @@ public function testFieldSelectionWithAlias(): void
case 'NoAliasLast':
self::assertSame([
'level2' => [
'aliases' => [
'level2000' => [
'args' => [
'width' => 1,
'height' => 1,
],
'level2000' => [
'args' => [
'width' => 1,
'height' => 1,
],
'level2' => [
'args' => [
'width' => 2,
'height' => 20,
],
],
'level2' => [
'args' => [
'width' => 2,
'height' => 20,
],
],
],
Expand All @@ -714,18 +708,16 @@ public function testFieldSelectionWithAlias(): void
case 'AllAliases':
self::assertSame([
'level2' => [
'aliases' => [
'level1000' => [
'args' => [
'width' => 1,
'height' => 1,
],
'level1000' => [
'args' => [
'width' => 1,
'height' => 1,
],
'level2000' => [
'args' => [
'width' => 2,
'height' => 20,
],
],
'level2000' => [
'args' => [
'width' => 2,
'height' => 20,
],
],
],
Expand All @@ -735,38 +727,32 @@ public function testFieldSelectionWithAlias(): void
case 'WithFragments':
self::assertSame([
'level2' => [
'aliases' => [
'level3000' => [
'args' => [
'width' => 1,
'height' => 1,
],
'level3000' => [
'args' => [
'width' => 1,
'height' => 1,
],
'level2' => [
'args' => [
'width' => 3,
'height' => 30,
],
],
'level2' => [
'args' => [
'width' => 3,
'height' => 30,
],
],
],
'level2bis' => [
'aliases' => [
'level2bis' => [
'args' => [],
'fields' => [
'level2bis' => [
'args' => [],
'fields' => [
'level3' => [
'level3000' => [
'args' => [
'length' => 2,
],
],
'level3' => [
'aliases' => [
'level3000' => [
'args' => [
'length' => 2,
],
],
'level3' => [
'args' => [
'length' => 10,
],
],
'args' => [
'length' => 10,
],
],
],
Expand All @@ -783,49 +769,39 @@ public function testFieldSelectionWithAlias(): void
$aliasArgs = $info->getFieldSelectionWithAlias($depth);
self::assertSame([
'level2bis' => [
'aliases' => [
'level2Alias' => [
'args' => [],
'fields' => [
'level2Alias' => [
'args' => [],
'fields' => [
'level3deeper' => [
'level3deeper' => [
'aliases' => [
'level3deeper' => [
'args' => [],
'fields' => [
'level4evenmore' => [
'aliases' => [
'level4evenmore' => [
'args' => [],
'fields' => [
'level5' => [
'aliases' => [
'level5' => [
'args' => [
'crazyness' => 0.124,
],
],
'lastAlias' => [
'args' => [
'crazyness' => 0.758,
],
],
],
],
'args' => [],
'fields' => [
'level4evenmore' => [
'level4evenmore' => [
'args' => [],
'fields' => [
'level5' => [
'level5' => [
'args' => [
'crazyness' => 0.124,
],
],
],
],
'level4' => [
'aliases' => [
'level4' => [
'lastAlias' => [
'args' => [
'temperature' => -20,
'crazyness' => 0.758,
],
],
],
],
],
],
'level4' => [
'level4' => [
'args' => [
'temperature' => -20,
],
],
],
],
],
],
Expand Down

0 comments on commit 2e34239

Please sign in to comment.