Automattic\WooCommerce\Internal\Api\Autogenerated\GraphQLQueries

ListProducts{}WC 1.0

No Hooks.

Usage

$ListProducts = new ListProducts();
// use class methods

Methods

  1. public static compute_preauthorized( \Automattic\WooCommerce\Api\Infrastructure\Principal $principal )
  2. public __( 'Return the last N results. Must be between 0 and 100.', 'woocommerce' )
  3. public static get_field_definition()
  4. public static resolve( mixed $root, array $args, mixed $context, ResolveInfo $info )

ListProducts{} code WC 10.9.1

class ListProducts {
	public static function get_field_definition(): array {
		return array(
			'type'          => Type::nonNull( ProductConnectionType::get() ),
			'description'   => __( 'List products with cursor-based pagination and optional filtering.', 'woocommerce' ),
			'authorization' => array(
				array(
					'attribute' => 'RequiredCapability',
					'args'      => array(
						0 => 'manage_woocommerce',
					),
				),
				array(
					'attribute' => 'RequiredCapability',
					'args'      => array(
						0 => 'edit_products',
					),
				),
			),
			'args'          => array(
				'first'        => array(
					'type'         => Type::int(),
					'description'  => __( 'Return the first N results. Must be between 0 and 100.', 'woocommerce' ),
					'defaultValue' => null,
				),
				'last'         => array(
					'type'         => Type::int(),
					'description'  => __( 'Return the last N results. Must be between 0 and 100.', 'woocommerce' ),
					'defaultValue' => null,
				),
				'after'        => array(
					'type'         => Type::string(),
					'description'  => __( 'Return results after this cursor.', 'woocommerce' ),
					'defaultValue' => null,
				),
				'before'       => array(
					'type'         => Type::string(),
					'description'  => __( 'Return results before this cursor.', 'woocommerce' ),
					'defaultValue' => null,
				),
				'status'       => array(
					'type'         => ProductStatusType::get(),
					'description'  => __( 'Filter by product status.', 'woocommerce' ),
					'defaultValue' => null,
				),
				'stock_status' => array(
					'type'         => StockStatusType::get(),
					'description'  => __( 'Filter by stock status.', 'woocommerce' ),
					'defaultValue' => null,
				),
				'search'       => array(
					'type'         => Type::string(),
					'description'  => __( 'Search products by keyword.', 'woocommerce' ),
					'defaultValue' => null,
				),
				'product_type' => array(
					'type'         => ProductTypeType::get(),
					'description'  => __( 'Filter by product type.', 'woocommerce' ),
					'defaultValue' => null,
				),
			),
			'complexity'    => ResolverHelpers::complexity_from_pagination( ... ),
			'resolve'       => array( self::class, 'resolve' ),
		);
	}

	public static function resolve( mixed $root, array $args, mixed $context, ResolveInfo $info ): mixed {
		// Standalone authorization gate: no authorize() method on the command,
		// so the autodiscovered authorization attributes are the sole guard.
		if ( ! self::compute_preauthorized( $context['principal'] ) ) {
			throw ResolverHelpers::build_authorization_error( $context['principal'] );
		}

		// Publish the root query's metadata so downstream field-level
		// authorization gates can read it via `$_metadata['query']`.
		// $context is an ArrayObject (see GraphQLController::process_request())
		// so the mutation propagates to nested resolvers.
		$context['_query_metadata'] = array();

		$command = \Automattic\WooCommerce\Api\Infrastructure\ClassResolver::resolve_class( ListProductsCommand::class );

		$query_info                 = QueryInfoExtractor::extract_from_info( $info, $args );
		$execute_args               = array();
		$execute_args['pagination'] = ResolverHelpers::create_pagination_params( $args );
		$execute_args['filters']    = ResolverHelpers::create_input(
			fn() => new \Automattic\WooCommerce\Api\InputTypes\Products\ProductFilterInput(
				status: $args['status'],
				stock_status: $args['stock_status'],
				search: $args['search'] ?? null,
			)
		);
		if ( array_key_exists( 'product_type', $args ) ) {
			$execute_args['product_type'] = $args['product_type'];
		}
		$execute_args['_query_info'] = $query_info;

		$result = ResolverHelpers::execute_command( $command, $execute_args );

		return $result;
	}

	/**
	 * Compute the value `_preauthorized` would carry for a given principal —
	 * the AND of the autodiscovered authorization attributes' authorize()
	 * outcomes on this command. Single source of truth for both the resolver's
	 * own gates and external (code-API) callers asking about authorization
	 * without going through GraphQL execution.
	 *
	 * Returns true vacuously when the command has no authorization attributes
	 * (in that case authorize() on the command is the sole guard, and that
	 * method should be consulted instead).
	 */
	public static function compute_preauthorized( \Automattic\WooCommerce\Api\Infrastructure\Principal $principal ): bool {
		return ( new \Automattic\WooCommerce\Api\Attributes\RequiredCapability( 'manage_woocommerce' ) )->authorize( $principal ) && ( new \Automattic\WooCommerce\Api\Attributes\RequiredCapability( 'edit_products' ) )->authorize( $principal );
	}
}