Skip to content

Commit

Permalink
Address feedback on pagination examples raised in #5881.
Browse files Browse the repository at this point in the history
Follow-up to #5677.

Thanks to @tafelito for actually trying out the code examples in the new
cache policies documentation!
  • Loading branch information
benjamn committed Jan 30, 2020
1 parent 47a667b commit 6a2aff3
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions docs/source/caching/cache-field-behavior.md
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,8 @@ const cache = new InMemoryCache({
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) {
const end = args.offset + Math.min(args.limit, incoming.length);
for (let i = args.offset; i < end; ++i) {
merged[i] = incoming[i - args.offset];
}
return merged;
Expand All @@ -346,10 +347,16 @@ const cache = new InMemoryCache({
// 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(
const page = existing && existing.slice(
args.offset,
args.offset + args.limit,
);
// If we ask for a page outside the bounds of the existing array,
// page.length will be 0, and we should return undefined instead of
// the empty array.
if (page && page.length > 0) {
return page;
}
},
},
},
Expand Down Expand Up @@ -394,10 +401,13 @@ const cache = new InMemoryCache({
const afterIndex = existing.findIndex(
task => args.afterId === readField("id", task));
if (afterIndex >= 0) {
return existing.slice(
const page = existing.slice(
afterIndex + 1,
afterIndex + 1 + args.limit,
);
if (page && page.length > 0) {
return page;
}
}
}
},
Expand Down

0 comments on commit 6a2aff3

Please sign in to comment.