apollo-client: fetchMore's updateQuery option receives the wrong variables
The updateQuery
method passed to ObservableQuery.fetchMore
is receiving the original query variables instead of the new variables that it used to fetch more data.
This is problematic not so much because it breaks the updateQuery
method contract (which is bad, but clearly doesn’t seem to impact many people), but rather because when the results from fetchMore
are written to the store in writeQuery
, the old variables are used. This cases issues, say, if you were using an @include
directive and the variable driving its value changes.
Repro is available here: https://github.com/jamesreggio/react-apollo-error-template/tree/repro-2499
Expected behavior
nextResults.variables
shows petsIncluded: true
and the pets
list gets displayed.
Actual behavior
nextResults.variables
shows petsIncluded: null
and the pets
list is not displayed because the write never happens due to the @include
directive being evaluated with the old variables.
Version
- apollo-client@2.0.1
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 30
- Comments: 53 (13 by maintainers)
Commits related to this issue
- HACK to workaround apollographql/apollo-client#2499 — committed to jamesreggio/apollo-client by jamesreggio 7 years ago
- HACK to workaround apollographql/apollo-client#2499 — committed to jamesreggio/apollo-client by jamesreggio 7 years ago
Here’s a GIF to keep this open.
@jbaxleyiii any update on this at all please?
Kind regards,
I think I’ve run into this same issue while trying to do pagination. I’m trying to do
where
variables
come fromdata.variables
but those never seem to get updated, they are always the initial variables. This causes the code to always attempt to fetch the second page.It’s 2021 and I am having this issue too. My work around was to use a ref to keep track of the cursor
I had this problem and found a workaround for the issue. To give you some context: I was using React and had a button to fetch more posts inside a Query component using cursor-based pagination.
Instead of calling the fetchMore function received from the Query component, I would:
const res = await client.query({ query: getPosts, variables: { ...differentVariables })
const prev = client.readQuery({ query: getPosts, variables: { ...myVariables })
const updatedData = { ...prev, posts: [...prev.posts, ...res.data.posts] } client.writeQuery({ query: getPosts, variables: { ...myVariables }, data: updatedData })
This worked flawlessly for me.
same here, I’m using
apollo-client@2.2.8
Any chances to fix that 5-months old issue? It seems to be quite significant disfunction. A bit funny considering an activity of Apollo team on various React confs and theirs effort to promote Apollo integrations…
This issue still happening on
"apollo-client": "^2.3.7"
In case this might interest someone, I’m currently working around this by passing the necessary paging parameters combined with an updater function from the child component calling the
fetchMore
, something like this:edit: What I initially assumed would work is this:
I have the same issue also after upgrading to apollo-client@^2.3.2
Any update ? I use fetchMore function to create an infinite scroll with the FlatList Component (React Native). As @jamesreggio said, the
variables
seems not be updated and my infinite scroll turns into an infinite loop of the same query result.I also notice that
queryVariables
is null inside theupdateQuery
callback.List of the packages I use :
There is a workaround for this… or maybe the solution
fetchPolicy
as default@connection directive
to the field in your query so that apollo can identify which to concat…like below…
Still having a similar issue:
I call this function when reaching the bottom of the page.
It works perfectly when the search variable remains as “”, when reaching the bottom the skip variable changes and new data is refetched and works great.
However when i change the search variable and refetch the original query, and then try and paginate, i get an “ObservableQuery with this id doesn’t exist: 5” error, I had a look in the source code/added some logs and it seems that apollo is using the old queryId (from the query where the search variable was “”) for the fetchMore. I can see the data being successfully fetched in the network with all the correct variables but saving it back to the cache seems to be where it’s erroring, am i missing something with this implementation?
Note: I am using react-apollo-hooks, which i know isn’t production ready or even provided by apollo-client right now, but from what i’ve seen it looks like its something to do with the fetchMore API
Note: @FunkSoulNinja solution works for me, however would be nice to be able to use the provided API for this kind of feature
@MarttiR You could also use the client object from the Query component render prop function without having to use the withApollo HOC.
This is still an issue on
2.4.13
. Could @hwillson consider reopening this?The workaround by @FunkSoulNinja above works, but becomes convoluted – here’s a more complete example of how it goes together:
Also heres the fix, test pass and nextResult.variables is correct https://github.com/apollographql/apollo-client/pull/3500
still got this issue… hard to do pagination with refetch now
@jamesreggio as always, thank you for the thoughtful research and repo! I’ll get on it ASAP!