Automattic\WooCommerce\Api\Infrastructure

QueryInfoExtractor::merge_selectionsprivate staticWC 1.0

Recursively merge two selection trees produced by extract()/build_field_entry().

Used wherever selections from different sources are combined under the same key (notably: named fragment spreads expanded inline). Matches GraphQL's selection-set merge semantics — overlapping fields have their sub-selections unioned rather than one replacing the other, which a shallow array_merge would do.

Rules:

  • Key only in one side: kept verbatim.
  • Both sides arrays: recurse, unioning children.
  • One array, one true (leaf): keep the array — it carries the sub-selection detail, and its presence already implies the field was requested.
  • Both true: keep true.
  • __args collisions (same field with different argument values): the second operand wins. Conflicting field args are a GraphQL validation error upstream of us, so this path is defensive.

Method of the class: QueryInfoExtractor{}

No Hooks.

Returns

array. The merged tree.

Usage

$result = QueryInfoExtractor::merge_selections( $a, $b ): array;
$a(array) (required)
First selection tree.
$b(array) (required)
Second selection tree, merged into $a.

QueryInfoExtractor::merge_selections() code WC 11.0.1

private static function merge_selections( array $a, array $b ): array {
	foreach ( $b as $key => $value ) {
		if ( ! array_key_exists( $key, $a ) ) {
			$a[ $key ] = $value;
			continue;
		}
		$existing = $a[ $key ];
		if ( is_array( $existing ) && is_array( $value ) ) {
			$a[ $key ] = self::merge_selections( $existing, $value );
		} elseif ( is_array( $value ) ) {
			// One side is `true`, the other is a sub-selection array — keep the array.
			$a[ $key ] = $value;
		}
		// Both true, or existing-array + new-true: keep existing.
	}
	return $a;
}