Skip to content
Open
Show file tree
Hide file tree
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
11 changes: 7 additions & 4 deletions src/Type/Definition/ResolveInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ public function getFieldSelectionWithAliases(int $depth = 0): array
$field = $this->parentType->getField($fieldNode->name->value);
$fieldType = $field->getType();

$fields = array_merge_recursive(
$fields = array_replace_recursive(
$fields,
$this->foldSelectionWithAlias($selectionSet, $depth, $fieldType)
);
Expand Down Expand Up @@ -470,7 +470,10 @@ private function foldSelectionWithAlias(SelectionSetNode $selectionSet, int $des
continue;
}

$aliasInfo['selectionSet'] = $this->foldSelectionWithAlias($nestedSelectionSet, $descend - 1, $fieldType);
$aliasInfo['selectionSet'] = array_replace_recursive(
$aliasInfo['selectionSet'] ?? [],
$this->foldSelectionWithAlias($nestedSelectionSet, $descend - 1, $fieldType)
);
} elseif ($selection instanceof FragmentSpreadNode) {
$spreadName = $selection->name->value;
$fragment = $this->fragments[$spreadName] ?? null;
Expand All @@ -481,7 +484,7 @@ private function foldSelectionWithAlias(SelectionSetNode $selectionSet, int $des
$fieldType = $this->schema->getType($fragment->typeCondition->name->value);
assert($fieldType instanceof Type, 'ensured by query validation');

$fields = array_merge_recursive(
$fields = array_replace_recursive(
$this->foldSelectionWithAlias($fragment->selectionSet, $descend, $fieldType),
$fields
);
Expand All @@ -500,7 +503,7 @@ private function foldSelectionWithAlias(SelectionSetNode $selectionSet, int $des
continue;
}

$fields = array_merge_recursive(
$fields = array_replace_recursive(
$this->foldSelectionWithAlias($selection->selectionSet, $descend, $fieldType),
$fields
);
Expand Down
111 changes: 111 additions & 0 deletions tests/Type/ResolveInfoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,117 @@ public function testGetFieldSelectionWithAliases(): void
self::assertEmpty($result10->errors, 'Query With Union type should have no errors');
}

/** @dataProvider providerGetFieldSelectionWithAliasesMergedSelections */
public function testGetFieldSelectionWithAliasesMergedSelections(string $document, bool $recursiveType): void
{
$post = new ObjectType([
'name' => 'Post',
'fields' => [
'id' => Type::id(),
'text' => Type::string(),
],
]);

if ($recursiveType) {
$post->config['fields'] = [
'id' => Type::id(),
'text' => Type::string(),
'related' => $post,
];
}

$user = new ObjectType([
'name' => 'User',
'fields' => [
'name' => Type::string(),
'posts' => [
'type' => Type::nonNull(Type::listOf(Type::nonNull($post))),
'args' => [
'limit' => Type::int(),
'ids' => Type::listOf(Type::int()),
],
],
],
]);

$selection = null;
$query = new ObjectType([
'name' => 'Query',
'fields' => [
'user' => [
'type' => $user,
'resolve' => static function ($value, array $args, $context, ResolveInfo $info) use (&$selection) {
$selection = $info->getFieldSelectionWithAliases(1);

return null;
},
],
],
]);

$result = GraphQL::executeQuery(new Schema(['query' => $query]), $document);

self::assertSame([], $result->errors);
self::assertIsArray($selection);
self::assertSame($post, $selection['posts']['recent']['type']);
self::assertSame(Type::string(), $selection['name']['name']['type']);
self::assertSame(['limit' => 2, 'ids' => [1, 2]], $selection['posts']['recent']['args']);
self::assertEquals([
'name' => [
'name' => ['args' => [], 'type' => Type::string()],
],
'posts' => [
'recent' => [
'args' => ['limit' => 2, 'ids' => [1, 2]],
'type' => $post,
'selectionSet' => [
'id' => [
'id' => ['args' => [], 'type' => Type::id()],
],
'text' => [
'text' => ['args' => [], 'type' => Type::string()],
],
],
],
],
], $selection);
}

/** @return \Generator<string, array{string, bool}> */
public static function providerGetFieldSelectionWithAliasesMergedSelections(): \Generator
{
$first = 'name recent: posts(limit: 2, ids: [1, 2]) { id }';
$second = 'name recent: posts(limit: 2, ids: [1, 2]) { text }';

$documents = [
'named fragment after field' => <<<GRAPHQL
{ user { {$first} ...Posts } }
fragment Posts on User { {$second} }
GRAPHQL,
'named fragment before field' => <<<GRAPHQL
{ user { ...Posts {$second} } }
fragment Posts on User { {$first} }
GRAPHQL,
'inline fragment after field' => <<<GRAPHQL
{ user { {$first} ... on User { {$second} } } }
GRAPHQL,
'inline fragment before field' => <<<GRAPHQL
{ user { ... on User { {$first} } {$second} } }
GRAPHQL,
'duplicate root field nodes' => <<<GRAPHQL
{ user { {$first} } user { {$second} } }
GRAPHQL,
'duplicate nested field nodes' => <<<GRAPHQL
{ user { {$first} {$second} } }
GRAPHQL,
];

foreach ($documents as $name => $document) {
yield $name => [$document, false];
yield "{$name} with recursive type" => [$document, true];
}
}

public function testPathAndUnaliasedPath(): void
{
$resolveInfo = new ObjectType([
Expand Down