Skip to content
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: Bring back support for nested _fields values #25083

Merged
merged 3 commits into from
Sep 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions packages/core-data/src/queried-data/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import createSelector from 'rememo';
import EquivalentKeyMap from 'equivalent-key-map';
import { get, has, set } from 'lodash';

/**
* Internal dependencies
Expand Down Expand Up @@ -76,12 +77,12 @@ function getQueriedItemsUncached( state, query ) {
// This accounts for the fact that queried items are stored by
// stable key without an associated fields query. Other requests
// may have included fewer fields properties.
const field = fields[ f ];
if ( ! item.hasOwnProperty( field ) ) {
const field = fields[ f ].split( '.' );
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this value is left as a string, it matches an undefined value in the state— in my testing I had both meta: { key: 'value' } and 'meta.key': undefined on item, and get matches the string key first, returning undefined. I'm not sure where that other key comes from, but preventing that would be a better fix for this workaround.

if ( ! has( item, field ) ) {
return null;
}

filteredItem[ field ] = item[ field ];
const value = get( item, field );
set( filteredItem, field, value );
}
} else {
// If expecting a complete item, validate that completeness, or
Expand Down
41 changes: 41 additions & 0 deletions packages/core-data/src/queried-data/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,47 @@ describe( 'getQueriedItems', () => {
] );
} );

it( 'should dynamically construct fields-filtered item from available data with nested fields', () => {
const state = {
items: {
1: {
id: 1,
content: 'chicken',
author: 'bob',
meta: {
template: 'single',
_private: 'unused',
},
},
2: {
id: 2,
content: 'ribs',
author: 'sally',
meta: {
template: 'single',
_private: 'unused',
},
},
},
itemIsComplete: {
1: true,
2: true,
},
queries: {
'': [ 1, 2 ],
},
};

const result = getQueriedItems( state, {
_fields: [ 'content', 'meta.template' ],
} );

expect( result ).toEqual( [
{ content: 'chicken', meta: { template: 'single' } },
{ content: 'ribs', meta: { template: 'single' } },
] );
} );

it( 'should return null if attempting to filter by yet-unknown fields', () => {
const state = {
items: {
Expand Down