-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix returning empty array in getEntityRecords for unknown entities #36984
Conversation
Size Change: +391 B (0%) Total Size: 1.1 MB
ℹ️ View Unchanged
|
@@ -160,14 +160,14 @@ describe( 'hasEntityRecords', () => { | |||
expect( hasEntityRecords( state, 'root', 'postType' ) ).toBe( false ); | |||
} ); | |||
|
|||
it( 'returns true if the entity configuration is not known', () => { | |||
it( 'returns false if the entity configuration is not known', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't make sense to me either. Why would we want to return true
for hasEntityRecords
when the entity is still unknown?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, agree, that doesn't seem right given the documentation for hasEntityRecords
:
Returns true if records have been received for the given set of parameters, or false otherwise.
The key word being 'received'. That queried data prepopulates for known entity configurations is irrelevant, those records weren't received in the typical way we use that term.
I also find the name hasEntityRecords
confusing since it should really be called hasReceivedEntityRecords
. The current name would lead me to believe an empty array would return false
too, but the function definition instead just checks Array.isArray
.
And with the way it works in trunk
it should perhaps just be called hasEntityConfiguration
😄
@kevin940726 It'd be good to detail what led you to making this fix. Does it fix a known bug or is it an observation you made about some code that could be improved? |
Sure! I found this bug while I was working on the templates list page. Specifically, I was calling I put a Since that the initial data is always |
This makes sense, and semantically I prefer it, but I'm not sure of what unexpected consequences it could have. So I'd defer to @youknowriad. (Also cc @ntsekouras — no action needed, just thought you might be interested in the topic.) |
Neither. I'd say let's document the change in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also agree with this semantically speaking but I know we had a few surprises in the past in this kind of changes so let's be vigilant about any subtle breakage.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto what Riad and Miguel said
Description
getEntityRecords
currently will return an empty array if the entity is unknown (not fetched), but it will also later returnnull
when the entity is loaded (viagetQueriedItems
), until finally return the list of records when the data is fully fetched.This behavior doesn't make sense to me, I think we should also return
null
if the entity is unknown, to align with thenull
value when the data is still fetching. By returningnull
for the initial request, the end developers can also better distinguish between a loaded empty list and a loading state.There are some former discussion about this: #19498 (comment), #21630 (comment). I believe the consensus was to always return
null
.This shouldn't be a breaking change since it already returns
null
in other cases, and we could treat the original behavior as a bug.How has this been tested?
Unit tests should pass.
Types of changes
Bug fix
Checklist:
*.native.js
files for terms that need renaming or removal).