Fix apollo cache merge issue

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2021-08-09 14:24:38 +02:00
parent bfc9dd7ea3
commit 9243be2448
No known key found for this signature in database
GPG Key ID: A061B9DDE0CA0773

View File

@ -165,12 +165,13 @@ function doMerge<T = any>(
args: Record<string, any> | null args: Record<string, any> | null
): Array<T> { ): Array<T> {
const merged = existing && Array.isArray(existing) ? existing.slice(0) : []; const merged = existing && Array.isArray(existing) ? existing.slice(0) : [];
const previous = incoming && Array.isArray(incoming) ? incoming.slice(0) : [];
let res; let res;
if (args) { if (args) {
// Assume an page of 1 if args.page omitted. // Assume an page of 1 if args.page omitted.
const { page = 1, limit = 10 } = args; const { page = 1, limit = 10 } = args;
for (let i = 0; i < incoming.length; ++i) { for (let i = 0; i < previous.length; ++i) {
merged[(page - 1) * limit + i] = incoming[i]; merged[(page - 1) * limit + i] = previous[i];
} }
res = merged; res = merged;
} else { } else {
@ -178,7 +179,7 @@ function doMerge<T = any>(
// to receive any arguments, so you might prefer to throw an // to receive any arguments, so you might prefer to throw an
// exception here, instead of recovering by appending incoming // exception here, instead of recovering by appending incoming
// onto the existing array. // onto the existing array.
res = [...merged, ...incoming]; res = [...merged, ...previous];
// eslint-disable-next-line no-underscore-dangle // eslint-disable-next-line no-underscore-dangle
res = uniqBy(res, (elem: any) => elem.__ref); res = uniqBy(res, (elem: any) => elem.__ref);
} }