Skip to content

Commit aa7d567

Browse files
committed
feat: add dummy data in extensions
1 parent 29c8c31 commit aa7d567

File tree

3 files changed

+157
-1
lines changed

3 files changed

+157
-1
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php
2+
/**
3+
* Extends WPGraphQL's Query Analyzer to add custom heuristic rules and metrics.
4+
*
5+
* @package WPGraphQL\Debug
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace WPGraphQL\Debug\Analysis;
11+
12+
use WPGraphQL\Utils\QueryAnalyzer as OriginalQueryAnalyzer;
13+
use WPGraphQL\Request;
14+
use GraphQL\Type\Schema;
15+
16+
/**
17+
* Class QueryAnalyzerExtension
18+
*
19+
* This class hooks into the WPGraphQL Query Analyzer to add custom analysis.
20+
*/
21+
class QueryAnalyzer {
22+
23+
/**
24+
* @var QueryAnalyzer The instance of the WPGraphQL Query Analyzer.
25+
*/
26+
protected OriginalQueryAnalyzer $query_analyzer;
27+
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+
}
36+
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+
}
43+
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();
64+
65+
// Simulate excessive field selection check
66+
$hasExcessiveFields = $this->analyzeFieldSelection();
67+
68+
// Simulate a custom complexity score calculation
69+
$customComplexityScore = $this->calculateCustomComplexity();
70+
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+
];
83+
84+
return $graphql_keys;
85+
}
86+
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+
}
98+
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+
}
121+
}

plugins/wpgraphql-debug-extensions/src/Plugin.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
namespace WPGraphQL\Debug;
1111

1212
use AxeWP\GraphQL\Helper\Helper;
13+
use WPGraphQL\Debug\Analysis\QueryAnalyzer;
14+
use WPGraphQL\Utils\QueryAnalyzer as OriginalQueryAnalyzer;
1315

1416
/**
1517
* Plugin singleton class.
@@ -55,6 +57,40 @@ private function setup(): void {
5557
// Set the hook prefix for consistency across the plugin's actions/filters.
5658
// This can be used by other parts of the plugin if generic helpers are introduced.
5759
Helper::set_hook_prefix( 'graphql_debug_extensions' );
60+
/**
61+
* Hook into the 'graphql_determine_graphql_keys' action.
62+
* This action is triggered within WPGraphQL\Utils\QueryAnalyzer::determine_graphql_keys().
63+
* It provides the QueryAnalyzer instance as its first argument, allowing us to
64+
* initialize our custom extension with the core QueryAnalyzer object.
65+
*
66+
* We're using a static variable `$initialized` to ensure our extension is
67+
* initialized only once per request, even if this action is called multiple times
68+
* (though typically it's once per main request).
69+
*
70+
* @param QueryAnalyzer $query_analyzer_instance The instance of the WPGraphQL Query Analyzer.
71+
* @param string $query The GraphQL query string being executed.
72+
*/
73+
add_action( 'graphql_determine_graphql_keys', function ($query_analyzer_instance) {
74+
static $initialized = false;
75+
76+
// Only initialize once per request to prevent redundant hooks/logic.
77+
if ( $initialized ) {
78+
return;
79+
}
80+
81+
// Ensure that the received instance is indeed a QueryAnalyzer.
82+
if ( $query_analyzer_instance instanceof OriginalQueryAnalyzer ) {
83+
// Create an instance of your custom QueryAnalyzer.
84+
// Pass the core QueryAnalyzer instance to its constructor
85+
// so your extension can access its methods and properties.
86+
$debug_analyzer = new QueryAnalyzer( $query_analyzer_instance );
87+
88+
// Initialize your extension. This is where it will register its own hooks
89+
$debug_analyzer->init();
90+
91+
$initialized = true;
92+
}
93+
}, 10, 2 );
5894
}
5995

6096
/**

plugins/wpgraphql-debug-extensions/wpgraphql-debug-extensions.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ function wpgraphql_debug_extensions_dependencies_not_ready(): array {
104104
function wpgraphql_debug_extensions_init(): void {
105105
wpgraphql_debug_extensions_constants();
106106
$not_ready = wpgraphql_debug_extensions_dependencies_not_ready();
107-
108107
if ( $not_ready === [] && defined( 'WPGRAPHQL_DEBUG_EXTENSIONS_PLUGIN_DIR' ) ) {
109108
// Load text domain at the init hook
110109
add_action( 'init', 'WPGraphQL\Debug\wpgraphql_debug_extensions_load_textdomain' );

0 commit comments

Comments
 (0)