Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ResolveInfo::getFieldSelectionWithAliases() => now add instance types and the folded union types to the returned schema. #1681

Merged
merged 3 commits into from
Mar 21, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 89 additions & 39 deletions src/Type/Definition/ResolveInfo.php
Original file line number Diff line number Diff line change
@@ -221,8 +221,10 @@ public function getFieldSelection(int $depth = 0): array
*
* The result maps original field names to a map of selections for that field, including aliases.
* For each of those selections, you can find the following keys:
* - "args" contains the passed arguments for this field/alias
* - "selectionSet" contains potential nested fields of this field/alias. The structure is recursive from here.
* - "args" contains the passed arguments for this field/alias (not on an union inline fragment)
* - "type" contains the related Type instance found (will be the same for all aliases of a field)
* - "selectionSet" contains potential nested fields of this field/alias (only on ObjectType). The structure is recursive from here.
* - "unions" contains potential object types contained in an UnionType (only on UnionType). The structure is recursive from here and will go through the selectionSet of the object types.
*
* Example:
* {
@@ -235,71 +237,101 @@ public function getFieldSelection(int $depth = 0): array
* alias1: nested {
* nested1(myArg: 2, mySecondAg: "test")
* }
* myUnion(myArg: 3) {
* ...on Nested {
* nested1(myArg: 4)
* }
* ...on MyCustomObject {
* nested3
* }
* }
* }
* }
*
* Given this ResolveInfo instance is a part of "root" field resolution, and $depth === 1,
* Given this ResolveInfo instance is a part of root field resolution,
* $depth === 1,
* and fields "nested" represents an ObjectType named "Nested",
* this method will return:
* [
* 'id' => [
* 'id' => [
* 'args' => [],
* 'type' => GraphQL\Type\Definition\IntType Object ( ... )),
* ],
* ],
* 'nested' => [
* 'nested' => [
* 'args' => [],
* 'type' => GraphQL\Type\Definition\ObjectType Object ( ... )),
* 'selectionSet' => [
* 'nested1' => [
* 'nested1' => [
* 'args' => [
* 'myArg' => 1,
* ],
* 'type' => GraphQL\Type\Definition\StringType Object ( ... )),
* ],
* 'nested1Bis' => [
* 'args' => [],
* 'type' => GraphQL\Type\Definition\StringType Object ( ... )),
* ],
* ],
* ],
* ],
* 'alias1' => [
* ],
* ],
* 'alias1' => [
* 'alias1' => [
* 'args' => [],
* 'type' => GraphQL\Type\Definition\ObjectType Object ( ... )),
* 'selectionSet' => [
* 'nested1' => [
* 'nested1' => [
* 'args' => [
* 'myArg' => 2,
* 'mySecondAg' => "test,
* ],
* 'nested1' => [
* 'nested1' => [
* 'args' => [
* 'myArg' => 2,
* 'mySecondAg' => "test",
* ],
* 'type' => GraphQL\Type\Definition\StringType Object ( ... )),
* ],
* ],
* ],
* ],
* ],
* ],
* ],
* 'myUnion' => [
* 'myUnion' => [
* 'args' => [
* 'myArg' => 3,
* ],
* 'type' => GraphQL\Type\Definition\UnionType Object ( ... )),
* 'unions' => [
* 'Nested' => [
* 'type' => GraphQL\Type\Definition\ObjectType Object ( ... )),
* 'selectionSet' => [
* 'nested1' => [
* 'nested1' => [
* 'args' => [
* 'myArg' => 4,
* ],
* 'type' => GraphQL\Type\Definition\StringType Object ( ... )),
* ],
* ],
* ],
* ],
* 'MyCustomObject' => [
* 'type' => GraphQL\Tests\Type\TestClasses\MyCustomType Object ( ... )),
* 'selectionSet' => [
* 'nested3' => [
* 'nested3' => [
* 'args' => [],
* 'type' => GraphQL\Type\Definition\StringType Object ( ... )),
* ],
* ],
* ],
* ],
* ],
* ],
* ],
* ]
*
* This method does not consider conditional typed fragments.
* Use it with care for fields of interface and union types.
* You can still alias the union type fields with the same name in order to extract their corresponding args.
*
* Example:
* {
* root {
* id
* unionPerson {
* ...on Child {
* name
* birthdate(format: "d/m/Y")
* }
* ...on Adult {
* adultName: name
* adultBirthDate: birthdate(format: "Y-m-d")
* job
* }
* }
* }
* }
*
* @param int $depth How many levels to include in the output beyond the first
*
* @throws \Exception
@@ -416,6 +448,13 @@ private function foldSelectionWithAlias(SelectionSetNode $selectionSet, int $des
$fieldType = $fieldDef->getType();
$fields[$fieldName][$aliasName]['args'] = Values::getArgumentValues($fieldDef, $selection, $this->variableValues);

$innerType = $fieldType;
if ($innerType instanceof WrappingType) {
$innerType = $innerType->getInnermostType();
}

$fields[$fieldName][$aliasName]['type'] = $innerType;

if ($descend <= 0) {
continue;
}
@@ -425,6 +464,11 @@ private function foldSelectionWithAlias(SelectionSetNode $selectionSet, int $des
continue;
}

if (is_a($innerType, UnionType::class)) {
$fields[$fieldName][$aliasName]['unions'] = $this->foldSelectionWithAlias($nestedSelectionSet, $descend, $fieldType);
continue;
}

$fields[$fieldName][$aliasName]['selectionSet'] = $this->foldSelectionWithAlias($nestedSelectionSet, $descend - 1, $fieldType);
} elseif ($selection instanceof FragmentSpreadNode) {
$spreadName = $selection->name->value;
@@ -447,10 +491,16 @@ private function foldSelectionWithAlias(SelectionSetNode $selectionSet, int $des
: $this->schema->getType($typeCondition->name->value);
assert($fieldType instanceof Type, 'ensured by query validation');

$fields = array_merge_recursive(
$this->foldSelectionWithAlias($selection->selectionSet, $descend, $fieldType),
$fields
);
if (is_a($parentType, UnionType::class)) {
assert($fieldType instanceof NamedType, 'ensured by query validation');
$fields[$fieldType->name()]['type'] = $fieldType;
$fields[$fieldType->name()]['selectionSet'] = $this->foldSelectionWithAlias($selection->selectionSet, $descend, $fieldType);
} else {
$fields = array_merge_recursive(
$this->foldSelectionWithAlias($selection->selectionSet, $descend, $fieldType),
$fields
);
}
}
}

177 changes: 174 additions & 3 deletions tests/Type/ResolveInfoTest.php
Original file line number Diff line number Diff line change
@@ -2,11 +2,18 @@

namespace GraphQL\Tests\Type;

use GraphQL\Error\Error;
use GraphQL\GraphQL;
use GraphQL\Tests\Type\TestClasses\CustomWithObject;
use GraphQL\Tests\Type\TestClasses\MyCustomType;
use GraphQL\Tests\Type\TestClasses\OtherCustom;
use GraphQL\Type\Definition\IntType;
use GraphQL\Type\Definition\ListOfType;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\StringType;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Definition\UnionType;
use GraphQL\Type\Schema;
use PHPUnit\Framework\TestCase;

@@ -456,102 +463,128 @@ public function testGetFieldSelectionWithAliases(): void
++$aliasArgsNbTests;
switch ($args['testName']) {
case 'NoAlias':
$level2Type = $aliasArgs['level2']['level2']['type'] ?? null;
self::assertInstanceOf(IntType::class, $level2Type);
self::assertSame([
'level2' => [
'level2' => [
'args' => [
'width' => 1,
'height' => 1,
],
'type' => $level2Type,
],
],
], $aliasArgs);
break;
case 'NoAliasFirst':
$level2Type = $aliasArgs['level2']['level2']['type'] ?? null;
self::assertInstanceOf(IntType::class, $level2Type);
self::assertSame([
'level2' => [
'level2' => [
'args' => [
'width' => 1,
'height' => 1,
],
'type' => $level2Type,
],
'level1000' => [
'args' => [
'width' => 2,
'height' => 20,
],
'type' => $level2Type,
],
],
], $aliasArgs);
break;
case 'NoAliasLast':
$level2Type = $aliasArgs['level2']['level2000']['type'] ?? null;
self::assertInstanceOf(IntType::class, $level2Type);
self::assertSame([
'level2' => [
'level2000' => [
'args' => [
'width' => 1,
'height' => 1,
],
'type' => $level2Type,
],
'level2' => [
'args' => [
'width' => 2,
'height' => 20,
],
'type' => $level2Type,
],
],
], $aliasArgs);
break;
case 'AllAliases':
$level2Type = $aliasArgs['level2']['level1000']['type'] ?? null;
self::assertInstanceOf(IntType::class, $level2Type);
self::assertSame([
'level2' => [
'level1000' => [
'args' => [
'width' => 1,
'height' => 1,
],
'type' => $level2Type,
],
'level2000' => [
'args' => [
'width' => 2,
'height' => 20,
],
'type' => $level2Type,
],
],
], $aliasArgs);
break;
case 'MultiLvlSameAliasName':
case 'WithFragments':
$level2Type = $aliasArgs['level2']['level3000']['type'] ?? null;
$level2BisType = $aliasArgs['level2bis']['level2bis']['type'] ?? null;
$level3Type = $aliasArgs['level2bis']['level2bis']['selectionSet']['level3']['level3000']['type'] ?? null;
self::assertInstanceOf(IntType::class, $level2Type);
self::assertInstanceOf(ObjectType::class, $level2BisType);
self::assertInstanceOf(IntType::class, $level3Type);
self::assertSame([
'level2' => [
'level3000' => [
'args' => [
'width' => 1,
'height' => 1,
],
'type' => $level2Type,
],
'level2' => [
'args' => [
'width' => 3,
'height' => 30,
],
'type' => $level2Type,
],
],
'level2bis' => [
'level2bis' => [
'args' => [],
'type' => $level2BisType,
'selectionSet' => [
'level3' => [
'level3000' => [
'args' => [
'length' => 2,
],
'type' => $level3Type,
],
'level3' => [
'args' => [
'length' => 10,
],
'type' => $level3Type,
],
],
],
@@ -565,29 +598,50 @@ public function testGetFieldSelectionWithAliases(): void
case 'Deepest':
$depth ??= 5;
$aliasArgs = $info->getFieldSelectionWithAliases($depth);

$level2BisType = $aliasArgs['level2bis']['level2Alias']['type'] ?? null;
$level3DeeperType = $aliasArgs['level2bis']['level2Alias']['selectionSet']['level3deeper']['level3deeper']['type'] ?? null;
$level4evenmoreType = $aliasArgs['level2bis']['level2Alias']['selectionSet']['level3deeper']['level3deeper']['selectionSet']['level4evenmore']['level4evenmore']['type'] ?? null;
$level5Type = $aliasArgs['level2bis']['level2Alias']['selectionSet']['level3deeper']['level3deeper']['selectionSet']['level4evenmore']['level4evenmore']['selectionSet']['level5']['level5']['type'] ?? null;
$level4Type = $aliasArgs['level2bis']['level2Alias']['selectionSet']['level3deeper']['level3deeper']['selectionSet']['level4']['level4']['type'] ?? null;

self::assertInstanceOf(ObjectType::class, $level2BisType);
// Don't test the deepest types because we don't retrieve them with a low $depth
if ($depth > 1) {
self::assertInstanceOf(ObjectType::class, $level3DeeperType);
self::assertInstanceOf(ObjectType::class, $level4evenmoreType);
self::assertInstanceOf(StringType::class, $level5Type);
self::assertInstanceOf(IntType::class, $level4Type);
}

self::assertSame([
'level2bis' => [
'level2Alias' => [
'args' => [],
'type' => $level2BisType,
'selectionSet' => [
'level3deeper' => [
'level3deeper' => [
'args' => [],
'type' => $level3DeeperType,
'selectionSet' => [
'level4evenmore' => [
'level4evenmore' => [
'args' => [],
'type' => $level4evenmoreType,
'selectionSet' => [
'level5' => [
'level5' => [
'args' => [
'crazyness' => 0.124,
],
'type' => $level5Type,
],
'lastAlias' => [
'args' => [
'crazyness' => 0.758,
],
'type' => $level5Type,
],
],
],
@@ -598,6 +652,75 @@ public function testGetFieldSelectionWithAliases(): void
'args' => [
'temperature' => -20,
],
'type' => $level4Type,
],
],
],
],
],
],
],
],
], $aliasArgs);
break;
case 'WithUnion':
$levelUnionType = $aliasArgs['levelUnion']['levelUnion']['type'] ?? null;
$levelMyCustomType = $aliasArgs['levelUnion']['levelUnion']['unions']['MyCustom']['type'] ?? null;
$levelAType = $aliasArgs['levelUnion']['levelUnion']['unions']['MyCustom']['selectionSet']['a']['a']['type'] ?? null;
$levelCustomWithObjectType = $aliasArgs['levelUnion']['levelUnion']['unions']['CustomWithObject']['type'] ?? null;
$levelOtherCustomType = $aliasArgs['levelUnion']['levelUnion']['unions']['CustomWithObject']['selectionSet']['customB']['customB']['type'] ?? null;
$levelBType = $aliasArgs['levelUnion']['levelUnion']['unions']['CustomWithObject']['selectionSet']['customB']['customB']['selectionSet']['b']['b']['type'] ?? null;
self::assertInstanceOf(UnionType::class, $levelUnionType);
self::assertInstanceOf(MyCustomType::class, $levelMyCustomType);
self::assertInstanceOf(StringType::class, $levelAType);
self::assertInstanceOf(CustomWithObject::class, $levelCustomWithObjectType);
self::assertInstanceOf(OtherCustom::class, $levelOtherCustomType);
self::assertInstanceOf(StringType::class, $levelBType);
self::assertSame([
'levelUnion' => [
'levelUnion' => [
'args' => [],
'type' => $levelUnionType,
'unions' => [
'MyCustom' => [
'type' => $levelMyCustomType,
'selectionSet' => [
'a' => [
'a' => [
'args' => [],
'type' => $levelAType,
],
],
],
],
'CustomWithObject' => [
'type' => $levelCustomWithObjectType,
'selectionSet' => [
'customA' => [
'customA' => [
'args' => [],
'type' => $levelMyCustomType,
'selectionSet' => [
'a' => [
'a' => [
'args' => [],
'type' => $levelAType,
],
],
],
],
],
'customB' => [
'customB' => [
'args' => [],
'type' => $levelOtherCustomType,
'selectionSet' => [
'b' => [
'b' => [
'args' => [],
'type' => $levelBType,
],
],
],
],
],
@@ -613,6 +736,26 @@ public function testGetFieldSelectionWithAliases(): void
}
};

$myCustomWithObjectType = new CustomWithObject();
// retrieve the instance from the parent type in order to don't instantiate twice the same type in the same schema
$fields = $myCustomWithObjectType->config['fields'];
assert(is_array($fields), 'ensured by type config');
$myCustomType = $fields['customA'];
$levelUnion = new UnionType([
'name' => 'CustomOrOther',
'types' => [
$myCustomType,
$myCustomWithObjectType,
],
'resolveType' => function ($value) use ($myCustomType, $myCustomWithObjectType): ObjectType {
switch (get_class($value)) {
case MyCustomType::class: return $myCustomType;
case CustomWithObject::class: return $myCustomWithObjectType;
default: throw new Error('Unexpected union type');
}
},
]);

$level4EvenMore = new ObjectType([
'name' => 'Level4EvenMore',
'fields' => [
@@ -685,6 +828,10 @@ public function testGetFieldSelectionWithAliases(): void
'type' => $level2Bis,
'resolve' => fn (): bool => true,
],
'levelUnion' => [
'type' => $levelUnion,
'resolve' => fn (): bool => true,
],
],
]);

@@ -797,7 +944,7 @@ public function testGetFieldSelectionWithAliases(): void
}
}
}
fragment level3Frag on Level2bis {
level3000: level3(length: 2)
level3(length: 10)
@@ -827,7 +974,7 @@ public function testGetFieldSelectionWithAliases(): void
$result8 = GraphQL::executeQuery(
new Schema(['query' => $query]),
<<<GRAPHQL
query {
{
level1(testName: "Deepest") {
level2Alias: level2bis {
level3deeper {
@@ -846,7 +993,7 @@ public function testGetFieldSelectionWithAliases(): void
$result9 = GraphQL::executeQuery(
new Schema(['query' => $queryList]),
<<<GRAPHQL
query {
{
level1(testName: "NoAliasFirst") {
level2(width: 1, height: 1)
level1000: level2(width: 2, height: 20)
@@ -855,6 +1002,29 @@ public function testGetFieldSelectionWithAliases(): void
GRAPHQL
);

$result10 = GraphQL::executeQuery(
new Schema(['query' => $query]),
<<<GRAPHQL
{
level1(testName: "WithUnion") {
levelUnion {
...on MyCustom {
a
}
...on CustomWithObject {
customA {
a
}
customB {
b
}
}
}
}
}
GRAPHQL
);

self::assertEmpty($result1->errors, 'Query NoAlias should have no errors');
self::assertEmpty($result2->errors, 'Query NoAliasFirst should have no errors');
self::assertEmpty($result3->errors, 'Query NoAliasLast should have no errors');
@@ -864,6 +1034,7 @@ public function testGetFieldSelectionWithAliases(): void
self::assertSame('Failed asserting that two arrays are identical.', $result7->errors[0]->getMessage(), 'Query DeepestTooLowDepth should have failed');
self::assertEmpty($result8->errors, 'Query Deepest should have no errors');
self::assertEmpty($result9->errors, 'Query With ListOf type should have no errors');
self::assertEmpty($result10->errors, 'Query With Union type should have no errors');
}

public function testPathAndUnaliasedPath(): void
18 changes: 18 additions & 0 deletions tests/Type/TestClasses/CustomWithObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php declare(strict_types=1);

namespace GraphQL\Tests\Type\TestClasses;

use GraphQL\Type\Definition\ObjectType;

final class CustomWithObject extends ObjectType
{
public function __construct()
{
parent::__construct([
'fields' => [
'customA' => new MyCustomType(),
'customB' => new OtherCustom(),
],
]);
}
}