Skip to content

Conversation

@jasonbahl
Copy link
Collaborator

What does this implement/fix? Explain your changes.

🐛 Problem:

The graphql_root_value filter was being applied during the Request constructor before request parameters were parsed, preventing developers from accessing valuable request context (query string, variables, operation name, etc.) when filtering the root value.

Current behavior:

// In Request constructor - params are null here
$this->root_value = $this->get_root_value(); // ❌ Called too early

Impact:

  • Developers cannot access $request->get_params() in the filter
  • No access to query string, variables, operation name, or extensions
  • Blocks context-aware root value logic (like in WPGraphQL Subscriptions)

✅ Solution

Move root value determination to execution time when request parameters are available:

  1. Remove early assignment of $this->root_value = $this->get_root_value();
  2. Call at execution time when params are populated:
   $result = GraphQL::executeQuery(
       $this->schema,
       $query,
       $this->get_root_value(), // ✅ Now called when params are available
       $this->app_context,
       // ...
   );
  1. Update server config to use method call:
if ( ! empty( $this->get_root_value() ) ) {
  $config->setRootValue( $this->get_root_value() );
}

🧪 Testing

Added comprehensive test coverage in FiltersTest.php:

  1. testFilterGraphqlRootValueHasAccessToRequestParams()
  • Verifies filter receives Request object with populated params
  • Asserts params contain query, variables, operation name
  • Tests that root value can be modified based on request context
  1. testFilterGraphqlRootValueHasAccessToRequestParamsForBatchQuery()
  • Validates filter works correctly for batch requests
  • Ensures each request gets its own params and context
  • Tests filter is called for each query in the batch

Does this close any currently open issues?

closes #3405

Any other comments?

This supports a use-case in WPGraphQL Subscriptions:

add_filter( 'graphql_root_value', function( $root_value, $request ) {
    $params = $request->get_params();
    return $params->extensions['root_value'] ?? $root_value;
}, 10, 2 );

⚡ Backward Compatibility

This change should be fully backward compatible. Existing graphql_root_value filter callbacks that don't use request context will continue to work exactly as before. The change only adds new capabilities for filters that want to access request information, which was previously null so no filters would have been using it.

@jasonbahl jasonbahl merged commit fee8f7d into wp-graphql:develop Aug 29, 2025
31 of 35 checks passed
jasonbahl pushed a commit that referenced this pull request Aug 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: graphql_root_value filter runs before request params are available, limiting access to query context

1 participant