Skip to content

Commit

Permalink
add alias to query plan
Browse files Browse the repository at this point in the history
  • Loading branch information
crissi committed Sep 25, 2023
1 parent 012c58a commit 64b364d
Show file tree
Hide file tree
Showing 2 changed files with 209 additions and 5 deletions.
35 changes: 30 additions & 5 deletions src/Type/Definition/QueryPlan.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,9 @@ private function analyzeSelectionSet(SelectionSetNode $selectionSet, Type $paren
{
$fields = [];
$implementors = [];
$aliases = [];
foreach ($selectionSet->selections as $selectionNode) {

if ($selectionNode instanceof FieldNode) {
$fieldName = $selectionNode->name->value;

Expand All @@ -182,12 +184,35 @@ private function analyzeSelectionSet(SelectionSetNode $selectionSet, Type $paren
if (isset($selectionNode->selectionSet)) {
$subfields = $this->analyzeSubFields($selectionType, $selectionNode->selectionSet, $subImplementors);
}

if (! ($fields[$fieldName] ?? null )) {
$fields[$fieldName] = [
'type' => $selectionType,
];
}

$fields[$fieldName] = [
'type' => $selectionType,
'fields' => $subfields,
'args' => Values::getArgumentValues($type, $selectionNode, $this->variableValues),
];
if ($selectionNode->alias) {

var_dump($aliases);
$alreadyExists = array_filter($aliases, function(array $value) {
return $value === $selectionNode->alias->value;
});
$aliases[] = $selectionNode->alias->value;

if (!$alreadyExists) {
$fields[$fieldName]['aliases'][] = [
'name' => $selectionNode->alias->value,
'args' => Values::getArgumentValues($type, $selectionNode, $this->variableValues),
'fields' => $subfields,
];
}


} else {
$fields[$fieldName]['args'] = Values::getArgumentValues($type, $selectionNode, $this->variableValues);
$fields[$fieldName]['fields'] = $subfields;
}

if ($this->groupImplementorFields && $subImplementors) {
$fields[$fieldName]['implementors'] = $subImplementors;
}
Expand Down
179 changes: 179 additions & 0 deletions tests/Type/QueryPlanTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,11 @@ public function testMergedFragmentsQueryPlan(): void
'replies' => [
'type' => Type::listOf($reply),
'args' => [],
'aliases' => [
[

]
],
'fields' => [
'body' => [
'type' => Type::string(),
Expand Down Expand Up @@ -1084,4 +1089,178 @@ public function testQueryPlanForMultipleFieldNodes(): void
self::assertInstanceOf(QueryPlan::class, $queryPlan);
self::assertEquals($expectedQueryPlan, $queryPlan->queryPlan());
}

public function testQueryPlanForAliasArgs(): void
{
$image = new ObjectType([
'name' => 'Image',
'fields' => [
'url' => ['type' => Type::string()],
'width' => ['type' => Type::int()],
'height' => ['type' => Type::int()],
],
]);

$author = new ObjectType([
'name' => 'Author',
'fields' => static function () use ($image): array {
return [
'id' => ['type' => Type::string()],
'name' => ['type' => Type::string()],
'pic' => [
'type' => $image,
'args' => [
'width' => ['type' => Type::int()],
'height' => ['type' => Type::int()],
],
],
];
},
]);

$doc = '
query Test {
article {
__typename
author {
name
alias150: pic(width: 150, height: 150) {
url
width
}
pic(width: 100, height: 200) {
url
width
},
alias50: pic(width: 50, height: 50) {
url
width
}
}
}
}
';
$expectedQueryPlan = [
'author' => [
'type' => $author,
'args' => [],
'fields' => [
'name' => [
'type' => Type::string(),
'args' => [],
'fields' => [],
],
'pic' => [
'type' => $image,
'args' => [
'width' => 100,
'height' => 200,
],
'fields' => [
'url' => [
'type' => Type::string(),
'args' => [],
'fields' => [],
],
'width' => [
'type' => Type::int(),
'args' => [],
'fields' => [],
],
],
'aliases' => [
[
'name' => 'alias150',
'args' => [
'width' => 150,
'height' => 150,
],
'fields' => [
'url' => [
'type' => Type::string(),
'args' => [],
'fields' => [],
],
'width' => [
'type' => Type::int(),
'args' => [],
'fields' => [],
],
],
],
[
'name' => 'alias50',
'args' => [
'width' => 50,
'height' => 50,
],
'fields' => [
'url' => [
'type' => Type::string(),
'args' => [],
'fields' => [],
],
'width' => [
'type' => Type::int(),
'args' => [],
'fields' => [],
],
],
]

]
],
],
]
];

$article = new ObjectType([
'name' => 'Article',
'fields' => [
'author' => ['type' => $author],
],
]);

$expectedReferencedTypes = [
'Image',
'Author',
'Article',
];

$expectedReferencedFields = [
'url',
'width',
'name',
'pic',
'author',
];

/** @var QueryPlan|null $queryPlan */
$queryPlan = null;

$blogQuery = new ObjectType([
'name' => 'Query',
'fields' => [
'article' => [
'type' => $article,
'resolve' => static function ($value, array $args, $context, ResolveInfo $info) use (&$queryPlan) {
$queryPlan = $info->lookAhead();

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

$schema = new Schema(['query' => $blogQuery]);
$result = GraphQL::executeQuery($schema, $doc)->toArray();

self::assertSame(['data' => ['article' => null]], $result);
self::assertInstanceOf(QueryPlan::class, $queryPlan);
self::assertEquals($expectedQueryPlan, $queryPlan->queryPlan());
self::assertSame($expectedReferencedTypes, $queryPlan->getReferencedTypes());
self::assertSame($expectedReferencedFields, $queryPlan->getReferencedFields());

}

}

0 comments on commit 64b364d

Please sign in to comment.