Automattic\WooCommerce\Vendor\GraphQL\Type\Definition

ResolveInfo::foldSelectionWithAliasprivateWC 1.0

Method of the class: ResolveInfo{}

No Hooks.

Returns

Array.

Usage

// private - for code of main (parent) class only
$result = $this->foldSelectionWithAlias( $selectionSet, $descend, $parentType ): array;
$selectionSet(SelectionSetNode) (required)
.
$descend(int) (required)
.
$parentType(Type) (required)
.

ResolveInfo::foldSelectionWithAlias() code WC 10.9.1

private function foldSelectionWithAlias(SelectionSetNode $selectionSet, int $descend, Type $parentType): array
{
    /** @var array<string, bool> $fields */
    $fields = [];

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

    foreach ($selectionSet->selections as $selection) {
        if ($selection instanceof FieldNode) {
            $fieldName = $selection->name->value;
            $aliasName = $selection->alias->value ?? $fieldName;

            if ($fieldName === Introspection::TYPE_NAME_FIELD_NAME) {
                continue;
            }
            assert($parentType instanceof HasFieldsType, 'ensured by query validation');

            $aliasInfo = &$fields[$fieldName][$aliasName];

            $fieldDef = $parentType->getField($fieldName);

            $aliasInfo['args'] = Values::getArgumentValues($fieldDef, $selection, $this->variableValues);

            $fieldType = $fieldDef->getType();

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

            $aliasInfo['type'] = $namedFieldType;

            if ($descend <= 0) {
                continue;
            }

            $nestedSelectionSet = $selection->selectionSet;
            if ($nestedSelectionSet === null) {
                continue;
            }

            if ($namedFieldType instanceof UnionType) {
                $aliasInfo['unions'] = $this->foldSelectionWithAlias($nestedSelectionSet, $descend, $fieldType);
                continue;
            }

            $aliasInfo['selectionSet'] = $this->foldSelectionWithAlias($nestedSelectionSet, $descend - 1, $fieldType);
        } elseif ($selection instanceof FragmentSpreadNode) {
            $spreadName = $selection->name->value;
            $fragment = $this->fragments[$spreadName] ?? null;
            if ($fragment === null) {
                continue;
            }

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

            $fields = array_merge_recursive(
                $this->foldSelectionWithAlias($fragment->selectionSet, $descend, $fieldType),
                $fields
            );
        } elseif ($selection instanceof InlineFragmentNode) {
            $typeCondition = $selection->typeCondition;
            $fieldType = $typeCondition === null
                ? $parentType
                : $this->schema->getType($typeCondition->name->value);
            assert($fieldType instanceof Type, 'ensured by query validation');

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

            $fields = array_merge_recursive(
                $this->foldSelectionWithAlias($selection->selectionSet, $descend, $fieldType),
                $fields
            );
        }
    }

    return $fields;
}