Skip to content

Conversation

@jasonbahl
Copy link
Collaborator

@jasonbahl jasonbahl commented Oct 30, 2025

What bug does this fix? Explain your changes.

The Bug

On WordPress VIP and similar platforms where intermediate image sizes are disabled, MediaItemSizeEnum was being registered with zero values. This creates an invalid GraphQL schema because the GraphQL specification requires that enums must define at least one value.

Error Message:

Enum type MediaItemSizeEnum must define one or more values.

This breaks:

  • Schema introspection
  • Code generation tools (e.g., graphql-codegen)
  • Any GraphQL client that validates schemas

Root Cause

The breaking change introduced in commit e5a9fe0 removed fallback support for image sizes. The get_image_sizes() method now solely relies on get_intermediate_image_sizes(), which returns an empty array on WordPress VIP (and possibly other environments) where intermediate sizes are disabled by platform policy.

The Fix

Added fallback logic to ensure MediaItemSizeEnum always has at least one value:

  1. When get_intermediate_image_sizes() returns empty, populate with standard WordPress image sizes: thumbnail, medium, medium_large, large, and full
  2. Special handling for full size (0x0 dimensions since it represents the original uploaded image)
  3. These enum values act as "hints" that WordPress core functions (like wp_get_attachment_image_src()) gracefully resolve at runtime

This maintains backward compatibility while ensuring schema validity on all platforms.

Does this close any currently open issues?

Closes #3432

Testing Strategy

This PR follows the recommended commit structure:

Commit 1: Failing Test

Commit: 58fc33a - Add test reproducing MediaItemSizeEnum empty values bug on VIP

The test simulates VIP environment by filtering get_intermediate_image_sizes to return empty array, then performs schema introspection to verify MediaItemSizeEnum has at least one value.

This commit will show failing tests in CI

Commit 2: Implementation Fix

Commit: 7db8c1bd - Add fallback image sizes to MediaItemSizeEnum

Implements the fix by adding fallback logic to get_image_sizes() method with comprehensive inline documentation.

This commit will show passing tests in CI

Test Results

Before/After Examples

Before (Buggy Behavior):

On WordPress VIP with no intermediate sizes:

query IntrospectMediaItemSizeEnum {
  __type(name: "MediaItemSizeEnum") {
    name
    enumValues {
      name
    }
  }
}

Result: Schema validation error - Enum type MediaItemSizeEnum must define one or more values

After (Fixed Behavior):

query IntrospectMediaItemSizeEnum {
  __type(name: "MediaItemSizeEnum") {
    name
    enumValues {
      name
      description
    }
  }
}

Result:

{
  "data": {
    "__type": {
      "name": "MediaItemSizeEnum",
      "enumValues": [
        { "name": "THUMBNAIL", "description": "Small image preview suitable for thumbnails and listings. (150x150)" },
        { "name": "MEDIUM", "description": "Medium image preview typically suitable for listings and detail views. (300x300)" },
        { "name": "MEDIUM_LARGE", "description": "Medium-to-large image preview suitable for listings and detail views. (768x0)" },
        { "name": "LARGE", "description": "Large image preview suitable for detail views. (1024x1024)" },
        { "name": "FULL", "description": "Full-size image." }
      ]
    }
  }
}

Additional Context

  • No Breaking Changes: This fix is fully backward compatible
  • Performance: No performance impact - fallback only activates when get_intermediate_image_sizes() returns empty
  • Platform Support: Specifically fixes WordPress VIP but benefits any platform that disables intermediate sizes
  • WordPress Core Compatibility: The enum values serve as hints; WordPress core functions already handle missing sizes gracefully by falling back to available sizes

Documentation Added

Comprehensive inline comments explain:

This test simulates WordPress VIP environment where get_intermediate_image_sizes()
returns an empty array. The test performs schema introspection and asserts that
MediaItemSizeEnum has at least one value, which is required by GraphQL spec.

This test currently fails, reproducing the bug reported in issue wp-graphql#3432.

See: wp-graphql#3432
When get_intermediate_image_sizes() returns empty (e.g., on WordPress VIP where
intermediate sizes are disabled), MediaItemSizeEnum now falls back to standard
WordPress image sizes: thumbnail, medium, medium_large, large, and full.

This ensures the enum always has at least one value, meeting GraphQL spec
requirements that enums must define one or more values.

The fallback sizes act as hints that WordPress core functions (like
wp_get_attachment_image_src) gracefully resolve at runtime, falling back
to available sizes when requested sizes don't exist.

Fixes wp-graphql#3432
@coveralls
Copy link

coveralls commented Oct 30, 2025

Coverage Status

coverage: 83.228% (+0.02%) from 83.207%
when pulling 92f6eee on jasonbahl:fix/3432-missing-media-item-size-values
into 6e64fd1 on wp-graphql:develop.

@jasonbahl jasonbahl merged commit 69f5b2e into wp-graphql:develop Oct 30, 2025
63 of 64 checks passed
jasonbahl pushed a commit that referenced this pull request Oct 30, 2025
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.

MediaItemSizeEnum becomes empty when intermediate image sizes are disabled → schema invalid

2 participants