WooCommerce Code Reference

Main
in package

Entry point for the WooCommerce GraphQL API.

This class is intentionally free of PHP 8.0+ syntax so that it can be loaded and called on PHP 7.4 without parse errors. The PHP-8.1-only classes (GraphQLControllerBase, QueryCache, etc.) are resolved lazily from the DI container only after is_enabled() confirms PHP 8.1+ is available.

Table of Contents

OPTION_APQ_ENABLED  = 'woocommerce_graphql_apq_enabled'
Option name for the "Enable APQ caching" setting.
OPTION_ENDPOINT_URL  = 'woocommerce_graphql_endpoint_url'
Option name for the "Endpoint URL" setting.
OPTION_GET_ENDPOINT_ENABLED  = 'woocommerce_graphql_get_endpoint_enabled'
Option name for the "Enable GET endpoint" setting.
OPTION_MAX_QUERY_COMPLEXITY  = 'woocommerce_graphql_max_query_complexity'
Option name for the "Maximum query complexity" setting.
OPTION_MAX_QUERY_DEPTH  = 'woocommerce_graphql_max_query_depth'
Option name for the "Maximum query depth" setting.
OPTION_OBJECT_CACHE_ENABLED  = 'woocommerce_graphql_object_cache_enabled'
Option name for the "ObjectCache-based caching" setting.
OPTION_OPCACHE_ENABLED  = 'woocommerce_graphql_opcache_enabled'
Option name for the "OPcache-based caching" setting.
OPTION_QUERY_CACHE_TTL  = 'woocommerce_graphql_query_cache_ttl'
Option name for the "Parsed query cache TTL" setting.
FEATURE_SLUG  = 'dual_code_graphql_api'
Feature flag slug registered in FeaturesController.
filter_methods_against_settings()  : array<string|int, string>
Apply the GraphQL-scoped site settings to a caller-declared list of HTTP methods.
instantiate_graphql_controller()  : GraphQLControllerBase|null
Instantiate a GraphQL controller subclass and wire up its dependencies.
is_apq_enabled()  : bool
Whether the Apollo Automatic Persisted Queries (APQ) protocol is enabled.
is_enabled()  : bool
Check whether the Dual Code & GraphQL API feature is active.
is_get_endpoint_enabled()  : bool
Whether the GraphQL endpoint accepts GET requests.
is_object_cache_enabled()  : bool
Whether the ObjectCache-backed query cache is enabled.
is_opcache_enabled()  : bool
Whether the OPcache-backed query cache is enabled.
register()  : void
Register the GraphQL endpoint when the feature is active.
register_graphql_endpoint()  : void
Register a GraphQL REST endpoint backed by a plugin-provided controller subclass.
assert_is_controller_subclass()  : void
Assert that a class name resolves to a concrete GraphQLControllerBase subclass.
extract_namespace_from_php_source()  : string|null
Extract an unbracketed `namespace …;` declaration from a PHP source fragment.
resolve_controller_class()  : string
Resolve the first argument of {@see self::register_graphql_endpoint()} to a controller class name. If the argument is an existing directory, treat it as the plugin root and read the namespace from the generated controller file at the conventional path. Otherwise return the argument unchanged so it's used as a class FQCN.

Constants

OPTION_APQ_ENABLED

Option name for the "Enable APQ caching" setting.

public mixed OPTION_APQ_ENABLED = 'woocommerce_graphql_apq_enabled'

When disabled, the persistedQuery extension is ignored and requests are treated as standard queries.

OPTION_ENDPOINT_URL

Option name for the "Endpoint URL" setting.

public mixed OPTION_ENDPOINT_URL = 'woocommerce_graphql_endpoint_url'

Path (relative to /wp-json/) at which the GraphQL route is registered.

OPTION_GET_ENDPOINT_ENABLED

Option name for the "Enable GET endpoint" setting.

public mixed OPTION_GET_ENDPOINT_ENABLED = 'woocommerce_graphql_get_endpoint_enabled'

When disabled, the GraphQL route only accepts POST requests.

OPTION_MAX_QUERY_COMPLEXITY

Option name for the "Maximum query complexity" setting.

public mixed OPTION_MAX_QUERY_COMPLEXITY = 'woocommerce_graphql_max_query_complexity'

Caps the computed complexity score of a GraphQL query — connection fields multiply their children's cost by the requested page size.

OPTION_MAX_QUERY_DEPTH

Option name for the "Maximum query depth" setting.

public mixed OPTION_MAX_QUERY_DEPTH = 'woocommerce_graphql_max_query_depth'

Caps how deep the selection tree of a GraphQL query may nest.

OPTION_OBJECT_CACHE_ENABLED

Option name for the "ObjectCache-based caching" setting.

public mixed OPTION_OBJECT_CACHE_ENABLED = 'woocommerce_graphql_object_cache_enabled'

OPTION_OPCACHE_ENABLED

Option name for the "OPcache-based caching" setting.

public mixed OPTION_OPCACHE_ENABLED = 'woocommerce_graphql_opcache_enabled'

When enabled, parsed query ASTs are written to disk as PHP files so that OPcache serves them from shared memory on subsequent requests.

OPTION_QUERY_CACHE_TTL

Option name for the "Parsed query cache TTL" setting.

public mixed OPTION_QUERY_CACHE_TTL = 'woocommerce_graphql_query_cache_ttl'

Time-to-live (in seconds) applied to parsed-query AST entries written to the WP object cache by both the standard-query and APQ paths.

FEATURE_SLUG

Feature flag slug registered in FeaturesController.

private mixed FEATURE_SLUG = 'dual_code_graphql_api'

Methods

filter_methods_against_settings()

Apply the GraphQL-scoped site settings to a caller-declared list of HTTP methods.

public static filter_methods_against_settings(array<string|int, string> $methods) : array<string|int, string>

Centralises the "what verbs does the admin actually allow on a GraphQL endpoint" rule so both WooCommerce core's own endpoint and every sibling plugin endpoint applied through {@see} honour the same settings.

Currently the only rule is: if the GET endpoint has been disabled in the GraphQL settings section, strip GET from the list.

Parameters
$methods : array<string|int, string>

HTTP methods the caller declared.

Return values
array<string|int, string>Possibly narrowed list — may be empty, in which case the caller should skip the route registration entirely.

instantiate_graphql_controller()

Instantiate a GraphQL controller subclass and wire up its dependencies.

public static instantiate_graphql_controller(string $controller_class_name) : GraphQLControllerBase|null

Intended for sibling WooCommerce plugins that ship their own autogenerated GraphQLController subclass (emitted by build-api.php into their own autogenerated namespace). The returned controller is ready to have handle_request() attached to a REST route.

Returns null when the feature flag is off or PHP is < 8.1, so callers can invoke this unconditionally from inside their own rest_api_init handler.

Parameters
$controller_class_name : string

Fully-qualified name of a subclass of GraphQLControllerBase.

Tags
throws
InvalidArgumentException

If $controller_class_name does not extend GraphQLControllerBase.

Return values
GraphQLControllerBase|null

is_apq_enabled()

Whether the Apollo Automatic Persisted Queries (APQ) protocol is enabled.

public static is_apq_enabled() : bool

Defaults to true. When disabled, the persistedQuery request extension is ignored and requests are processed as standard (non-persisted) queries.

Return values
bool

is_enabled()

Check whether the Dual Code & GraphQL API feature is active.

public static is_enabled() : bool

Requires PHP 8.1+ and the dual_code_graphql_api feature flag to be enabled.

Return values
bool

is_get_endpoint_enabled()

Whether the GraphQL endpoint accepts GET requests.

public static is_get_endpoint_enabled() : bool

Defaults to false. Reads from the option written by the GraphQL settings section so the REST route registration can decide which HTTP methods to accept.

Return values
bool

is_object_cache_enabled()

Whether the ObjectCache-backed query cache is enabled.

public static is_object_cache_enabled() : bool

Defaults to true.

Return values
bool

is_opcache_enabled()

Whether the OPcache-backed query cache is enabled.

public static is_opcache_enabled() : bool

Defaults to true. Activation also depends on OPcache being loaded and the cache directory being writable; see {@see} for the runtime capability check.

Return values
bool

register()

Register the GraphQL endpoint when the feature is active.

public static register() : void

When the feature is off this is a no-op. Classes in the public Automattic\WooCommerce\Api\ namespace remain autoloadable — extensions that want to know whether the feature is active should check FeaturesUtil::feature_is_enabled( 'dual_code_graphql_api' ) rather than class_exists() on the Api namespace.

The feature-enabled check is deferred to the rest_api_init callback to avoid triggering translation loading (via FeaturesController) before the init action has fired, which would cause a _load_textdomain_just_in_time notice on WordPress 6.7+.

Return values
void

register_graphql_endpoint()

Register a GraphQL REST endpoint backed by a plugin-provided controller subclass.

public static register_graphql_endpoint(string $plugin_dir_or_controller_class, string $route_namespace, string $route[, array<string|int, string> $methods = array('GET', 'POST') ]) : void

May be called at any time up to and including the rest_api_init hook: if called earlier, registration is deferred to that hook; if called from inside another plugin's rest_api_init handler, registration happens immediately. Calls made after rest_api_init has 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's bootstrap 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.

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'
    );
}
Parameters
$plugin_dir_or_controller_class : string

Plugin root directory, OR a fully-qualified GraphQLController subclass name. See above.

$route_namespace : string

REST route namespace, as passed to register_rest_route().

$route : string

REST route path, as passed to register_rest_route().

$methods : array<string|int, string> = array('GET', 'POST')

HTTP methods accepted on the endpoint. Defaults to GET + POST.

Tags
throws
InvalidArgumentException

If no controller can be resolved from a directory argument, or if the resolved class does not extend GraphQLControllerBase.

Return values
void

assert_is_controller_subclass()

Assert that a class name resolves to a concrete GraphQLControllerBase subclass.

private static assert_is_controller_subclass(string $controller_class_name) : void
Parameters
$controller_class_name : string

Fully-qualified name of the class to validate.

Tags
throws
InvalidArgumentException

If the class cannot be autoloaded, or does not extend GraphQLControllerBase.

Return values
void

extract_namespace_from_php_source()

Extract an unbracketed `namespace …;` declaration from a PHP source fragment.

private static extract_namespace_from_php_source(string $source) : string|null

Uses PHP's tokenizer rather than a regex so the parse isn't fooled by declarations inside heredocs, comments, or bracketed-namespace syntax, and continues to work if the generator's output format changes (e.g. attributes before the declaration, single-line <?php namespace).

Parameters
$source : string

PHP source code.

Return values
string|nullThe namespace FQN without leading or trailing separator, or null if none found.

resolve_controller_class()

Resolve the first argument of {@see self::register_graphql_endpoint()} to a controller class name. If the argument is an existing directory, treat it as the plugin root and read the namespace from the generated controller file at the conventional path. Otherwise return the argument unchanged so it's used as a class FQCN.

private static resolve_controller_class(string $arg) : string
Parameters
$arg : string

Either a plugin root directory or a controller class FQCN.

Tags
throws
InvalidArgumentException

If the argument is a directory but the generated controller file is missing or doesn't declare a PHP namespace.

Return values
string