|
import { FieldPolicy } from "@apollo/client"; |
|
|
|
type KeyArgs = FieldPolicy<any>["keyArgs"]; |
|
|
|
interface Response { |
|
data: any[]; |
|
message?: string; |
|
status: boolean; |
|
} |
|
|
|
export function offsetLimitPagination(keyArgs: KeyArgs = false): FieldPolicy { |
|
return { |
|
keyArgs, |
|
merge(existing = {} as Response, incoming: Response, { args }) { |
|
const merged = existing?.data ? existing?.data?.slice(0) : []; |
|
|
|
if (incoming) { |
|
if (args) { |
|
|
|
const { offset = 0 } = args?.input; |
|
|
|
for (let i = 0; i < incoming?.data?.length; ++i) { |
|
merged[offset + i] = incoming?.data[i]; |
|
} |
|
} else { |
|
|
|
|
|
|
|
|
|
merged.push.apply(merged, incoming?.data); |
|
} |
|
} |
|
|
|
return { |
|
...incoming, |
|
data: merged, |
|
}; |
|
}, |
|
}; |
|
} |
|
|