Automattic\WooCommerce\Api\Infrastructure
Main::register_graphql_endpoint
Register a GraphQL REST endpoint backed by a plugin-provided controller subclass.
May be called at any time up to and including the rest_api_init if called earlier, registration is deferred to that hook; if called from inside another plugin's rest_api_init registration happens immediately. Calls made after rest_api_init already completed register a REST route that WP_REST_Server won't honour on the current request — callers should avoid deferring registration past bootstrap.
When the feature flag is off or PHP is < 8.1 this is a silent no-op.
The first argument accepts either of two forms:
- A plugin root directory (recommended): pass
__DIR__from the plugin'sbootstrap file. The controller class is resolved by convention from `{dir}/src/Internal/Api/Autogenerated/GraphQLController.php` — ApiBuilder always emits the generated subclass at that path.- A controller class FQCN: use this when the plugin keeps its generated
code somewhere other than the conventional location, or registers multiple endpoints backed by different controller classes.
- A controller class FQCN: use this when the plugin keeps its generated
Plugins calling this method should guard the call with method_exists():
if ( method_exists( \Automattic\WooCommerce\Api\Infrastructure\Main::class, 'register_graphql_endpoint' ) ) {
\Automattic\WooCommerce\Api\Infrastructure\Main::register_graphql_endpoint(
__DIR__,
'my-plugin',
'/graphql'
);
}
Метод класса: Main{}
Хуков нет.
Возвращает
null. Ничего (null).
Использование
$result = Main::register_graphql_endpoint( $plugin_dir_or_controller_class, $route_namespace, $route, $methods, fooo ) ): void;
- $plugin_dir_or_controller_class(строка) (обязательный)
- Plugin root directory, OR a fully-qualified GraphQLController subclass name. See above.
- $route_namespace(строка) (обязательный)
- REST route namespace, as passed to register_rest_route().
- $route(строка) (обязательный)
- REST route path, as passed to register_rest_route().
- $methods(string[])
- HTTP methods accepted on the endpoint.
По умолчанию:GET + POST - fooo )(обязательный)
- .
Код Main::register_graphql_endpoint() Main::register graphql endpoint WC 11.0.1
public static function register_graphql_endpoint(
string $plugin_dir_or_controller_class,
string $route_namespace,
string $route,
array $methods = array( 'GET', 'POST' )
): void {
if ( ! self::is_enabled() ) {
return;
}
$controller_class_name = self::resolve_controller_class( $plugin_dir_or_controller_class );
// Validate up front so a typo surfaces here at bootstrap rather than
// deep inside the deferred rest_api_init callback.
self::assert_is_controller_subclass( $controller_class_name );
$registrar = new GraphQLEndpointRegistrar( $controller_class_name, $route_namespace, $route, $methods );
if ( did_action( 'rest_api_init' ) ) {
$registrar->handle_rest_api_init();
} else {
add_action( 'rest_api_init', array( $registrar, 'handle_rest_api_init' ) );
}
}