Skip to content

Commit

Permalink
Add method getFieldSelectionWithAliases to class ResolveInfo (#1648)
Browse files Browse the repository at this point in the history
  • Loading branch information
zephyx authored Dec 4, 2024
1 parent b7b5bfc commit a84933c
Show file tree
Hide file tree
Showing 5 changed files with 706 additions and 129 deletions.
48 changes: 25 additions & 23 deletions src/Type/Definition/QueryPlan.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,59 +164,61 @@ private function analyzeSelectionSet(SelectionSetNode $selectionSet, Type $paren
{
$fields = [];
$implementors = [];
foreach ($selectionSet->selections as $selectionNode) {
if ($selectionNode instanceof FieldNode) {
$fieldName = $selectionNode->name->value;
foreach ($selectionSet->selections as $selection) {
if ($selection instanceof FieldNode) {
$fieldName = $selection->name->value;

if ($fieldName === Introspection::TYPE_NAME_FIELD_NAME) {
continue;
}

assert($parentType instanceof HasFieldsType, 'ensured by query validation and the check above which excludes union types');
assert($parentType instanceof HasFieldsType, 'ensured by query validation');

$type = $parentType->getField($fieldName);
$selectionType = $type->getType();

$subfields = [];
$subImplementors = [];
if (isset($selectionNode->selectionSet)) {
$subfields = $this->analyzeSubFields($selectionType, $selectionNode->selectionSet, $subImplementors);
}
$nestedSelectionSet = $selection->selectionSet;
$subfields = $nestedSelectionSet === null
? []
: $this->analyzeSubFields($selectionType, $nestedSelectionSet, $subImplementors);

$fields[$fieldName] = [
'type' => $selectionType,
'fields' => $subfields,
'args' => Values::getArgumentValues($type, $selectionNode, $this->variableValues),
'args' => Values::getArgumentValues($type, $selection, $this->variableValues),
];
if ($this->groupImplementorFields && $subImplementors) {
$fields[$fieldName]['implementors'] = $subImplementors;
}
} elseif ($selectionNode instanceof FragmentSpreadNode) {
$spreadName = $selectionNode->name->value;
if (isset($this->fragments[$spreadName])) {
$fragment = $this->fragments[$spreadName];
$type = $this->schema->getType($fragment->typeCondition->name->value);
assert($type instanceof Type, 'ensured by query validation');

$subfields = $this->analyzeSubFields($type, $fragment->selectionSet);
$fields = $this->mergeFields($parentType, $type, $fields, $subfields, $implementors);
} elseif ($selection instanceof FragmentSpreadNode) {
$spreadName = $selection->name->value;
$fragment = $this->fragments[$spreadName] ?? null;
if ($fragment === null) {
continue;
}
} elseif ($selectionNode instanceof InlineFragmentNode) {
$typeCondition = $selectionNode->typeCondition;

$type = $this->schema->getType($fragment->typeCondition->name->value);
assert($type instanceof Type, 'ensured by query validation');

$subfields = $this->analyzeSubFields($type, $fragment->selectionSet);
$fields = $this->mergeFields($parentType, $type, $fields, $subfields, $implementors);
} elseif ($selection instanceof InlineFragmentNode) {
$typeCondition = $selection->typeCondition;
$type = $typeCondition === null
? $parentType
: $this->schema->getType($typeCondition->name->value);
assert($type instanceof Type, 'ensured by query validation');

$subfields = $this->analyzeSubFields($type, $selectionNode->selectionSet);
$subfields = $this->analyzeSubFields($type, $selection->selectionSet);
$fields = $this->mergeFields($parentType, $type, $fields, $subfields, $implementors);
}
}

$parentTypeName = $parentType->name();

// TODO evaluate if this line is really necessary - it causes abstract types to appear
// in getReferencedTypes() even if they do not have any fields directly referencing them.
// TODO evaluate if this line is really necessary.
// It causes abstract types to appear in getReferencedTypes() even if they do not have any fields directly referencing them.
$this->typeToFields[$parentTypeName] ??= [];
foreach ($fields as $fieldName => $_) {
$this->typeToFields[$parentTypeName][$fieldName] = true;
Expand Down
Loading

0 comments on commit a84933c

Please sign in to comment.