Repository navigation
Core Data: Fix deleting a single item that is fetched on its own with a query - #86408
Conversation
…eleting it doesn't fail Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
🤖 PR meta 🤖🎉 PropsIf you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
Updated as activity occurs, without notifying anyone named here. Add the 📦 Bundle sizeSize Change: 0 B Total Size: 8.61 MB
⚡ PerformanceShow the resultsClient side metrics exclude the server response time. front-end-block-theme
front-end-classic-theme
media-processing
media-upload
post-editor
site-editor
🏁 Flaky testsSome tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information. Undo restores the post selection when another synced entity is loaded (RTC WebSocket) in
|
ramonjd
left a comment
There was a problem hiding this comment.
I'll give this a tentative check.
It's testing well for me.
I had to poke around, but the isArray check looks good in its new location.
Before #76318, receiveItems wrapped single items in an array (Array.isArray( items ) ? items : [ items ]), so they reached queries as a one-item list.
#76318 stopped wrapping them and added a check inside the reducer.
Returning early from there still stored the reducer's {} default, an entry with no itemIds.
Checking before the reducer runs means nothing gets stored at all.
| // `ifMatchingAction` lets through only collection responses. | ||
| action: ReceiveItemsAction & | ||
| Partial< QueryParts > & { items: Array< QueriedItem > } |
There was a problem hiding this comment.
This makes sense to me.
The Array.isArray( action.items ) seems like an oversight given the default state is {}?
There was a problem hiding this comment.
Yes, as far as I could tell, I think this might have just missed in the original PR. It's not that common to include a query with a single item and to then delete it via core-data, so it's probably just a flow we hadn't encountered until working on the media editor route.
|
Thanks for the review @ramonjd! I'll leave this PR open until next week, so we can gather feedback from others in case there's other ways to handle this. Between my own eyes and heavy reliance on a Claude session, I think this is a good way to fix it, but as always with core-data, I feel a bit cautious about changing things, so good to give others a chance to weigh in 🙂 |
You and me both. Thanks for diving in |
Mamaduka
left a comment
There was a problem hiding this comment.
This looks good. Thank you both!
What?
Fix a bug where deleting a single item that's fetched with its own query (e.g. adding
_embed) throws an error.This is effectively a follow-up to #76318 which updated core-data to treat single-item responses specially.
Why?
In
receiveQuerieswe bail early for the case of a single item being received (that's theArray.isArray()check). However, this reducer provides a default state of{}so an empty entry gets stored for single items that contain a query.As a result, further down the pipeline, when someone goes to delete the entity,
removeQueryItemsis called, and fails on the lineconst itemIds = queryItems.itemIds.filterbecauseitemIdsnever exists.At first, I was thinking of fixing this by doing an early return in
removeQueryItems, but I think the better fix is to skip going into thereceiveQueriesreducer, as that's more in keeping with the spirit of #76318 (if I'm reading things correctly).As for why we need to fix this? Well, over in #86349 I'm exploring adding actions to the experimental media editor route, so that we can enable permanently deleting media items. When I enabled the actions I ran into the bug that this PR fixes.
How?
receiveQueriesmove the check that the action items is an array to before the reducer is called, rather than an early return within the reducer. This ensures that we're not left with a stray empty{}objectTesting Instructions
If you'd like to test this out manually, you can use the following from the dev console while you're in the block editor. On trunk it'll throw an error, and on this PR it'll succeed.
Screenshots
Before this PR, you'd get the following error attempting to delete a single record that was received with a query (e.g.
_embed: 'author'):With this PR (if you're using the test script above) you should see this in the console:
Use of AI Tools
Claude Code (Opus 5.5)