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

allow querying connection metadata and then subsequent data in relayStylePagination #6935

Merged
merged 4 commits into from
Sep 9, 2020
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
65 changes: 65 additions & 0 deletions src/cache/inmemory/__tests__/__snapshots__/policies.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -765,3 +765,68 @@ Object {
},
}
`;

exports[`type policies field policies can handle Relay-style pagination without args 1`] = `
Object {
"ROOT_QUERY": Object {
"__typename": "Query",
"todos": Object {
"edges": Array [
Object {
"__typename": "TodoEdge",
"cursor": "YXJyYXljb25uZWN0aW9uOjI=",
"node": Object {
"__ref": "Todo:1",
},
},
],
"pageInfo": Object {
"__typename": "PageInfo",
"endCursor": "YXJyYXljb25uZWN0aW9uOjI=",
"hasNextPage": true,
"hasPreviousPage": false,
"startCursor": "YXJyYXljb25uZWN0aW9uOjI=",
},
"totalCount": 1292,
},
},
"Todo:1": Object {
"__typename": "Todo",
"id": "1",
"title": "Fix the tests",
},
}
`;

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",
},
}
`;
176 changes: 176 additions & 0 deletions src/cache/inmemory/__tests__/policies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2177,6 +2177,182 @@ describe("type policies", function () {
});
});

itAsync("can handle Relay-style pagination without args", (resolve, reject) => {
const cache = new InMemoryCache({
addTypename: false,
typePolicies: {
Query: {
fields: {
todos: relayStylePagination(),
},
},
},
});

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

const secondQuery = gql`
query TodoQuery {
todos(after: $after, first: $first) {
pageInfo {
__typename
hasNextPage
endCursor
}
totalCount
edges {
__typename
node {
__typename
id
title
}
}
}
}
`

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

const secondVariables = {
first: 1,
};

const secondEdges = [
{
__typename: "TodoEdge",
node: {
__typename: "Todo",
id: '1',
title: 'Fix the tests'
}
},
];

const secondPageInfo = {
__typename: "PageInfo",
endCursor: "YXJyYXljb25uZWN0aW9uOjI=",
hasNextPage: true,
};

const link = new MockLink([
{
request: {
query: firstQuery,
},
result: {
data: {
todos: {
totalCount: 1292
}
}
}
},
{
request: {
query: secondQuery,
variables: secondVariables,
},
result: {
data: {
todos: {
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: firstQuery}).then(result => {
expect(result).toEqual({
loading: false,
networkStatus: NetworkStatus.ready,
data: {
todos: {
totalCount: 1292
}
}
})

expect(cache.extract()).toEqual({
ROOT_QUERY: {
__typename: "Query",
todos: {
edges: [],
pageInfo: {
"endCursor": "",
"hasNextPage": true,
"hasPreviousPage": false,
"startCursor": "",
},
totalCount: 1292
},
}
});

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

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

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()
})
})
})
})

itAsync("can handle Relay-style pagination", (resolve, reject) => {
const cache = new InMemoryCache({
addTypename: false,
Expand Down
16 changes: 7 additions & 9 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,9 +79,7 @@ export function relayStylePagination<TNode = Reference>(
},

merge(existing = makeEmptyData(), incoming, { args }) {
if (!args) return existing; // TODO Maybe throw?

const incomingEdges = incoming.edges.slice(0);
const incomingEdges = incoming.edges ? incoming.edges.slice(0) : [];
if (incoming.pageInfo) {
updateCursor(incomingEdges, 0, incoming.pageInfo.startCursor);
updateCursor(incomingEdges, -1, incoming.pageInfo.endCursor);
Expand All @@ -90,17 +88,17 @@ export function relayStylePagination<TNode = Reference>(
let prefix = existing.edges;
let suffix: typeof prefix = [];

if (args.after) {
if (args && 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 && args.before) {
const index = prefix.findIndex(edge => edge.cursor === args.before);
suffix = index < 0 ? prefix : prefix.slice(index);
prefix = [];
} else {
} else if (incoming.edges) {
// 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.
Expand All @@ -114,14 +112,14 @@ export function relayStylePagination<TNode = Reference>(
];

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

const updatePageInfo = (name: keyof TInternalRelay<TNode>["pageInfo"]) => {
const value = incoming.pageInfo[name];
const value = incoming.pageInfo && incoming.pageInfo[name];
if (value !== void 0) {
(pageInfo as any)[name] = value;
}
Expand Down