Automattic\WooCommerce\Internal\Api\Autogenerated\GraphQLQueries

GetProduct{}WC 1.0

No Hooks.

Usage

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

Methods

  1. public resolve_class( GetProductCommand::class )
  2. public static compute_preauthorized( \Automattic\WooCommerce\Api\Infrastructure\Principal $principal )
  3. public static get_field_definition()
  4. public static resolve( mixed $root, array $args, mixed $context, ResolveInfo $info )
  5. ERROR: no method name found on line ``

GetProduct{} code WC 10.9.1

class GetProduct {
	public static function get_field_definition(): array {
		return array(
			'type'          => ProductInterface::get(),
			'description'   => __( 'Retrieve a single product by ID.', 'woocommerce' ),
			'authorization' => array(
				array(
					'attribute' => 'RequiredCapability',
					'args'      => array(
						0 => 'read_product',
					),
				),
			),
			'args'          => array(
				'id' => array(
					'type'        => Type::nonNull( Type::int() ),
					'description' => __( 'The ID of the product to retrieve.', 'woocommerce' ),
				),
			),
			'resolve'       => array( self::class, 'resolve' ),
		);
	}

	public static function resolve( mixed $root, array $args, mixed $context, ResolveInfo $info ): mixed {
		// 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( GetProductCommand::class );

		$query_info   = QueryInfoExtractor::extract_from_info( $info, $args );
		$execute_args = array();
		if ( array_key_exists( 'id', $args ) ) {
			$execute_args['id'] = $args['id'];
		}
		$execute_args['_query_info'] = $query_info;

		if ( ! ResolverHelpers::authorize_command(
			$command,
			array(
				'id'             => $execute_args['id'],
				'_preauthorized' => self::compute_preauthorized( $context['principal'] ),
			)
		) ) {
			throw ResolverHelpers::build_authorization_error( $context['principal'] );
		}

		$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( 'read_product' ) )->authorize( $principal );
	}
}