-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathQueryAnalyzer.php
More file actions
114 lines (100 loc) · 4.15 KB
/
Copy pathQueryAnalyzer.php
File metadata and controls
114 lines (100 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
/**
* Extends WPGraphQL's Query Analyzer to add custom heuristic rules and metrics.
*
* @package WPGraphQL\Debug\Analysis
*/
declare(strict_types=1);
namespace WPGraphQL\Debug\Analysis;
use WPGraphQL\Debug\Analysis\Interfaces\AnalyzerItemInterface;
use WPGraphQL\Utils\QueryAnalyzer as OriginalQueryAnalyzer;
/**
* Class QueryAnalyzer
*
* This class hooks into the WPGraphQL Query Analyzer to add custom analysis.
*/
class QueryAnalyzer {
/**
* @var OriginalQueryAnalyzer The instance of the WPGraphQL Query Analyzer from the core plugin.
*/
protected OriginalQueryAnalyzer $query_analyzer;
/**
* @var AnalyzerItemInterface[] An array of registered analyzer items (metrics and rules).
*/
protected array $analyzerItems = [];
/**
* Constructor for the QueryAnalyzerExtension.
*
* @param OriginalQueryAnalyzer $query_analyzer The instance of the WPGraphQL Query Analyzer.
*/
public function __construct( OriginalQueryAnalyzer $query_analyzer ) {
$this->query_analyzer = $query_analyzer;
}
/**
* Adds an AnalyzerItem (metric or rule) to be processed.
*
* @param AnalyzerItemInterface $item The item to add.
* @return void
*/
public function addAnalyzerItem( AnalyzerItemInterface $item ): void {
$this->analyzerItems[] = $item;
}
/**
* Initializes the extension by adding necessary WordPress hooks.
*/
public function init(): void {
// This filter allows us to inject custom data into the 'debugExtensions' part of the GraphQL response.
add_filter( 'graphql_query_analyzer_graphql_keys', [ $this, 'addAnalysisToOutput' ], 10, 5 );
}
/**
* Adds new metrics and analysis results to the Query Analyzer's output.
* This method is a callback for the 'graphql_query_analyzer_graphql_keys' filter.
*
* @param array<string,mixed> $graphql_keys Existing data from the Query Analyzer.
* @param string $return_keys The keys returned to the X-GraphQL-Keys header. (unused here)
* @param string $skipped_keys The keys that were skipped. (unused here)
* @param string[] $return_keys_array The keys returned in array format. (unused here)
* @param string[] $skipped_keys_array The keys skipped in array format. (unused here)
* @return array<string,mixed> The modified GraphQL keys with custom metrics.
*/
public function addAnalysisToOutput(
array $graphql_keys,
string $return_keys, // Keep for filter signature, but not used.
string $skipped_keys, // Keep for filter signature, but not used.
array $return_keys_array, // Keep for filter signature, but not used.
array $skipped_keys_array // Keep for filter signature, but not used.
): array {
if ( ! isset( $graphql_keys['debugExtensions'] ) ) {
$graphql_keys['debugExtensions'] = [];
}
$request = $this->query_analyzer->get_request();
$currentQuery = $request->params->query ?? null;
$currentVariables = (array) ( $request->params->variables ?? [] );
$schema = $this->query_analyzer->get_schema();
foreach ( $this->analyzerItems as $item ) {
try {
if ( ! empty( $currentQuery ) ) {
$result = $item->analyze( $currentQuery, $currentVariables, $schema );
} else {
$result = [
'value' => null,
'note' => 'No query provided for analysis.',
];
}
} catch ( \Exception $e ) {
error_log( sprintf(
'WPGraphQL Debug Extensions: Analysis item "%s" failed: %s',
$item->getKey(),
$e->getMessage()
) );
$result = [
'value' => null,
'note' => 'Analysis failed: ' . $e->getMessage(),
'error' => true,
];
}
$graphql_keys['debugExtensions'][ $item->getKey() ] = $result;
}
return $graphql_keys;
}
}