Skip to content

Commit

Permalink
Implement a naive concatPagination field policy function.
Browse files Browse the repository at this point in the history
This pagination helper policy is flawed because it does not consider any
field arguments, but it seems useful for AC3 migration purposes, because
it does exactly what a typical fetchMore updateQuery function would do,
just a little more reliably.
  • Loading branch information
benjamn committed Jun 22, 2020
1 parent 9281bb8 commit 616b518
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/cache/inmemory/policies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ export type FieldMergeFunction<TExisting = any, TIncoming = TExisting> = (
// reasons discussed in FieldReadFunction above.
incoming: SafeReadonly<TIncoming>,
options: FieldFunctionOptions,
) => TExisting;
) => SafeReadonly<TExisting>;

export const defaultDataIdFromObject = (
{ __typename, id, _id }: Readonly<StoreObject>,
Expand Down
1 change: 1 addition & 0 deletions src/utilities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,6 @@ export {
} from './graphql/transform';

export {
concatPagination,
offsetLimitPagination,
} from './policies/pagination';
16 changes: 16 additions & 0 deletions src/utilities/policies/pagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@ import { FieldPolicy, Reference } from '../../cache';

type KeyArgs = FieldPolicy<any>["keyArgs"];

// A very basic pagination field policy that always concatenates new
// results onto the existing array, without examining options.args.
export function concatPagination<T = Reference>(
keyArgs: KeyArgs = false,
): FieldPolicy<T[]> {
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
Expand Down

0 comments on commit 616b518

Please sign in to comment.