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

Issue #13618 - only request three fields: id, title and parent to populate the Parent page select list #23637

Merged
merged 2 commits into from
Aug 26, 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
20 changes: 18 additions & 2 deletions packages/core-data/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,24 @@ export function* getEntityRecords( kind, name, query = {} ) {
...query,
context: 'edit',
} );
const records = yield apiFetch( { path } );
yield receiveEntityRecords( kind, name, Object.values( records ), query );

let records = Object.values( yield apiFetch( { path } ) );
// If we request fields but the result doesn't contain the fields,
// explicitely set these fields as "undefined"
// that way we consider the query "fullfilled".
if ( query._fields ) {
records = records.map( ( record ) => {
query._fields.split( ',' ).forEach( ( field ) => {
if ( ! record.hasOwnProperty( field ) ) {
record[ field ] = undefined;
}
} );

return record;
} );
}

yield receiveEntityRecords( kind, name, records, query );
}

getEntityRecords.shouldInvalidate = ( action, kind, name ) => {
Expand Down
1 change: 1 addition & 0 deletions packages/editor/src/components/page-attributes/parent.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ const applyWithSelect = withSelect( ( select ) => {
parent_exclude: postId,
orderby: 'menu_order',
order: 'asc',
_fields: 'id,title,parent',
Copy link
Contributor

Choose a reason for hiding this comment

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

This is something we tried but it's not a valid approach because the data module keeps a cache per id and if you do this and another component request the same entity but want access to the remaining properties, it will get a trimmed version causing issues. Basically whenever you use getEntityRecords you shouldn't explicitly pass context or fields. This PR tried to solve it #19498 but it's more complex than it seems.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well that's a bit of a bummer. Why not prevent caching for requests where _fields is used? And what should be done about context which is already being passed as edit. Or should the request use a completely different approach - such as an AJAX request that calls wp_dropdown_pages, which is what this code is trying to emulate after all.

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree that it's not great. Ideally, something like #19498 is pursued to completion and support multiple entities shapes in core-data.

I also think the unlimited API requests is one of the worst aspects of Gutenberg right now and it has other solutions: pagination and I believe @epiqueras and @adamsilverstein are also looking into this issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Given that the information returned for this _fields request is not going to be useful elsewhere, would it be possible to store it as a different entity type?

Copy link
Contributor

Choose a reason for hiding this comment

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

To land something like #19498, we would need to reinvent GraphQL, as can be seen from the caveats that led to the closing of the PR.

Can we use a direct API call in this component until pagination lands?

};

return {
Expand Down