Skip to content

Commit

Permalink
dont overwrite page info when args are not passed to relayStylePagina…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
anark committed Sep 3, 2020
1 parent 9c825de commit a19f732
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 25 deletions.
33 changes: 33 additions & 0 deletions src/cache/inmemory/__tests__/__snapshots__/policies.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -797,3 +797,36 @@ Object {
},
}
`;

exports[`type policies field policies can handle Relay-style pagination without args 2`] = `
Object {
"ROOT_QUERY": Object {
"__typename": "Query",
"todos": Object {
"edges": Array [
Object {
"__typename": "TodoEdge",
"cursor": "YXJyYXljb25uZWN0aW9uOjI=",
"node": Object {
"__ref": "Todo:1",
},
},
],
"extraMetaData": "extra",
"pageInfo": Object {
"__typename": "PageInfo",
"endCursor": "YXJyYXljb25uZWN0aW9uOjI=",
"hasNextPage": true,
"hasPreviousPage": false,
"startCursor": "YXJyYXljb25uZWN0aW9uOjI=",
},
"totalCount": 1293,
},
},
"Todo:1": Object {
"__typename": "Todo",
"id": "1",
"title": "Fix the tests",
},
}
`;
66 changes: 51 additions & 15 deletions src/cache/inmemory/__tests__/policies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2189,15 +2189,15 @@ describe("type policies", function () {
},
});

const initialQuery = gql`
const firstQuery = gql`
query TodoQuery {
todos {
totalCount
}
}
`

const query = gql`
const secondQuery = gql`
query TodoQuery {
todos(after: $after, first: $first) {
pageInfo {
Expand All @@ -2218,11 +2218,20 @@ describe("type policies", function () {
}
`

const firstVariables = {
const thirdQuery = gql`
query TodoQuery {
todos {
totalCount
extraMetaData
}
}
`

const secondVariables = {
first: 1,
};

const firstEdges = [
const secondEdges = [
{
__typename: "TodoEdge",
node: {
Expand All @@ -2233,7 +2242,7 @@ describe("type policies", function () {
},
];

const firstPageInfo = {
const secondPageInfo = {
__typename: "PageInfo",
endCursor: "YXJyYXljb25uZWN0aW9uOjI=",
hasNextPage: true,
Expand All @@ -2242,7 +2251,7 @@ describe("type policies", function () {
const link = new MockLink([
{
request: {
query: initialQuery,
query: firstQuery,
},
result: {
data: {
Expand All @@ -2254,24 +2263,37 @@ describe("type policies", function () {
},
{
request: {
query,
variables: firstVariables,
query: secondQuery,
variables: secondVariables,
},
result: {
data: {
todos: {
edges: firstEdges,
pageInfo: firstPageInfo,
edges: secondEdges,
pageInfo: secondPageInfo,
totalCount: 1292,
}
}
},
},
{
request: {
query: thirdQuery,
},
result: {
data: {
todos: {
totalCount: 1293,
extraMetaData: 'extra',
}
}
},
}
]).setOnError(reject);

const client = new ApolloClient({ link, cache });

client.query({query: initialQuery}).then(result => {
client.query({query: firstQuery}).then(result => {
expect(result).toEqual({
loading: false,
networkStatus: NetworkStatus.ready,
Expand All @@ -2298,21 +2320,35 @@ describe("type policies", function () {
}
});

client.query({query, variables: firstVariables}).then(result => {
client.query({query: secondQuery, variables: secondVariables}).then(result => {
expect(result).toEqual({
loading: false,
networkStatus: NetworkStatus.ready,
data: {
todos: {
edges: firstEdges,
pageInfo: firstPageInfo,
edges: secondEdges,
pageInfo: secondPageInfo,
totalCount: 1292,
}
}
})

expect(cache.extract()).toMatchSnapshot()
resolve()

client.query({query: thirdQuery}).then(result => {
expect(result).toEqual({
loading: false,
networkStatus: NetworkStatus.ready,
data: {
todos: {
totalCount: 1293,
extraMetaData: 'extra',
}
}
})
expect(cache.extract()).toMatchSnapshot()
resolve()
})
})
})
})
Expand Down
19 changes: 9 additions & 10 deletions src/utilities/policies/pagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type TInternalRelay<TNode> = Readonly<{
// anything about connections, edges, cursors, or pageInfo objects.
export function relayStylePagination<TNode = Reference>(
keyArgs: KeyArgs = false,
): FieldPolicy<TInternalRelay<TNode>> {
): FieldPolicy<TInternalRelay<TNode>, Partial<TInternalRelay<TNode>>> {
return {
keyArgs,

Expand All @@ -79,42 +79,41 @@ export function relayStylePagination<TNode = Reference>(
},

merge(existing = makeEmptyData(), incoming, { args }) {
if (!args) return {...existing, ...incoming};

const incomingEdges = (incoming.edges || []).slice(0);
if (incoming.pageInfo) {
const incomingEdges = incoming.edges?.slice(0);
if (incoming.pageInfo && incomingEdges) {
updateCursor(incomingEdges, 0, incoming.pageInfo.startCursor);
updateCursor(incomingEdges, -1, incoming.pageInfo.endCursor);
}

let prefix = existing.edges;
let suffix: typeof prefix = [];

if (args.after) {
if (args?.after) {
const index = prefix.findIndex(edge => edge.cursor === args.after);
if (index >= 0) {
prefix = prefix.slice(0, index + 1);
// suffix = []; // already true
}
} else if (args.before) {
} else if (args?.before) {
const index = prefix.findIndex(edge => edge.cursor === args.before);
suffix = index < 0 ? prefix : prefix.slice(index);
prefix = [];
} else {
// If we have neither args.after nor args.before, the incoming
// edges cannot be spliced into the existing edges, so they must
// replace the existing edges. See #6592 for a motivating example.
prefix = [];
if(incomingEdges)
prefix = [];
}

const edges = [
...prefix,
...incomingEdges,
...(incomingEdges || []),
...suffix,
];

const pageInfo = {
...incoming.pageInfo,
...(incoming.pageInfo || {}),
...existing.pageInfo,
startCursor: cursorFromEdge(edges, 0),
endCursor: cursorFromEdge(edges, -1),
Expand Down

0 comments on commit a19f732

Please sign in to comment.