diff --git a/src/cache/inmemory/policies.ts b/src/cache/inmemory/policies.ts index f0c98ee069b..23e5508e8ea 100644 --- a/src/cache/inmemory/policies.ts +++ b/src/cache/inmemory/policies.ts @@ -186,7 +186,7 @@ export type FieldMergeFunction = ( // reasons discussed in FieldReadFunction above. incoming: SafeReadonly, options: FieldFunctionOptions, -) => TExisting; +) => SafeReadonly; export const defaultDataIdFromObject = ( { __typename, id, _id }: Readonly, diff --git a/src/utilities/index.ts b/src/utilities/index.ts index cd749e8ed4b..6c905bbb215 100644 --- a/src/utilities/index.ts +++ b/src/utilities/index.ts @@ -63,5 +63,6 @@ export { } from './graphql/transform'; export { + concatPagination, offsetLimitPagination, } from './policies/pagination'; diff --git a/src/utilities/policies/pagination.ts b/src/utilities/policies/pagination.ts index 30f54413dba..e30d617c0c4 100644 --- a/src/utilities/policies/pagination.ts +++ b/src/utilities/policies/pagination.ts @@ -2,6 +2,22 @@ import { FieldPolicy, Reference } from '../../cache'; type KeyArgs = FieldPolicy["keyArgs"]; +// A very basic pagination field policy that always concatenates new +// results onto the existing array, without examining options.args. +export function concatPagination( + keyArgs: KeyArgs = false, +): FieldPolicy { + return { + keyArgs, + merge(existing, incoming) { + return existing ? [ + ...existing, + ...incoming, + ] : incoming; + }, + }; +} + // A basic field policy that uses options.args.{offset,limit} to splice // the incoming data into the existing array. If your arguments are called // something different (like args.{start,count}), feel free to copy/paste