Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve handling of falsy existing and/or incoming parameters in relayStylePagination #8733

Merged
merged 3 commits into from
Sep 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## Apollo Client 3.4.12 (not yet released)

### Bug Fixes

- Improve handling of falsy `existing` and/or `incoming` parameters in `relayStylePagination` field policy helper function. <br/>
[@bubba](https://github.com/bubba) and [@benjamn](https://github.com/benjamn) in [#8733](https://github.com/apollographql/apollo-client/pull/8733)

## Apollo Client 3.4.11

### Bug Fixes
Expand Down
60 changes: 60 additions & 0 deletions src/utilities/policies/__tests__/relayStylePagination.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,66 @@ describe('relayStylePagination', () => {
});
});

it('should preserve existing if incoming is null', () => {
const existingEdges = [
{ cursor: 'alpha', node: makeReference("fakeAlpha") },
];

const fakeExisting = {
edges: existingEdges,
pageInfo: {
hasPreviousPage: false,
hasNextPage: true,
startCursor: 'alpha',
endCursor: 'alpha'
},
};

const fakeIncoming = null;

const fakeOptions = {
...options,
args: {
after: 'alpha',
},
};

const result = merge(
fakeExisting,
fakeIncoming,
fakeOptions,
);

expect(result).toEqual(fakeExisting);
})

it('should replace existing null with incoming', () => {
const incomingEdges = [
{ cursor: 'alpha', node: makeReference("fakeAlpha") },
];
const incoming = {
edges: incomingEdges,
pageInfo: {
hasPreviousPage: false,
hasNextPage: true,
startCursor: 'alpha',
endCursor: 'alpha'
},
};
const result = merge(
null,
incoming,
{
...options,
args: {
after: 'alpha',
},
},
);

expect(result).toEqual(incoming);
})

it('should maintain extra PageInfo properties', () => {
const existingEdges = [
{ cursor: 'alpha', node: makeReference("fakeAlpha") },
Expand Down
18 changes: 13 additions & 5 deletions src/utilities/policies/pagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ export type TIncomingRelay<TNode> = {
};

export type RelayFieldPolicy<TNode> = FieldPolicy<
TExistingRelay<TNode>,
TIncomingRelay<TNode>,
TIncomingRelay<TNode>
TExistingRelay<TNode> | null,
TIncomingRelay<TNode> | null,
TIncomingRelay<TNode> | null
>;

// As proof of the flexibility of field policies, this function generates
Expand All @@ -95,7 +95,7 @@ export function relayStylePagination<TNode = Reference>(
keyArgs,

read(existing, { canRead, readField }) {
if (!existing) return;
if (!existing) return existing;

const edges: TRelayEdge<TNode>[] = [];
let firstEdgeCursor = "";
Expand Down Expand Up @@ -133,7 +133,15 @@ export function relayStylePagination<TNode = Reference>(
};
},

merge(existing = makeEmptyData(), incoming, { args, isReference, readField }) {
merge(existing, incoming, { args, isReference, readField }) {
if (!existing) {
existing = makeEmptyData();
}
Comment on lines +137 to +139

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏


if (!incoming) {
return existing;
}

const incomingEdges = incoming.edges ? incoming.edges.map(edge => {
if (isReference(edge = { ...edge })) {
// In case edge is a Reference, we read out its cursor field and
Expand Down