Skip to content

Commit

Permalink
Core data: properly forward entity resolvers
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Apr 23, 2024
1 parent 14db362 commit 8ced963
Showing 1 changed file with 14 additions and 20 deletions.
34 changes: 14 additions & 20 deletions packages/core-data/src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
/**
* WordPress dependencies
*/
import { createReduxStore, register } from '@wordpress/data';
import {
createReduxStore,
register,
createRegistrySelector,
} from '@wordpress/data';

/**
* Internal dependencies
Expand Down Expand Up @@ -31,27 +35,17 @@ const entitiesConfig = [

const entitySelectors = entitiesConfig.reduce( ( result, entity ) => {
const { kind, name, plural } = entity;
result[ getMethodName( kind, name ) ] = ( state, key, query ) =>
selectors.getEntityRecord( state, kind, name, key, query );

if ( plural ) {
result[ getMethodName( kind, plural, 'get' ) ] = ( state, query ) =>
selectors.getEntityRecords( state, kind, name, query );
}
return result;
}, {} );

const entityResolvers = entitiesConfig.reduce( ( result, entity ) => {
const { kind, name, plural } = entity;
result[ getMethodName( kind, name ) ] = ( key, query ) =>
resolvers.getEntityRecord( kind, name, key, query );
result[ getMethodName( kind, name ) ] = createRegistrySelector(
( select ) => ( state, key, query ) =>
select( STORE_NAME ).getEntityRecord( kind, name, key, query )
);

if ( plural ) {
const pluralMethodName = getMethodName( kind, plural, 'get' );
result[ pluralMethodName ] = ( ...args ) =>
resolvers.getEntityRecords( kind, name, ...args );
result[ pluralMethodName ].shouldInvalidate = ( action ) =>
resolvers.getEntityRecords.shouldInvalidate( action, kind, name );
result[ pluralMethodName ] = createRegistrySelector(
( select ) => ( state, query ) =>
select( STORE_NAME ).getEntityRecords( kind, name, query )
);
}
return result;
}, {} );
Expand All @@ -69,7 +63,7 @@ const storeConfig = () => ( {
reducer,
actions: { ...actions, ...entityActions, ...createLocksActions() },
selectors: { ...selectors, ...entitySelectors },
resolvers: { ...resolvers, ...entityResolvers },
resolvers,
} );

/**
Expand Down

0 comments on commit 8ced963

Please sign in to comment.