Skip to content

Conversation

@justlevine
Copy link
Collaborator

@justlevine justlevine commented Sep 12, 2024

What does this implement/fix? Explain your changes.

This PR fixes an issue where trying to get a sourceUrl by size would cause the image URLs for all the sizes to resolve. This inevitably leads to slowdowns on sites with lots of registered image sizes (e.g. WooCommerce sites)

The fix works by introducing a new Post::get_source_url_by_size() public method used instead of the sourceUrlsBySize model field, which has been deprecated.

Does this close any currently open issues?

Discord: https://discord.com/channels/838854618406453300/1282730629305208832/1283796889615073321

Any relevant logs, error output, GraphiQL screenshots, etc?

Any other comments?

  • Alternatives considered:
    • Initially, I tried turning Post::sourceUrlsBySize into a callback, but that would be a breaking change.
    • Moreover, trying to turn any Model field into a callback won't work, since the first time the field is called, the callback + args will be cached and reused.
  • There's some questionable logic around arg coersion in the sourceUrl and fileSize resolvers in Registry\Utils\PostObject::register_attachment_fields(), but that's unaffected by this particular bug/fix.
  • Not entirely sure if/how to test this, since this is an under-the-hood fix.

Where has this been tested?

Operating System: Ubuntu 20.04 (wsl2 + devilbox + php 8.1.15)

WordPress Version: 6.6.2

@justlevine justlevine marked this pull request as draft September 12, 2024 17:50
@jasonbahl
Copy link
Collaborator

Not entirely sure if/how to test this, since this is an under-the-hood fix.

Do we have tests that query for the image sizes already and can at least confirm this is not a regression?

We could also add specific unit test(s) that call the function directly but not necessarily execute a GraphQL query like most of our wpunit tests do.

@justlevine
Copy link
Collaborator Author

Not entirely sure if/how to test this, since this is an under-the-hood fix.

Do we have tests that query for the image sizes already and can at least confirm this is not a regression?

Haha yes, that's why the test is failing now.

Got pulled away but basically returning a generator would be a breaking change for classes extending the Post model, while surfacing a model-level function doesn't fit with current conventions. Probably just needs to be wrapped better. 🤔

@qlty-cloud-legacy
Copy link

Code Climate has analyzed commit 79685af and detected 5 issues on this pull request.

Here's the issue category breakdown:

Category Count
Complexity 5

View more on Code Climate.

@justlevine justlevine changed the title fix: lazy-resolve Post.sourceUrlsBySize fix: lazy-resolve Post.sourceUrl and deprecate Post.sourceUrlsBySize Sep 14, 2024
@justlevine
Copy link
Collaborator Author

Callback approach won't work due to how fields are "cached" on the model. Instead I've made the Post::get_source_url_by_size() method public, which also means we have a way to test it explicitly (incoming).

@coveralls
Copy link

Coverage Status

coverage: 83.953% (-0.08%) from 84.035%
when pulling 79685af on justlevine:fix/lazy-load-sizes
into eaef11f on wp-graphql:develop.

@justlevine
Copy link
Collaborator Author

PS: this is ready once tests are added.

@justlevine justlevine added the needs: tests Tests should be added to be sure this works as expected label Sep 19, 2024
@justlevine justlevine marked this pull request as ready for review September 19, 2024 17:17
@jasonbahl jasonbahl merged commit 9d8a3d8 into wp-graphql:develop Oct 28, 2024
@justlevine
Copy link
Collaborator Author

@jasonbahl this needed tests 🫣

jasonbahl added a commit to jasonbahl/wp-graphql that referenced this pull request Oct 29, 2024
- tests querying the sourceUrl field and fileSize field on Media Item with various size arguments
- unit tests the testGetSourceUrlBySize function
@jasonbahl
Copy link
Collaborator

@justlevine I just opened this PR with some tests: #3219

Let me know if you think there are other tests we should consider adding.

@jasonbahl jasonbahl mentioned this pull request Nov 8, 2024
@justlevine justlevine deleted the fix/lazy-load-sizes branch February 10, 2025 22:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs: tests Tests should be added to be sure this works as expected

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants