Automattic\WooCommerce\Vendor\GraphQL\Validator\Rules

OverlappingFieldsCanBeMerged::internalCollectFieldsAndFragmentNamesprotectedWC 1.0

Given a reference to a fragment, return the represented collection of fields as well as a list of nested fragment names referenced via fragment spreads.

Method of the class: OverlappingFieldsCanBeMerged{}

No Hooks.

Returns

null. Nothing (null).

Usage

// protected - for code of main (parent) or child class
$result = $this->internalCollectFieldsAndFragmentNames( $context, ?Type $parentType, $selectionSet, $astAndDefs, $fragmentNames ): void;
$context(QueryValidationContext) (required)
.
?Type $parentType(required)
.
$selectionSet(SelectionSetNode) (required)
.
$astAndDefs(array) (required)
.
$fragmentNames(array<string, bool>) (required)
.

OverlappingFieldsCanBeMerged::internalCollectFieldsAndFragmentNames() code WC 11.0.1

protected function internalCollectFieldsAndFragmentNames(
    QueryValidationContext $context,
    ?Type $parentType,
    SelectionSetNode $selectionSet,
    array &$astAndDefs,
    array &$fragmentNames
): void {
    foreach ($selectionSet->selections as $selection) {
        switch (true) {
            case $selection instanceof FieldNode:
                $fieldName = $selection->name->value;
                $fieldDef = null;
                if (
                    ($parentType instanceof ObjectType || $parentType instanceof InterfaceType)
                    && $parentType->hasField($fieldName)
                ) {
                    $fieldDef = $parentType->getField($fieldName);
                }

                $responseName = $selection->alias->value ?? $fieldName;

                $astAndDefs[$responseName] ??= [];
                $astAndDefs[$responseName][] = [$parentType, $selection, $fieldDef];
                break;
            case $selection instanceof FragmentSpreadNode:
                $fragmentNames[$selection->name->value] = true;
                break;
            case $selection instanceof InlineFragmentNode:
                $typeCondition = $selection->typeCondition;
                $inlineFragmentType = $typeCondition === null
                    ? $parentType
                    : AST::typeFromAST([$context->getSchema(), 'getType'], $typeCondition);

                $this->internalCollectFieldsAndFragmentNames(
                    $context,
                    $inlineFragmentType,
                    $selection->selectionSet,
                    $astAndDefs,
                    $fragmentNames
                );
                break;
        }
    }
}