-
Notifications
You must be signed in to change notification settings - Fork 2
feat: new rule for excessive fields #383
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
141 changes: 141 additions & 0 deletions
141
plugins/wpgraphql-debug-extensions/src/Analysis/Rules/ExcessiveFields.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| <?php | ||
| /** | ||
| * GraphQL Rule to identify excessive field selection (over-fetching). | ||
| * | ||
| * @package WPGraphQL\Debug\Analysis\Rules | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace WPGraphQL\Debug\Analysis\Rules; | ||
|
|
||
| use GraphQL\Language\AST\FieldNode; | ||
| use GraphQL\Language\Parser; | ||
| use GraphQL\Language\Visitor; | ||
| use GraphQL\Type\Schema; | ||
| use GraphQL\Error\SyntaxError; | ||
| use GraphQL\Utils\TypeInfo; | ||
| use WPGraphQL\Debug\Analysis\Interfaces\AnalyzerItemInterface; | ||
|
|
||
| class ExcessiveFields implements AnalyzerItemInterface { | ||
|
|
||
| /** | ||
| * @var string|null A descriptive note about the analysis result. | ||
| */ | ||
| protected ?string $internalNote = null; | ||
|
|
||
| /** | ||
| * Default field threshold. | ||
| * | ||
| * This value determines how many fields may be requested | ||
| * on a single type before the rule considers it "excessive." | ||
| * | ||
| * Developers may override this using the | ||
| * `graphql_debug_excessive_fields_threshold` WordPress filter. | ||
| * | ||
| * @var int | ||
| */ | ||
| protected int $fieldThreshold = 15; | ||
|
|
||
| /** | ||
| * Analyze a GraphQL query for excessive field selections. | ||
| * | ||
| * @param string $query The raw GraphQL query string. | ||
| * @param array $variables Optional query variables. | ||
| * @param Schema|null $schema The GraphQL schema (required for type resolution). | ||
| * | ||
| * @return array{ | ||
| * triggered: bool, | ||
| * message: string | ||
| * } | ||
| */ | ||
| public function analyze( string $query, array $variables = [], ?Schema $schema = null ): array { | ||
| if ( null === $schema ) { | ||
| $schema = $this->query_analyzer->get_schema(); | ||
| } | ||
| if ( null === $schema ) { | ||
| $this->internalNote = 'Excessive field selection analysis requires a GraphQL schema.'; | ||
| return [ | ||
| 'triggered' => false, | ||
| 'message' => $this->internalNote, | ||
| ]; | ||
| } | ||
|
|
||
| $fieldCounts = []; | ||
|
|
||
| try { | ||
| $ast = Parser::parse( $query ); | ||
| } catch (SyntaxError $error) { | ||
| $this->internalNote = 'Excessive field selection analysis failed due to GraphQL syntax error: ' . $error->getMessage(); | ||
| error_log( 'WPGraphQL Debug Extensions: ' . $this->internalNote ); | ||
| return [ | ||
| 'triggered' => false, | ||
| 'message' => $this->internalNote, | ||
| ]; | ||
| } | ||
|
|
||
| // Use TypeInfo to correctly resolve parent types during traversal | ||
| $typeInfo = new TypeInfo( $schema ); | ||
|
|
||
| Visitor::visit( | ||
| $ast, | ||
| Visitor::visitWithTypeInfo( | ||
| $typeInfo, | ||
| [ | ||
| 'Field' => function (FieldNode $node) use (&$fieldCounts, $typeInfo) { | ||
| $parentType = $typeInfo->getParentType(); | ||
| if ( $parentType ) { | ||
| $typeName = $parentType->name; | ||
| if ( ! isset( $fieldCounts[ $typeName ] ) ) { | ||
| $fieldCounts[ $typeName ] = 0; | ||
| } | ||
| $fieldCounts[ $typeName ]++; | ||
| } | ||
| }, | ||
| ] | ||
| ) | ||
| ); | ||
|
|
||
| $threshold = apply_filters( | ||
| 'graphql_debug_rule_excessive_fields_threshold', | ||
| $this->fieldThreshold, | ||
| $query, | ||
| $variables, | ||
| $schema | ||
| ); | ||
|
|
||
| $triggered = false; | ||
| $details = []; | ||
| foreach ( $fieldCounts as $type => $count ) { | ||
| if ( $count > $threshold ) { | ||
| $triggered = true; | ||
| $details[] = sprintf( | ||
| 'Type "%s" selects %d fields, exceeding the threshold of %d.', | ||
| $type, | ||
| $count, | ||
| $threshold | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| $message = $triggered | ||
| ? 'Over-fetching detected: ' . implode( ' ', $details ) | ||
| : 'No excessive fields detected.'; | ||
|
|
||
| $this->internalNote = $message; | ||
|
|
||
| return [ | ||
| 'triggered' => $triggered, | ||
| 'message' => $message, | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * Return the unique key for this analyzer rule. | ||
| * | ||
| * @return string | ||
| */ | ||
| public function getKey(): string { | ||
| return 'excessiveFieldsRule'; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.