99
1010namespace WPGraphQL \Debug \Analysis ;
1111
12+ use WPGraphQL \Debug \Analysis \Metrics \Complexity ;
1213use WPGraphQL \Utils \QueryAnalyzer as OriginalQueryAnalyzer ;
13- use WPGraphQL \Request ;
14- use GraphQL \Type \Schema ;
1514
1615/**
1716 * Class QueryAnalyzerExtension
2019 */
2120class QueryAnalyzer {
2221
23- /**
24- * @var QueryAnalyzer The instance of the WPGraphQL Query Analyzer.
25- */
26- protected OriginalQueryAnalyzer $ query_analyzer ;
22+ /**
23+ * @var QueryAnalyzer The instance of the WPGraphQL Query Analyzer.
24+ */
25+ protected OriginalQueryAnalyzer $ query_analyzer ;
2726
28- /**
29- * Constructor for the QueryAnalyzerExtension.
30- *
31- * @param OriginalQueryAnalyzer $query_analyzer The instance of the WPGraphQL Query Analyzer.
32- */
33- public function __construct ( OriginalQueryAnalyzer $ query_analyzer ) {
34- $ this ->query_analyzer = $ query_analyzer ;
35- }
27+ /**
28+ * @var string|null The GraphQL query string for the current request.
29+ */
30+ protected ?string $ currentQuery = null ;
3631
37- /**
38- * Initializes the extension by adding necessary WordPress hooks.
39- */
40- public function init (): void {
41- add_filter ( 'graphql_query_analyzer_graphql_keys ' , [ $ this , 'addMetricsToAnalyzerOutput ' ], 10 , 5 );
42- }
32+ /**
33+ * @var array<string,mixed> The variables for the current GraphQL request.
34+ */
35+ protected array $ currentVariables = [];
4336
44- /**
45- * Adds new metrics and analysis results to the Query Analyzer's output.
46- * This method is a callback for the 'graphql_query_analyzer_graphql_keys' filter.
47- *
48- * @param array<string,mixed> $graphql_keys Existing data from the Query Analyzer.
49- * @param string $return_keys The keys returned to the X-GraphQL-Keys header.
50- * @param string $skipped_keys The keys that were skipped.
51- * @param string[] $return_keys_array The keys returned in array format.
52- * @param string[] $skipped_keys_array The keys skipped in array format.
53- * @return array<string,mixed> The modified GraphQL keys with custom metrics.
54- */
55- public function addMetricsToAnalyzerOutput (
56- array $ graphql_keys ,
57- string $ return_keys ,
58- string $ skipped_keys ,
59- array $ return_keys_array ,
60- array $ skipped_keys_array
61- ): array {
62- // Simulate deeply nested queries check
63- $ hasDeepNesting = $ this ->analyzeNestingDepth ();
37+ /**
38+ * Constructor for the QueryAnalyzerExtension.
39+ *
40+ * @param OriginalQueryAnalyzer $query_analyzer The instance of the WPGraphQL Query Analyzer.
41+ */
42+ public function __construct ( OriginalQueryAnalyzer $ query_analyzer ) {
43+ $ this ->query_analyzer = $ query_analyzer ;
44+ }
6445
65- // Simulate excessive field selection check
66- $ hasExcessiveFields = $ this ->analyzeFieldSelection ();
46+ /**
47+ * Initializes the extension by adding necessary WordPress hooks.
48+ */
49+ public function init (): void {
50+ add_filter ( 'graphql_query_analyzer_graphql_keys ' , [ $ this , 'addMetricsToAnalyzerOutput ' ], 10 , 5 );
51+ }
6752
68- // Simulate a custom complexity score calculation
69- $ customComplexityScore = $ this ->calculateCustomComplexity ();
53+ /**
54+ * Adds new metrics and analysis results to the Query Analyzer's output.
55+ * This method is a callback for the 'graphql_query_analyzer_graphql_keys' filter.
56+ *
57+ * @param array<string,mixed> $graphql_keys Existing data from the Query Analyzer.
58+ * @param string $return_keys The keys returned to the X-GraphQL-Keys header.
59+ * @param string $skipped_keys The keys that were skipped.
60+ * @param string[] $return_keys_array The keys returned in array format.
61+ * @param string[] $skipped_keys_array The keys skipped in array format.
62+ * @return array<string,mixed> The modified GraphQL keys with custom metrics.
63+ */
64+ public function addMetricsToAnalyzerOutput (
65+ array $ graphql_keys ,
66+ string $ return_keys ,
67+ string $ skipped_keys ,
68+ array $ return_keys_array ,
69+ array $ skipped_keys_array
70+ ): array {
71+ $ complexityValue = null ;
72+ $ complexityNote = 'Could not compute complexity ' ;
7073
71- // Add your custom data under a new key within the 'queryAnalyzer' extension.
72- $ graphql_keys ['DebugExtensionsAnalysis ' ] = [
73- 'heuristicRules ' => [
74- 'deepNestingDetected ' => $ hasDeepNesting ,
75- 'excessiveFieldsDetected ' => $ hasExcessiveFields ,
76- ],
77- 'performanceMetrics ' => [
78- 'customComplexityScore ' => $ customComplexityScore ,
79- 'dummyMemoryUsageKB ' => rand ( 1024 , 8192 ),
80- 'dummyExecutionTimeMs ' => rand ( 50 , 500 ),
81- ],
82- ];
74+ $ request = $ this ->query_analyzer ->get_request ();
75+ $ currentQuery = $ request ->params ->query ?? null ;
76+ $ currentVariables = (array ) ( $ request ->params ->variables ?? [] );
8377
84- return $ graphql_keys ;
85- }
78+ // Add some logging to debug.
79+ error_log ( 'QueryAnalyzerExtension: addCustomMetricsToAnalyzerOutput called. ' );
80+ error_log ( 'QueryAnalyzerExtension: Retrieved Query: ' . ( $ currentQuery ?? 'NULL ' ) );
81+ error_log ( 'QueryAnalyzerExtension: Retrieved Variables: ' . print_r ( $ currentVariables , true ) );
82+ if ( ! empty ( $ currentQuery ) ) {
83+ try {
84+ $ complexityMetrics = new Complexity ();
85+ $ schema = $ this ->query_analyzer ->get_schema ();
86+ $ complexityValue = $ complexityMetrics ->calculate ( $ currentQuery , $ currentVariables , $ schema );
8687
87- /**
88- * Placeholder method to simulate analysis of nesting depth.
89- * In a real implementation, this would parse the query AST
90- * and determine nesting levels.
91- *
92- * @return bool True if deep nesting is detected, false otherwise.
93- */
94- protected function analyzeNestingDepth (): bool {
95- // For now, return a random boolean for demonstration.
96- return (bool ) rand ( 0 , 1 );
97- }
88+ } catch (\Exception $ e ) {
89+ error_log ( 'WPGraphQL Debug Extensions: Complexity calculation failed: ' . $ e ->getMessage () );
90+ $ complexityNote .= ': ' . $ e ->getMessage ();
91+ }
92+ }
93+ if ( ! isset ( $ graphql_keys ['debugExtensions ' ] ) ) {
94+ $ graphql_keys ['debugExtensions ' ] = [];
95+ }
96+ $ graphql_keys ['debugExtensions ' ]['complexity ' ] = $ complexityValue ;
9897
99- /**
100- * Placeholder method to simulate analysis of excessive field selection.
101- * In a real implementation, this would count fields selected per type
102- * and compare against a threshold.
103- *
104- * @return bool True if excessive fields are detected, false otherwise.
105- */
106- protected function analyzeFieldSelection (): bool {
107- // For now, return a random boolean for demonstration.
108- return (bool ) rand ( 0 , 1 );
109- }
110-
111- /**
112- * Placeholder method to simulate a custom complexity score calculation.
113- * This could combine factors like nesting, field count, list fetches, etc.
114- *
115- * @return int A dummy complexity score.
116- */
117- protected function calculateCustomComplexity (): int {
118- // For now, return a random integer.
119- return rand ( 100 , 1000 );
120- }
98+ return $ graphql_keys ;
99+ }
121100}
0 commit comments