apollo-client: [3.0] - pagination example does not work as intended
Intended outcome:
I’m trying to implement the pagination example with the new cache implementation
Actual outcome:
Using pagination example based n #5677 doesn’t work as expected
merge(existing: any[], incoming: any[], { args }) {
const merged = existing ? existing.slice(0) : [];
// Insert the incoming elements in the right places, according to args.
for (let i = args.offset; i < args.offset + args.limit; ++i) {
merged[i] = incoming[i - args.offset];
}
return merged;
},
read(existing: any[], { args }) {
// If we read the field before any data has been written to the
// cache, this function will return undefined, which correctly
// indicates that the field is missing.
return existing && existing.slice(
args.offset,
args.offset + args.limit,
);
},
In the merge function, instead of doing
for (let i = args.offset; i < args.offset + args.limit; ++i)
I had to do
for (let i = args.offset; i < Math.min(args.offset + incoming.length, args.offset + args.limit) ; ++i)
since the incoming length could be less than the offset, so it will add undefined items to the array
In the read function, if the existing arg has data, and we slice to a bigger offset, it return an empty array, not undefined, so the query is not passed to the server
insetad of doing
return existing && existing.slice(
args.offset,
args.offset + args.limit,
);
I did
const data =
existing && existing.slice(args.offset, args.offset + args.limit);
return data && data.length === 0 ? undefined : data;
Also the types of the example fails as they are
intead of
merge(existing: any[], incoming: any[], { args }) {
...
}
read(existing: any[], { args }) {
...
}
I replaced with
merge(existing: any, incoming: any, { args }: { args: any }) {
...
}
read(existing: any, { args }: { args: any }) {
....
}
Versions
System:
OS: macOS 10.15.2
Binaries:
Node: 12.12.0 - /usr/local/bin/node
Yarn: 1.19.1 - /usr/local/bin/yarn
npm: 6.11.3 - /usr/local/bin/npm
Browsers:
Chrome: 79.0.3945.130
Safari: 13.0.4
npmPackages:
@apollo/client: ^3.0.0-beta.30 => 3.0.0-beta.30
@apollo/link-context: ^2.0.0-beta.3 => 2.0.0-beta.3
@apollo/link-error: ^2.0.0-beta.3 => 2.0.0-beta.3
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 15 (5 by maintainers)
Commits related to this issue
- Avoid constraining read/merge parameters to StoreValue type. Should help with the type issues reported in #5881, by defaulting TExisting and TIncoming to any. The last time I tried this, I ran into ... — committed to apollographql/apollo-client by benjamn 4 years ago
- Address feedback on pagination examples raised in #5881. Follow-up to #5677. Thanks to @tafelito for actually trying out the code examples in the new cache policies documentation! — committed to apollographql/apollo-client by benjamn 4 years ago
- Merge pull request #5884 from apollographql/relax-read-and-merge-default-parameter-types Address feedback on pagination examples raised in #5881. — committed to apollographql/apollo-client by benjamn 4 years ago
with the latest updates of AC, this seems to be working fine now. I just wanted to leave a working example on how to handle a numbered pagination, where you can delete and insert items to the list without having to refetch queries from the server in it’s not necessary. Of course this works in hand with the
cache.modifyandcache.evictfunctions when doing an insert and a delete mutationpagination.ts
I agree this is an area that we need to handle better in the documentation. Let us know if the updates in #6429 do not answer your questions (or the questions you think other developers might have). Thanks again for pointing out the issues with the original pagination examples!
@benjamn I know you guys are busy with the release of the upcoming v3 but can we get back to this at some point, at least on the docs?. We use a lot of tables in a admin dashboard where we can edit/remove/add rows and we still have to refetch after any crud operation. I thought with the new AC cache the pagination was gonna be fixed but this still happening. i think is a really common use case and It would be great to include a best way to implement this sort of pagination in the docs
Tanks for the great work!
@benjamn you can see the repro of the 1 issue https://codesandbox.io/s/nostalgic-pike-94wxh