Automattic\WooCommerce\Api\Infrastructure

QueryInfoExtractor::extractpublic staticWC 1.0

Recursively extract query info from a selection set.

Method of the class: QueryInfoExtractor{}

No Hooks.

Returns

array. The query info tree for the selection set.

Usage

$result = QueryInfoExtractor::extract( ?SelectionSetNode $selection_set, $variable_values, $fragments ): array;
?SelectionSetNode $selection_set(required)
.
$variable_values(array) (required)
Variable values for resolving arguments.
$fragments(array<string, FragmentDefinitionNode>)
Named fragment definitions from the document.
Default: array()

QueryInfoExtractor::extract() code WC 11.0.1

public static function extract( ?SelectionSetNode $selection_set, array $variable_values, array $fragments = array() ): array {
	if ( null === $selection_set ) {
		return array();
	}

	$result = array();

	foreach ( $selection_set->selections as $selection ) {
		if ( $selection instanceof FieldNode ) {
			$field_name            = $selection->name->value;
			$result[ $field_name ] = self::build_field_entry( $selection, $variable_values, $fragments );
		} elseif ( $selection instanceof InlineFragmentNode ) {
			$type_name      = $selection->typeCondition->name->value;
			$key            = '...' . $type_name;
			$result[ $key ] = self::extract( $selection->selectionSet, $variable_values, $fragments );
		} elseif ( $selection instanceof FragmentSpreadNode ) {
			// Expand named fragment spreads inline: their fields become
			// siblings of the other selections, matching how GraphQL
			// evaluates them. Consumers of _query_info (mappers that
			// check array_key_exists for specific fields) see them the
			// same as if the fragment had been written inline. Use a
			// recursive merge so overlapping selections are unioned
			// rather than replaced — `array_merge` would drop the
			// existing sub-selection under the same field name.
			$fragment = $fragments[ $selection->name->value ] ?? null;
			if ( null === $fragment ) {
				continue;
			}
			$spread = self::extract( $fragment->selectionSet, $variable_values, $fragments );
			$result = self::merge_selections( $result, $spread );
		}
	}

	return $result;
}