-
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
Core Data: Update method generating plural names #59881
Conversation
Size Change: -10 B (0%) Total Size: 1.71 MB
ℹ️ View Unchanged
|
ccaf2cf
to
6e77f68
Compare
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If 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. |
const entityConfig = rootEntitiesConfig.find( | ||
( config ) => config.kind === kind && config.name === name | ||
); | ||
export const getMethodName = ( kind, name, prefix = 'get' ) => { |
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.
So the "name" here is actually not the name of the entity but a plural form already. It was not clear to me when reading the diff and may confuse people. Maybe we should rename the argument?
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 think this is more like name
or pluralName
. Would updating method arg and JSDoc would be helpful? We could use nameOrPluralName
or something similar.
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.
JSDoc would be helpful yes
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.
Looks like a nice simplification 👍 Thanks!
Initially, I had concerns, but at a further glance, it seems like this touches on internal bits only and shouldn't cause any issues with 3rd party consumption whatsoever.
I have a few questions, but I believe this should mostly be good to go!
result[ getMethodName( kind, name, 'get', true ) ] = ( state, query ) => | ||
selectors.getEntityRecords( state, kind, name, query ); | ||
|
||
if ( plural ) { |
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.
Why is this check necessary? Don't all root entities have plural
now? Is it in case we add an entity in the future and it doesn't have a plural
defined?
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 guess Riad answered most of my question here - we intentionally don't want plural support for some of the entities.
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 mentioned this in the description, but I probably should've added an inline comment as well. Not every entity will have "plural" selectors. Current cases are site
and __unstableBase
, but we have to maintain them for BC.
result[ pluralMethodName ].shouldInvalidate = ( action ) => | ||
resolvers.getEntityRecords.shouldInvalidate( action, kind, name ); | ||
|
||
if ( plural ) { |
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.
Same as above.
@@ -228,6 +232,7 @@ export const rootEntitiesConfig = [ | |||
kind: 'root', | |||
baseURL: '/wp/v2/plugins', | |||
baseURLParams: { context: 'edit' }, | |||
plural: 'plugins', |
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.
Can we somehow guarantee that if we add a new entity in the future it will also have a plural
defined?
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.
It's not a required property. Maybe converting the file to TS and adding proper types could help us here.
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.
Since we keep the plural
check, we should be good 👍
usePlural && 'plural' in entityConfig && entityConfig?.plural | ||
? pascalCase( entityConfig.plural ) | ||
: nameSuffix; | ||
const suffix = pascalCase( name ); |
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.
Neat simplification here 👍
@@ -39,6 +39,7 @@ export const rootEntitiesConfig = [ | |||
'url', | |||
].join( ',' ), | |||
}, | |||
plural: '__unstableBases', |
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.
Do we need this for BC? because this particular entity doesn't support multiple records.
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.
Yes. This is for BC. I mentioned this in the PR description.
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.
Ooops, sorry :P
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 guess it could use an extra comment that explains why we need it then.
@@ -63,6 +64,7 @@ export const rootEntitiesConfig = [ | |||
name: 'site', | |||
kind: 'root', | |||
baseURL: '/wp/v2/settings', | |||
plural: 'sites', |
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.
Same here it seems this entity doesn't really need plural.
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.
Needed for BC as well.
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 think adding some comments on the BC aspect would be helpful.
Otherwise, looks good 🚀
@draganescu, you're too fast. I was till working on this PR 😄 |
Here's the follow-up with comments - #59946. |
* Core Data: Update method generating plural names * Remove plural flag
What?
PR updates the internal
getMethodName
helper and makes it "pure" by removing the reliance onrootEntitiesConfig
. The entity'splural
name needs to be explicitly passed to the method.I've also updated shortcut generation logic to generate plural selectors only when the entity config specifies its existence by defining a
plural
property. All existing configurations have also been updated.Why?
This will allow the method to be used for entities defined outside of the
rootEntitiesConfig
list. See #59243.Not every entity supports fetching the list of items (via
getEntityRecords
). Good examples are thesite
and__unstableBase
entities. They represent a single item with multiple properties. Running thegetSites()
selector does nothing. Updated logic allows configs to opt-out from generating plural shortcuts when the REST API schema doesn't support them.Note: We have to keep
getSites
andgetUnstableBases
around for backward compatibility.Testing Instructions
CI checks should be passing.
Inspect shortcuts by running
wp.data.select( 'core' )
in the DevTools console. Returned selectors should match the trunk.