-
-
Notifications
You must be signed in to change notification settings - Fork 267
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/master'
- Loading branch information
Showing
15 changed files
with
868 additions
and
22 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
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,84 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rebing\GraphQL\Support\AliasArguments; | ||
|
||
use GraphQL\Type\Definition\InputObjectField; | ||
use GraphQL\Type\Definition\InputObjectType; | ||
use GraphQL\Type\Definition\ListOfType; | ||
use GraphQL\Type\Definition\NonNull; | ||
use GraphQL\Type\Definition\Type; | ||
use GraphQL\Type\Definition\WrappingType; | ||
|
||
class AliasArguments | ||
{ | ||
private $typedArgs; | ||
private $arguments; | ||
|
||
public function get(array $typedArgs, array $arguments): array | ||
{ | ||
$pathsWithAlias = $this->getAliasesInFields($typedArgs, ''); | ||
|
||
return (new ArrayKeyChange())->modify($arguments, $pathsWithAlias); | ||
} | ||
|
||
private function getAliasesInFields(array $fields, $prefix = '', $parentType = null): array | ||
{ | ||
$pathAndAlias = []; | ||
foreach ($fields as $name => $arg) { | ||
// $arg is either an array DSL notation or an InputObjectField | ||
$arg = $arg instanceof InputObjectField ? $arg : (object) $arg; | ||
|
||
$type = $arg->type ?? null; | ||
|
||
if (null === $type) { | ||
continue; | ||
} | ||
|
||
$newPrefix = $prefix ? $prefix.'.'.$name : $name; | ||
|
||
if (isset($arg->alias)) { | ||
$pathAndAlias[$newPrefix] = $arg->alias; | ||
} | ||
|
||
if ($this->isWrappedInList($type)) { | ||
$newPrefix .= '.*'; | ||
} | ||
|
||
$type = $this->getWrappedType($type); | ||
|
||
if (! ($type instanceof InputObjectType)) { | ||
continue; | ||
} | ||
|
||
if ($parentType && $type->toString() === $parentType->toString()) { | ||
// in case the field is a self reference we must not do | ||
// a recursive call as it will never stop | ||
continue; | ||
} | ||
|
||
$pathAndAlias = $pathAndAlias + $this->getAliasesInFields($type->getFields(), $newPrefix, $type); | ||
} | ||
|
||
return $pathAndAlias; | ||
} | ||
|
||
private function isWrappedInList(Type $type): bool | ||
{ | ||
if ($type instanceof NonNull) { | ||
$type = $type->getWrappedType(); | ||
} | ||
|
||
return $type instanceof ListOfType; | ||
} | ||
|
||
private function getWrappedType(Type $type): Type | ||
{ | ||
if ($type instanceof WrappingType) { | ||
$type = $type->getWrappedType(true); | ||
} | ||
|
||
return $type; | ||
} | ||
} |
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,62 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rebing\GraphQL\Support\AliasArguments; | ||
|
||
class ArrayKeyChange | ||
{ | ||
public function modify(array $array, array $pathKeyMappings): array | ||
{ | ||
$pathKeyMappings = $this->orderPaths($pathKeyMappings); | ||
|
||
foreach ($pathKeyMappings as $path => $replaceKey) { | ||
$array = $this->changeKey($array, explode('.', $path), $replaceKey); | ||
} | ||
|
||
return $array; | ||
} | ||
|
||
/** | ||
* @return array<string, string> | ||
*/ | ||
private function orderPaths(array $paths): array | ||
{ | ||
uksort($paths, function (string $a, string $b): int { | ||
return $this->pathLevels($b) <=> $this->pathLevels($a); | ||
}); | ||
|
||
return $paths; | ||
} | ||
|
||
private function pathLevels(string $path): int | ||
{ | ||
return substr_count($path, '.'); | ||
} | ||
|
||
private function changeKey(array $target, array $segments, string $replaceKey): array | ||
{ | ||
$segment = array_shift($segments); | ||
|
||
if (empty($segments)) { | ||
if (isset($target[$segment])) { | ||
$target[$replaceKey] = $target[$segment]; | ||
unset($target[$segment]); | ||
} | ||
|
||
return $target; | ||
} | ||
|
||
if ('*' === $segment) { | ||
foreach ($target as $index => $inner) { | ||
$target[$index] = $this->changeKey($inner, $segments, $replaceKey); | ||
} | ||
|
||
return $target; | ||
} | ||
|
||
$target[$segment] = $this->changeKey($target[$segment], $segments, $replaceKey); | ||
|
||
return $target; | ||
} | ||
} |
Oops, something went wrong.