Automattic\WooCommerce\Vendor\GraphQL\Type\Definition

QueryPlan::analyzeSelectionSetprivateWC 1.0

Method of the class: QueryPlan{}

No Hooks.

Returns

Array.

Usage

// private - for code of main (parent) class only
$result = $this->analyzeSelectionSet( $selectionSet, $parentType, $implementors ): array;
$selectionSet(SelectionSetNode) (required)
.
$parentType(Type&NamedType) (required)
.
$implementors(array) (required)
.

QueryPlan::analyzeSelectionSet() code WC 10.9.1

private function analyzeSelectionSet(SelectionSetNode $selectionSet, Type $parentType, array &$implementors): array
{
    $fields = [];
    $implementors = [];
    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');

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

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

            $fields[$fieldName] = [
                'type' => $selectionType,
                'fields' => $subfields,
                'args' => Values::getArgumentValues($type, $selection, $this->variableValues),
            ];
            if ($this->groupImplementorFields && $subImplementors !== []) {
                $fields[$fieldName]['implementors'] = $subImplementors;
            }
        } elseif ($selection instanceof FragmentSpreadNode) {
            $spreadName = $selection->name->value;
            $fragment = $this->fragments[$spreadName] ?? null;
            if ($fragment === null) {
                continue;
            }

            $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, $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.
    $this->typeToFields[$parentTypeName] ??= [];
    foreach ($fields as $fieldName => $_) {
        $this->typeToFields[$parentTypeName][$fieldName] = true;
    }

    return $fields;
}