From 8e92be3e8b791187d8557fccd30ffc82feac5cd4 Mon Sep 17 00:00:00 2001 From: Ben Newman Date: Sat, 10 Oct 2020 13:21:24 -0400 Subject: [PATCH] Default args.offset to 0 in offsetLimitPagination. (#7141) https://github.com/apollographql/apollo-client/issues/7135#issuecomment-706415063 --- CHANGELOG.md | 7 +++++++ src/utilities/policies/pagination.ts | 16 ++++++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 88efbefa97e..a253ec9830f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## Apollo Client 3.2.3 + +## Improvements + +- Default `args.offset` to zero in `offsetLimitPagination`.
+ [@benjamn](https://github.com/benjamn) in [#7141](https://github.com/apollographql/apollo-client/pull/7141) + ## Apollo Client 3.2.2 ## Bug Fixes diff --git a/src/utilities/policies/pagination.ts b/src/utilities/policies/pagination.ts index 66f3429558c..8b7d8a23959 100644 --- a/src/utilities/policies/pagination.ts +++ b/src/utilities/policies/pagination.ts @@ -31,10 +31,18 @@ export function offsetLimitPagination( keyArgs, merge(existing, incoming, { args }) { const merged = existing ? existing.slice(0) : []; - const start = args ? args.offset : merged.length; - const end = start + incoming.length; - for (let i = start; i < end; ++i) { - merged[i] = incoming[i - start]; + if (args) { + // Assume an offset of 0 if args.offset omitted. + const { offset = 0 } = args; + for (let i = 0; i < incoming.length; ++i) { + merged[offset + i] = incoming[i]; + } + } else { + // It's unusual (probably a mistake) for a paginated field not + // to receive any arguments, so you might prefer to throw an + // exception here, instead of recovering by appending incoming + // onto the existing array. + merged.push.apply(merged, incoming); } return merged; },