Automattic\WooCommerce\Api\Infrastructure

MetadataController::resolvepublic staticWC 1.0

Resolver for the _apiMetadata root field. Signature matches the engine's resolver contract; $root is unused here (root operations have no parent). $context is read for the principal so the can_query_metadata ladder can run.

Method of the class: MetadataController{}

No Hooks.

Returns

list. mixed>>

Usage

$result = MetadataController::resolve( ?array $root, $args, $context, $info ): array;
?array $root(required)
.
$args(array) (required)
GraphQL arguments (name, type, field, attribute).
$context(mixed) (required)
Per-request context — an ArrayObject wrapping {principal, _query_metadata}.
$info(ResolveInfo) (required)
Carries the schema instance to walk.

MetadataController::resolve() code WC 10.9.1

public static function resolve( ?array $root, array $args, mixed $context, ResolveInfo $info ): array {
	unset( $root );

	$principal = is_object( $context ) || is_array( $context ) ? ( $context['principal'] ?? null ) : null;
	if ( ! self::can_query_metadata( $principal ) ) {
		// phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- Static error message + machine code; serialized as JSON, not HTML.
		throw self::build_metadata_query_authorization_error( $principal );
	}

	// Wrap the resolver's engine-typed schema into the same handle clients
	// receive from `GraphQLControllerBase::get_schema()`, so the resolver and
	// PHP-side callers share a single inspection surface.
	$schema = new SchemaHandle( $info->schema );

	$rows = $schema->find_metadata(
		$args['name'] ?? null,
		$args['type'] ?? null,
		$args['field'] ?? null,
		$args['attribute'] ?? null,
	);

	// SchemaHandle returns entries as an associative `name => value` map,
	// which is the natural shape for filtering and PHP-side consumers. The
	// GraphQL `MetadataEntry` type instead exposes each entry as a
	// `{ name, value }` object so clients can `entries { name value }` over
	// a list. Reshape here.
	return array_map(
		static function ( array $row ): array {
			$row['entries'] = array_map(
				static fn( string $entry_name, $entry_value ): array => array(
					'name'  => $entry_name,
					'value' => $entry_value,
				),
				array_keys( $row['entries'] ),
				array_values( $row['entries'] ),
			);
			return $row;
		},
		$rows
	);
}