Skip to content

Commit ddaae61

Browse files
committed
Removed event after query execution, as it is a duplicate of response in some ways. I wanted to reduce the amount of events for MVP.
1 parent 9a0f22e commit ddaae61

File tree

7 files changed

+6
-102
lines changed

7 files changed

+6
-102
lines changed

plugins/wpgraphql-logging/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ wpgraphql-logging/
7878
- **Query event lifecycle logging**
7979
- **Pre Request** (`do_graphql_request`): captures `query`, `variables`, `operation_name`.
8080
- **Before Execution** (`graphql_before_execute`): includes a snapshot of request `params`.
81-
- **After Execution** (`graphql_execute`): captures `response`/`ExecutionResult`, `schema`, and `request`.
8281
- **Before Response Returned** (`graphql_return_response`): inspects `response` and automatically upgrades level to Error when GraphQL `errors` are present (adds `errors` to context when found).
8382

8483
- **Built-in pub/sub event bus**

plugins/wpgraphql-logging/docs/Events.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ Currently we subscribe to the following WPGraphQL events:
3030
| --- | --- | --- |
3131
| `Events::PRE_REQUEST` | `do_graphql_request` | Before the request is processed |
3232
| `Events::BEFORE_GRAPHQL_EXECUTION` | `graphql_before_execute` | Before query execution |
33-
| `Events::AFTER_GRAPHQL_EXECUTION` | `graphql_execute` | After query execution |
3433
| `Events::BEFORE_RESPONSE_RETURNED` | `graphql_return_response` | Before response is returned to client |
3534

3635
## Usage
@@ -88,7 +87,7 @@ add_action('init', function () {
8887

8988
**Example**
9089

91-
Currently the plugin logs at four points in the WPGraphQL lifecycle: `do_graphql_request`, `graphql_before_execute`, `graphql_execute`, `graphql_return_response`. If you need more visibility you could do the following:
90+
Currently the plugin logs at three points in the WPGraphQL lifecycle: `do_graphql_request`, `graphql_before_execute`, `graphql_return_response`. If you need more visibility you could do the following:
9291

9392
```php
9493
use WPGraphQL\Logging\Logger\LoggerService;

plugins/wpgraphql-logging/src/Events/Events.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,6 @@ final class Events {
2424
*/
2525
public const BEFORE_GRAPHQL_EXECUTION = 'graphql_before_execute';
2626

27-
/**
28-
* WPGraphQL action: graphql_execute
29-
*
30-
* After the request is processed.
31-
*
32-
* @var string
33-
*/
34-
public const AFTER_GRAPHQL_EXECUTION = 'graphql_execute';
35-
3627
/**
3728
* WPGraphQL action: graphql_return_response
3829
*

plugins/wpgraphql-logging/src/Events/QueryEventLifecycle.php

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -133,51 +133,7 @@ public function log_graphql_before_execute(Request $request ): void {
133133
}
134134
}
135135

136-
/**
137-
* After Query Execution.
138-
*
139-
* @hook graphql_execute
140-
*
141-
* @param array<mixed>|\GraphQL\Executor\ExecutionResult $response The GraphQL execution result(s).
142-
* @param \WPGraphQL\WPSchema $schema The WPGraphQL schema.
143-
* @param string|null $operation The operation name.
144-
* @param string $query The query string.
145-
* @param array<string, mixed> $variables The variables for the query.
146-
* @param \WPGraphQL\Request $request The WPGraphQL request instance.
147-
*/
148-
public function log_after_graphql_execute( array|ExecutionResult $response, WPSchema $schema, ?string $operation, string $query, array $variables, Request $request ): void {
149136

150-
try {
151-
$context = [
152-
'query' => $query,
153-
'operation_name' => $operation ?? '',
154-
'variables' => $variables,
155-
'response' => $response,
156-
'schema' => $schema,
157-
'request' => $request,
158-
];
159-
160-
$payload = EventManager::transform(
161-
Events::AFTER_GRAPHQL_EXECUTION,
162-
[
163-
'context' => $context,
164-
'level' => Level::Info,
165-
]
166-
);
167-
168-
$this->logger->log( $payload['level'], 'WPGraphQL After Query Execution', $payload['context'] );
169-
170-
EventManager::publish(
171-
Events::AFTER_GRAPHQL_EXECUTION,
172-
[
173-
'context' => $payload['context'],
174-
'level' => (string) $payload['level']->getName(),
175-
]
176-
);
177-
} catch ( \Throwable $e ) {
178-
$this->process_application_error( Events::AFTER_GRAPHQL_EXECUTION, $e );
179-
}
180-
}
181137

182138
/**
183139
* Before the GraphQL response is returned to the client.
@@ -277,11 +233,6 @@ protected function setup(): void {
277233
*/
278234
add_action( 'graphql_before_execute', [ $this, 'log_graphql_before_execute' ], 10, 1 );
279235

280-
/**
281-
* After Query Execution
282-
*/
283-
add_action( 'graphql_execute', [ $this, 'log_after_graphql_execute' ], 10, 6 );
284-
285236
/**
286237
* Response/Error Handling
287238
*/

plugins/wpgraphql-logging/src/Logger/LoggerService.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,8 @@ public static function get_default_processors(): array {
226226
new ProcessIdProcessor(), // Logs the process ID.
227227
new WPGraphQLQueryProcessor(), // Custom processor to capture GraphQL request data.
228228
];
229+
230+
// Filter for users to add their own processors.
229231
return apply_filters( 'wpgraphql_logging_default_processors', $default_processors );
230232
}
231233

@@ -240,6 +242,8 @@ public static function get_default_handlers(): array {
240242
$default_handlers = [
241243
new WordPressDatabaseHandler(),
242244
];
245+
246+
// Filter for users to add their own handlers.
243247
return apply_filters( 'wpgraphql_logging_default_handlers', $default_handlers );
244248
}
245249

@@ -256,6 +260,7 @@ public static function get_default_context(): array {
256260
'site_url' => home_url(),
257261
];
258262

263+
// Filter for users to modify the default context.
259264
return apply_filters( 'wpgraphql_logging_default_context', $context );
260265
}
261266
}

plugins/wpgraphql-logging/tests/wpunit/Events/EventManagerTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,6 @@ public function eventConstantsProvider(): array {
328328
return [
329329
[Events::PRE_REQUEST, 'do_graphql_request'],
330330
[Events::BEFORE_GRAPHQL_EXECUTION, 'graphql_before_execute'],
331-
[Events::AFTER_GRAPHQL_EXECUTION, 'graphql_execute'],
332331
[Events::BEFORE_RESPONSE_RETURNED, 'graphql_return_response'],
333332
];
334333
}

plugins/wpgraphql-logging/tests/wpunit/Events/QueryEventLifecycleTest.php

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -215,47 +215,9 @@ public function test_log_graphql_before_execute_logs_correctly(): void {
215215
$this->assertEquals($request->params, $record['context']['params']);
216216
}
217217

218-
public function test_log_after_graphql_execute_logs_correctly(): void {
219-
$lifecycle = $this->create_lifecycle_with_mock_logger();
220-
221-
$response = $this->create_mock_execution_result(['posts' => [['title' => 'Test Post']]]);
222-
$schema = $this->create_mock_schema();
223-
$operation = 'GetPosts';
224-
$query = '{ posts { title } }';
225-
$variables = ['limit' => 10];
226-
$request = $this->create_mock_request();
227-
228-
$lifecycle->log_after_graphql_execute($response, $schema, $operation, $query, $variables, $request);
229-
230-
$this->assertTrue($this->test_handler->hasInfoRecords());
231-
$records = $this->test_handler->getRecords();
232-
$record = $records[0];
233-
234-
$this->assertEquals('WPGraphQL After Query Execution', $record['message']);
235-
$this->assertEquals($query, $record['context']['query']);
236-
$this->assertEquals($operation, $record['context']['operation_name']);
237-
$this->assertEquals($variables, $record['context']['variables']);
238-
$this->assertEquals($response, $record['context']['response']);
239-
$this->assertEquals($schema, $record['context']['schema']);
240-
$this->assertEquals($request, $record['context']['request']);
241-
}
242218

243-
public function test_log_after_graphql_execute_handles_null_operation(): void {
244-
$lifecycle = $this->create_lifecycle_with_mock_logger();
245219

246-
$response = $this->create_mock_execution_result();
247-
$schema = $this->create_mock_schema();
248-
$query = '{ posts { title } }';
249-
$variables = [];
250-
$request = $this->create_mock_request();
251-
252-
$lifecycle->log_after_graphql_execute($response, $schema, null, $query, $variables, $request);
253-
254-
$records = $this->test_handler->getRecords();
255-
$record = $records[0];
256220

257-
$this->assertEquals('', $record['context']['operation_name']);
258-
}
259221

260222
public function test_log_before_response_returned_logs_info_for_success(): void {
261223
$lifecycle = $this->create_lifecycle_with_mock_logger();
@@ -420,7 +382,6 @@ public function test_setup_registers_correct_priorities(): void {
420382

421383
$this->assertEquals(10, has_action('do_graphql_request', [$lifecycle, 'log_pre_request']));
422384
$this->assertEquals(10, has_action('graphql_before_execute', [$lifecycle, 'log_graphql_before_execute']));
423-
$this->assertEquals(10, has_action('graphql_execute', [$lifecycle, 'log_after_graphql_execute']));
424385
$this->assertEquals(10, has_action('graphql_return_response', [$lifecycle, 'log_before_response_returned']));
425386
}
426387

@@ -503,7 +464,6 @@ public function test_all_lifecycle_methods_handle_exceptions_gracefully(): void
503464

504465
$response = $this->create_mock_execution_result();
505466
$schema = $this->create_mock_schema();
506-
$lifecycle->log_after_graphql_execute($response, $schema, null, '{ test }', [], $request);
507467

508468
$lifecycle->log_before_response_returned($response, $response, $schema, null, '{ test }', null, $request, null);
509469

0 commit comments

Comments
 (0)