Fix apollo pagination merging

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2021-06-10 09:41:27 +02:00
parent 1ac9b43a61
commit 5ea530a13f
No known key found for this signature in database
GPG Key ID: A061B9DDE0CA0773

View File

@ -16,6 +16,7 @@ import { IMember } from "@/types/actor/member.model";
import { IComment } from "@/types/comment.model"; import { IComment } from "@/types/comment.model";
import { IEvent } from "@/types/event.model"; import { IEvent } from "@/types/event.model";
import { IActivity } from "@/types/activity.model"; import { IActivity } from "@/types/activity.model";
import uniqBy from "lodash/uniqBy";
type possibleTypes = { name: string }; type possibleTypes = { name: string };
type schemaType = { type schemaType = {
@ -124,10 +125,6 @@ export function pageLimitPagination<T = Reference>(
// eslint-disable-next-line @typescript-eslint/ban-ts-comment // eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore // @ts-ignore
merge(existing, incoming, { args }) { merge(existing, incoming, { args }) {
console.log("pageLimitPagination");
console.log("existing", existing);
console.log("incoming", incoming);
// console.log("args", args);
if (!incoming) return existing; if (!incoming) return existing;
if (!existing) return incoming; // existing will be empty the first time if (!existing) return incoming; // existing will be empty the first time
@ -144,9 +141,6 @@ export function paginatedLimitPagination<T = Paginate<any>>(
// eslint-disable-next-line @typescript-eslint/ban-ts-comment // eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore // @ts-ignore
merge(existing, incoming, { args }) { merge(existing, incoming, { args }) {
console.log("paginatedLimitPagination");
console.log("existing", existing);
console.log("incoming", incoming);
if (!incoming) return existing; if (!incoming) return existing;
if (!existing) return incoming; // existing will be empty the first time if (!existing) return incoming; // existing will be empty the first time
@ -168,7 +162,6 @@ function doMerge<T = any>(
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;
console.log("args, selected", { page, limit });
for (let i = 0; i < incoming.length; ++i) { for (let i = 0; i < incoming.length; ++i) {
merged[(page - 1) * limit + i] = incoming[i]; merged[(page - 1) * limit + i] = incoming[i];
} }
@ -179,7 +172,8 @@ function doMerge<T = any>(
// 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, ...incoming];
// eslint-disable-next-line no-underscore-dangle
res = uniqBy(res, (elem: any) => elem.__ref);
} }
console.log("doMerge returns", res);
return res; return res;
} }