Automattic\WooCommerce\Api\Infrastructure

GraphQLControllerBase::compute_query_depthprivateWC 1.0

Compute the maximum nesting depth of the executing operation, under two different metrics:

  • tree_only: only fields whose own selection set is non-empty count toward depth; leaves are excluded. This is the number directly comparable to the "Maximum query depth" setting's limit, and matches what webonyx's QueryDepth validation rule measures for the enforcement decision.
  • in_depth: counts every field in the deepest chain, leaves included. Useful as a shape metric when inspecting a query.

Inline fragments pass through without incrementing either metric. Named-fragment spreads are not expanded here, so both numbers are lower bounds when spreads are present. The webonyx QueryDepth validation rule (which does expand spreads) remains the authoritative gate.

Method of the class: GraphQLControllerBase{}

No Hooks.

Returns

Array{tree_only:. int, in_depth: int}

Usage

// private - for code of main (parent) class only
$result = $this->compute_query_depth( $document, ?string $operation_name ): array;
$document(DocumentNode) (required)
The parsed GraphQL document.
?string $operation_name(required)
.

GraphQLControllerBase::compute_query_depth() code WC 10.9.1

private function compute_query_depth( DocumentNode $document, ?string $operation_name ): array {
	$tree_only = 0;
	$in_depth  = 0;
	foreach ( $document->definitions as $definition ) {
		if ( ! $definition instanceof OperationDefinitionNode ) {
			continue;
		}

		if ( null !== $operation_name && ( $definition->name->value ?? null ) !== $operation_name ) {
			continue;
		}

		$tree_only = max( $tree_only, $this->walk_depth_tree_only( $definition->selectionSet, 0 ) );
		$in_depth  = max( $in_depth, $this->walk_depth_in_depth( $definition->selectionSet, 0 ) );
	}

	return array(
		'tree_only' => $tree_only,
		'in_depth'  => $in_depth,
	);
}