Intended outcome:
Expecting results to come back when querying a relayStylePagination field for just totalCount. E.g:
query usersCount {
users {
totalCount
}
}
Actual outcome:
Both data and error come back as undefined after migrating to relayStylePagination from updateQuery.
How to reproduce the issue:
relayStylePagination setup like so:
new InMemoryCache({
typePolicies: {
Query: {
fields: {
users: relayStylePagination(['groupId']),
},
},
},
})
When querying for usersCount above, then data and error both come back as undefined. updateQuery was previously on each call, so it didn't apply to this query.
Versions
@apollo/client: 3.0.0
Ahh, I think I know what this is. The read function returns the result of makeEmptyData() by default, which is an object that only has edges and pageInfo. While the merge function does try to save any additional properties like totalCount that are received from the server, those extra properties are not present in the initial empty data, and anything undefined looks like a missing field when reading from the cache. I'll put together a PR to add totalCount: 0 to the makeEmptyData result, but if you have time to try making that change locally, that would help confirm this theory.
what is the purpose of makeEmptyData() on the read function? I'm finding it's causing problems in my react app where the makeEmptyData() is preventing queries from hitting the backend under certain circumstances (I believe it's priming the cache with empty results)
@fracmak I think that's actually the most elegant solution鈥攖hanks! See #6684 for a fix.
@benjamn relayStylePagination implementation assumes that query has edges and pageInfo defined, but the query may lack one or both of those. I have the same case as @WillSquire. A query like:
query usersCount {
users {
totalCount
}
}
It'll crash in merge on incoming.edges.slice(0) with "incoming.edges is undefined"
I patched it with:
diff --git a/node_modules/@apollo/client/utilities/utilities.cjs.js b/node_modules/@apollo/client/utilities/utilities.cjs.js
index 99cc06a..788e8d7 100644
--- a/node_modules/@apollo/client/utilities/utilities.cjs.js
+++ b/node_modules/@apollo/client/utilities/utilities.cjs.js
@@ -692,7 +692,7 @@ function relayStylePagination(keyArgs) {
var args = _a.args;
if (!args)
return existing;
- var incomingEdges = incoming.edges.slice(0);
+ var incomingEdges = (incoming.edges || []).slice(0);
if (incoming.pageInfo) {
updateCursor(incomingEdges, 0, incoming.pageInfo.startCursor);
updateCursor(incomingEdges, -1, incoming.pageInfo.endCursor);
@@ -716,7 +716,7 @@ function relayStylePagination(keyArgs) {
var edges = tslib.__spreadArrays(prefix, incomingEdges, suffix);
var pageInfo = tslib.__assign(tslib.__assign(tslib.__assign({}, incoming.pageInfo), existing.pageInfo), { startCursor: cursorFromEdge(edges, 0), endCursor: cursorFromEdge(edges, -1) });
var updatePageInfo = function (name) {
- var value = incoming.pageInfo[name];
+ var value = incoming.pageInfo ? incoming.pageInfo[name] : undefined;
if (value !== void 0) {
pageInfo[name] = value;
}
Also running into issues after querying for only totalCount. Tried this patch and version 3.2.0.beta-7 but still couldn't get reliable updates.
It seems if you first query without any edges/pageInfo
query usersCount {
users {
totalCount
}
}
And then query for the edges
query usersCount {
users {
totalCount
pageInfo{
hasNextPage
endCursor
}
edges {
node{
email
}
}
}
}
The users will always be an empty and return no data.
Using some of @binchik solution and still functioning without args https://github.com/apollographql/apollo-client/pull/6935