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

Add support for embedding and prefetching #10270

Merged
merged 20 commits into from
Oct 16, 2024
Merged

Add support for embedding and prefetching #10270

merged 20 commits into from
Oct 16, 2024

Conversation

fzaninotto
Copy link
Member

@fzaninotto fzaninotto commented Oct 10, 2024

Problem

Pages aggregating data from more than one resource always have to fetch more than one API endpoint in cascade, and with a slow backend this leads to an incomplete and blinking UI.

For example, the Comment list page in the simple example shows the post for each comment. So it has to fetch dataProvider.getList('comments'), then dataProvider.getMany('posts', { ids: [comment postIds] }) (via <ReferenceField>).

image

Some backends are capable of embedding related records (e.g. the post of a comment). However, populating the cache based on these embeds is cumbersome.

Solution

  • Allow developers to embed related data in the main data
  • Allow developers to prefetch related records and populate the cache with it

TODO

  • Allow every data provider method to return a response meta
  • Update useDataProvider to populate query cache when a response contains a meta: { prefetched }
  • Add support for embedding and prefetching to ra-data-fakerest
  • Add support for embedding to ra-data-json-server
  • [ ] Add support for embedding and prefetching to ra-data-simplerest(out of scope)
  • Update the simple example to use prefetch in the comment list and show views.
  • add tests
  • Add doc
const CommentList = () => (
    <ListBase queryOptions={{ meta: { prefetch: ['post'] } }}>
        <ListView />
    </ListBase>
);
image

## Problem

Pages aggregating data from more than one resource always have to fetch more than one API endpoint in cascade, and with a slow backend this leads to a blinking UI.

For example, the Comment detail page in the simple example has to call `dataProvider.getOne('comments', { id })`, then `dataProvider.getMany('posts', { ids: [comment.postId] })`.

Some backends are capable of embedding related records (e.g. the post of a comment). Populating the cache based on these embeds is possible, but cumbersome.

## Solution

Add first-class support for the `_embed` key in response metadata, and populate query cache from it.

## TODO

- [x] Allow every data provider method to return a response `meta`
- [x] Update `useDataProvider` to populate query cache when a response contains a `meta: { _embed }`
- [x] Update the fakerest data provider to support embeds.
- [x] Update the simple example to use embeds in the comment list and show views.
- [ ] add tests
- [ ] Add doc
@djhi
Copy link
Collaborator

djhi commented Oct 10, 2024

Note: json-server supports embeds

docs/ReferenceField.md Outdated Show resolved Hide resolved
@fzaninotto fzaninotto changed the title Add support for embeds in query responses Add support for embedding and prefetching Oct 11, 2024
docs/DataProviderWriting.md Outdated Show resolved Hide resolved
packages/ra-core/src/dataProvider/populateQueryCache.ts Outdated Show resolved Hide resolved
packages/ra-core/src/dataProvider/useDataProvider.spec.tsx Outdated Show resolved Hide resolved
Co-authored-by: Gildas Garcia <1122076+djhi@users.noreply.github.com>
Copy link
Contributor

@slax57 slax57 left a comment

Choose a reason for hiding this comment

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

Incredible feature! And the actual implementation is actually not as hard as I would have imagined.
Love it!

docs/DataProviderWriting.md Outdated Show resolved Hide resolved
docs/DataProviderWriting.md Outdated Show resolved Hide resolved
docs/DataProviderWriting.md Outdated Show resolved Hide resolved
packages/ra-data-fakerest/README.md Show resolved Hide resolved
packages/ra-data-fakerest/README.md Outdated Show resolved Hide resolved
packages/ra-data-json-server/README.md Show resolved Hide resolved
packages/ra-core/src/dataProvider/populateQueryCache.ts Outdated Show resolved Hide resolved
Co-authored-by: Jean-Baptiste Kaiser <jb@marmelab.com>
@slax57 slax57 added this to the 5.3.0 milestone Oct 16, 2024
@slax57 slax57 merged commit 5e3a7ea into next Oct 16, 2024
14 checks passed
@slax57 slax57 deleted the embeds branch October 16, 2024 11:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
RFR Ready For Review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants