Skip to content

Conversation

@theodesp
Copy link
Member

@theodesp theodesp commented Sep 1, 2025

Description

This pull request refactors the WPGraphQL logging system to improve modularity and maintainability by addressing the "god class" anti-pattern. The primary changes involve splitting the monolithic QueryEventLifecycle class into smaller, more focused components.

Key changes include:

  • Separation of Concerns: QueryEventLifecycle now serves as a Facade.
  • New Loggers: The new QueryActionLogger and QueryFilterLogger classes are now responsible for handling all logging events related to WordPress actions and filters, respectively.
  • Code Reusability: A LoggingHelper trait centralizes the is_logging_enabled() logic.
  • Enhanced Admin Settings: The logging settings page has been updated to include all new log points.

Related Issue

Fixes #390


Dependant PRs

N/A


Type of Change

  • ✅ Bug fix (non-breaking change which fixes an issue)
  • ✨ New feature (non-breaking change which adds functionality)
  • 🧹 Code refactoring (no functional changes)

How Has This Been Tested?

The following manual tests were performed to verify the functionality of each new logging point.

Testing Environment

  • WordPress: 6.5.4
  • WPGraphQL: 1.20.0
  • PHP: 8.2
  • Local by Flywheel environment

Manual Testing Instructions

  1. Navigate to WP Admin > GraphQL > Logging and enable the Basic Configuration tab.
  2. In the Log Points field, select the specific events you want to test and save the settings.
  3. Execute the corresponding GraphQL query using an IDE like GraphiQL.
  4. Check the configured log destination (e.g., a file or database) to confirm the log entry was created with the correct data.

Action Hook Tests

Log Point Test Steps Expected Result
Pre Request (Events::PRE_REQUEST) Execute any GraphQL query. A log entry should be created immediately, containing the raw query string, operation name, and variables.
Before Query Execution (Events::BEFORE_GRAPHQL_EXECUTION) Execute a valid GraphQL query. A log entry should be created right before the query is resolved, containing details of the operation, query, and variables.
Before Response Returned (Events::BEFORE_RESPONSE_RETURNED) Execute a valid GraphQL query. A log entry should be created with the full response payload, including the final data or errors.

Filter Hook Tests

Log Point Test Steps Expected Result
Request Data (Events::REQUEST_DATA) Execute any GraphQL query. A log entry should be created, containing the raw request data array.
Request Results (Events::REQUEST_RESULTS) Execute a valid GraphQL query that returns data. A log entry should be created with the final execution result (data).
Execute a query with a syntax error or a non-existent field. A log entry should be created with the final execution result, including the error details.
Response Headers (Events::RESPONSE_HEADERS_TO_SEND) Execute a valid GraphQL query. The HTTP response headers should include a new header: X-WPGraphQL-Logging-ID with a unique ID.

Global Filter Test

Log Point Test Steps Expected Result
Global Logging Filter (wpgraphql_logging_is_enabled) Add the following to your theme's functions.php:
add_filter('wpgraphql_logging_is_enabled', function($is_enabled, $config) {
$query_name = $config['query_name'] ?? '';
if ($query_name === 'GetPost') {
return false;
}
return $is_enabled;
}, 10, 2);

Then, run a query named GetPost and another query named GetPosts.
The GetPost query should not log, while GetPosts query should log normally.

Screenshots


Checklist

  • I have read the CONTRIBUTING document
  • My code follows the project's coding standards
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation (if applicable)
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works (if applicable)
  • Any dependent changes have been highlighted, merged or published

@theodesp theodesp requested a review from a team as a code owner September 1, 2025 14:00
@changeset-bot
Copy link

changeset-bot bot commented Sep 1, 2025

⚠️ No Changeset found

Latest commit: 8178a4f

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@github-actions
Copy link

github-actions bot commented Sep 1, 2025

ℹ️ Download the latest wpgraphql-logging plugin zip from this PR
(See the 'Artifacts' section at the bottom)

Copy link
Member

@colinmurphy colinmurphy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@theodesp LGTM 🚀 ❤️

@theodesp theodesp added this pull request to the merge queue Sep 1, 2025
Merged via the queue into main with commit eba3f3c Sep 1, 2025
9 of 21 checks passed
@theodesp theodesp deleted the feat-logging-additional-settings branch September 1, 2025 14:27
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.

chore: Logging admin - integrate a few missing settings

3 participants