queryVariables returned by updateQuery contain stale values
See original GitHub issueWhile implementing pagination for a graphQL+React app, the updateQuery
method passed as an option to fetchMore
also contains queryVariables
as an argument, which are returned by fetchMore
. However, queryVariables
contain the variables for the original query, not the updated ones passed to fetchMore
. This seems counter-intuitive, as one would expect the updated variables to be returned by the query.
Here is a replication of the code in the Githunt example:
const withData = graphql(FEED_QUERY, {
options: props => ({
variables: {
type: (
props.params &&
props.params.type &&
props.params.type.toUpperCase()
) || 'TOP',
offset: 0,
limit: ITEMS_PER_PAGE,
},
forceFetch: true,
}),
props: ({ data: { loading, feed, currentUser, fetchMore } }) => ({
loading,
feed,
currentUser,
fetchMore: () => fetchMore({
variables: {
offset: feed.length,
},
updateQuery: (prev, { fetchMoreResult, queryVariables }) => {
// Here, queryVariables contains {offset: 0, limit: 10}
// even though the value of offset is updated above and should be equal to feed.length.
if (!fetchMoreResult.data) { return prev; }
return Object.assign({}, prev, {
feed: [...prev.feed, ...fetchMoreResult.data.feed],
});
},
}),
}),
});```
Issue Analytics
- State:
- Created 7 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Refetching queries in Apollo Client - Apollo GraphQL Docs
The InMemoryCache helps you determine which active queries might have been invalidated by recent cache updates. Local cache updates and refetching work ...
Read more >JPA JQL query after bulk UPDATE sees stale values
So it will perform a full list operation, but when it sees an Emp id it already has cached/managed, just returns that instance...
Read more >SQL error messages and exceptions - Oracle Help Center
VALUES clause must contain at least one element. Empty elements are not allowed. 42X81, A query expression must return at least one column....
Read more >MySQL 8.0 Reference Manual :: 9.4 User-Defined Variables
If the value of a user variable is selected in a result set, it is returned to the client as a string. If...
Read more >Executing SQL commands with executeUpdate() or through ...
executeQuery() was designed to execute query statements so it returns a ResultSet that contains the data returned by the query. The Statement class...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks for your comments @helfer. I tried both react-apollo 0.8.3 and the most recent release of 1.1.1 and both have the same issue of having queryVariables returning the original data. I was able to get the variables passed to fetchMore in the closure. I would think it might be better not having queryVariables at all since both the original variables and the ones used in fetchMore are available in updateQuery scope. I will add a short description to the use-cases and how I handled the scenario later today. Thanks again!
@HeatherZh I think this may be due to a bug. The variables you get should be the variables of the base query at the time the fetchMore request was made. Is that not the case?
As for knowing which fetchMore result is being returned, I think using a closure is probably the easiest and most flexible option. You could technically also use that to know what the query variables were at the time fetchMore was called (unless I’m missing something).
Your use case seems to be a common scenario for working with timeline data, so we’d like to make it as easy as possible. Could you add a short description to the use-cases issue on react-apollo so we can keep track of it? Thanks!