Skip to content

Commit 6152185

Browse files
committed
feat: skip logging introspection queries
1 parent 821908b commit 6152185

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function __construct( LoggerService $logger, array $config ) {
6161
*/
6262
public function log_pre_request( string $query, ?string $operation_name, ?array $variables ): void {
6363
try {
64-
if ( ! $this->is_logging_enabled( $this->config ) ) {
64+
if ( ! $this->is_logging_enabled( $this->config, $query ) ) {
6565
return;
6666
}
6767
$selected_events = $this->config[ Basic_Configuration_Tab::EVENT_LOG_SELECTION ] ?? [];
@@ -96,7 +96,15 @@ public function log_pre_request( string $query, ?string $operation_name, ?array
9696
*/
9797
public function log_graphql_before_execute( Request $request ): void {
9898
try {
99-
if ( ! $this->is_logging_enabled( $this->config ) ) {
99+
/** @var \GraphQL\Server\OperationParams $params */
100+
$params = $request->params;
101+
$context = [
102+
'query' => $params->query,
103+
'operation_name' => $params->operation,
104+
'variables' => $params->variables,
105+
'params' => $params,
106+
];
107+
if ( ! $this->is_logging_enabled( $this->config, $params->query ) ) {
100108
return;
101109
}
102110
$selected_events = $this->config[ Basic_Configuration_Tab::EVENT_LOG_SELECTION ] ?? [];
@@ -106,14 +114,7 @@ public function log_graphql_before_execute( Request $request ): void {
106114
if ( ! in_array( Events::BEFORE_GRAPHQL_EXECUTION, $selected_events, true ) ) {
107115
return;
108116
}
109-
/** @var \GraphQL\Server\OperationParams $params */
110-
$params = $request->params;
111-
$context = [
112-
'query' => $params->query,
113-
'operation_name' => $params->operation,
114-
'variables' => $params->variables,
115-
'params' => $params,
116-
];
117+
117118
$payload = EventManager::transform( Events::BEFORE_GRAPHQL_EXECUTION, [
118119
'context' => $context,
119120
'level' => Level::Info,
@@ -150,7 +151,7 @@ public function log_before_response_returned(
150151
?string $query_id
151152
): void {
152153
try {
153-
if ( ! $this->is_logging_enabled( $this->config ) ) {
154+
if ( ! $this->is_logging_enabled( $this->config, $query ) ) {
154155
return;
155156
}
156157
$selected_events = $this->config[ Basic_Configuration_Tab::EVENT_LOG_SELECTION ] ?? [];

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ trait LoggingHelper {
1717
*
1818
* phpcs:disable Generic.Metrics.CyclomaticComplexity, SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh
1919
*/
20-
protected function is_logging_enabled( array $config ): bool {
20+
protected function is_logging_enabled( array $config, ?string $query_string = null ): bool {
2121
$is_enabled = true;
22-
2322
// Check the main "Enabled" checkbox.
2423
if ( ! (bool) ( $config[ Basic_Configuration_Tab::ENABLED ] ?? false ) ) {
2524
$is_enabled = false;
@@ -50,6 +49,11 @@ protected function is_logging_enabled( array $config ): bool {
5049
}
5150
}
5251

52+
// Check if the query is an introspection query and skip logging if it is.
53+
if ( $is_enabled && $this->is_introspection_query( $query_string ) ) {
54+
$is_enabled = false;
55+
}
56+
5357
/**
5458
* Filter the final decision on whether to log a request.
5559
*
@@ -58,4 +62,18 @@ protected function is_logging_enabled( array $config ): bool {
5862
*/
5963
return apply_filters( 'wpgraphql_logging_is_enabled', $is_enabled, $config );
6064
}
65+
66+
/**
67+
* Checks if a query is an introspection query.
68+
*
69+
* @param string|null $query_string The GraphQL query string.
70+
* @return bool
71+
*/
72+
protected function is_introspection_query( ?string $query_string ): bool {
73+
if ( null === $query_string ) {
74+
return false;
75+
}
76+
77+
return strpos( $query_string, '__schema' ) !== false;
78+
}
6179
}

0 commit comments

Comments
 (0)