Skip to content

Conversation

@theodesp
Copy link
Member

Description

This PR introduces a new GraphQL debug rule: ExcessiveFields.

The rule analyzes GraphQL queries to detect over-fetching, i.e., selecting an excessive number of fields for a single object type.
It uses the GraphQL schema to traverse queries and count fields per type. If any type exceeds the configurable threshold, the rule is triggered.

Key features:

  • Detects over-fetching across nested fields.
  • Configurable threshold via WordPress filter: graphql_debug_rule_excessive_fields_threshold
  • Full doc comments for maintainability.
  • Returns structured output indicating whether the rule is triggered and a descriptive message.

Related Issue

Dependant PRs

  • None

Type of Change

  • ✅ Bug fix (non-breaking change which fixes an issue)
  • ✨ New feature (non-breaking change which adds functionality)
  • 💥 Breaking change (fix or feature that would cause existing functionality to change)
  • 🧹 Code refactoring (no functional changes)
  • 📄 Example update (no functional changes)
  • 📝 Documentation update
  • 🔍 Performance improvement
  • 🧪 Test update

How Has This Been Tested?

  • Queries with fewer fields than the threshold correctly return "triggered": false.
  • Queries exceeding the threshold correctly return "triggered": true.
  • Nested fields are counted properly using the schema.
  • Threshold can be modified using the WordPress filter.

Example filter usage:

// Set custom threshold to 20 fields
add_filter( 'graphql_debug_rule_excessive_fields_threshold', function( $default, $query, $variables, $schema ) {
    return 20;
}, 10, 4 )

Example Queries

Query that triggers the rule

query InefficientQuery {
  posts(first: 1) {
    nodes {
      id
      title
      slug
      uri
      status
      date
      modified
      commentCount
      commentStatus
      pingStatus
      password
      link
      featuredImage {
        node {
          sourceUrl
          altText
          caption
        }
      }
      author {
        node {
          name
          email
          firstName
          lastName
          registeredDate
          description
          nickname
        }
      }
      content
      excerpt
    }
  }
}

Expected output:

{
  "excessiveFieldsRule": {
    "triggered": true,
    "message": "Over-fetching detected: Type \"Post\" selects 16 fields, exceeding the threshold of 15."
  }
}

Query that does NOT trigger the rule

query EfficientQuery {
  posts(first: 1) {
    nodes {
      id
      title
      slug
      content
    }
  }
}

Expected output:

{
  "excessiveFieldsRule": {
    "triggered": false,
    "message": "No excessive fields detected."
  }
}

@theodesp theodesp requested a review from a team as a code owner August 20, 2025 12:34
@changeset-bot
Copy link

changeset-bot bot commented Aug 20, 2025

⚠️ No Changeset found

Latest commit: a41c93d

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@github-actions
Copy link

ℹ️ Download the latest wpgraphql-debug-extensions plugin zip from this PR
(See the 'Artifacts' section at the bottom)

Copy link
Member

@colinmurphy colinmurphy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

15 fields seems a sensible number to me. LGTM 🚀 🚀 🚀

@theodesp theodesp enabled auto-merge August 21, 2025 13:12
@theodesp theodesp added this pull request to the merge queue Aug 25, 2025
Merged via the queue into main with commit da1c67a Aug 25, 2025
8 checks passed
@theodesp theodesp deleted the feat-rule-excessive-fields branch August 25, 2025 09:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants