Skip to content

Conversation

jasonbahl
Copy link
Collaborator

@jasonbahl jasonbahl commented Feb 7, 2025

What does this implement/fix? Explain your changes.

This fixes a bug where the MediaDetails.file field was returning a path to the file instead of the filename.

The description of the field is: The filename of the mediaItem, however values such as 2025/02/test-4.png were being returned, which is unexpected behavior.

Does this close any currently open issues?

closes: #2982

Any other comments?

Running a query like so:

query GetMediaItemFilePath($id: ID!) {
  mediaItem(id: $id, idType: DATABASE_ID) {
    mediaDetails {
      file
    }
  }
}

Before

the file field returns a path:

CleanShot 2025-02-07 at 10 39 21

After

the file field returns just the filename as the field description states it will:

CleanShot 2025-02-07 at 10 39 32

Breaking Change?

This is not considered a breaking change as the actual behavior was not matching the documented intent of the field.

However, some users may have been relying on the output with the path, using the bug as a feature, so for those users this might come across as a breaking change.

Should a user need the field to resolve how it was previously, the following filter can be used, which will resolve the field as it was before this bugfix:

add_filter( 'graphql_resolve_field', function( $result, $source, $args, $context, $info, $type_name, $field_key ) {

  if ( 'MediaDetails' !== $type_name && 'file' !== $field_key ) {
    return $result;
  }
  
  return $source['file'];

}, 10, 7 );

@jasonbahl jasonbahl added type: bug Issue that causes incorrect or unexpected behavior object type: media Relating to the MediaItem Type labels Feb 7, 2025
@jasonbahl jasonbahl self-assigned this Feb 7, 2025
Copy link

Code Climate has analyzed commit 7b09603 and detected 0 issues on this pull request.

View more on Code Climate.

@coveralls
Copy link

Coverage Status

coverage: 83.194% (+0.004%) from 83.19%
when pulling 7b09603 on jasonbahl:fix/2982-mediaDetails-file
into a005192 on wp-graphql:develop.

@jasonbahl jasonbahl merged commit eb824fd into wp-graphql:develop Feb 7, 2025
41 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
object type: media Relating to the MediaItem Type type: bug Issue that causes incorrect or unexpected behavior
Projects
None yet
Development

Successfully merging this pull request may close these issues.

mediaDetails sizes query returns incorrect value for "file"
2 participants