-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
RefetchQueries after mutation not updating UI, yet network tab shows new data #3633
Comments
was able to solve by following this: |
@kkotwal94 i'm seeing this same issue even though there were not any errors with the initial query |
@stephenhandley maybe i could post some code to help with this, i don't know if it'll solve your issue. Initally, this was a apollo error in the query if I wasn't logged in (because, im not logged in 👍 ), so refetching didn't work. Look at the export at the bottom and I had to do that. The query name isAuthenticated gets refetched from my login component rerendering all my components using that same query. Header.jsx
Login.jsx:
I omitted a lot from the second file, but i think that was all that was needed to be shown (let me know if you want to see the whole file). |
I am also getting this same error. The solution suggested by @kkotwal94 did not work for me. |
can you be a bit more specific, post some code maybe, the exact scenario, so we can disect it, maybe find the true root of the error? |
@kkotwal94 Following the link your posted, it seems that it was problem in the As for my issue, I verified that the incoming GQL data was updated. It's just the component that was not updating. |
Im guessing this component is trying to fetch data? (So a query?) in my case it works for me so im trying to figure out the difference. If it was a mutation thats updating data then are you updating the cache (if you are manipulating the cache) or are you doing a refetch? |
Yeah I'm not seeing an error in my queries. Basically a mutation is run and I can verify via the chrome network tab that the updated data is arriving, however even with refetchQueries set, the component is still displaying stale data. |
@kkotwal94 I am not doing query. I am doing a mutation and have the same problem as @stephenhandley. |
and the query thats being refetched has this option?
|
neither of the suggestions mentioned in apollographql/react-apollo#2070 (changing |
🤷♂️ Maybe we can compare versions and setup? |
here's relevant apollo/react libs I'm using
|
Same for me. I have a complex query (custom sorting based on several options) so updating the store manually is not an option, unfortunately. My mutation is successful, but when I run Edit: Looks like I can get it to update the UI with an explicit call to |
I had a simple, client side query. When i modified the data model to no longer update an @client property, I got no results back from the query. I was not using a schema for the client properties. When i removed that property from the query, all my results came back. |
@hwillson , i think it is realted to apollographql/react-apollo#2070 |
Any update on this issue? This is breaking my app right now. This issue isn't fixed in |
I have the same problem as @Auchindoun. An explicit refetch works but |
This is a huge blocker bug for our app too. Edit: it DOES seem to work if i use a function. So replacing
with
does update the UI for me properly with the previous variables with which getBookings had been called. I'm using the graphql HOC btw. |
Nobody from from apollo team cares
…On Tue, Sep 4, 2018, 8:38 PM sandorvasas ***@***.***> wrote:
This is a huge blocker bug for our app too.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#3633 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAK0caVZuUMwL7UjM7aSkhCBocuncROcks5uXnRAgaJpZM4U8aBZ>
.
|
@iamrommel We care! If someone here can provide a small, runnable reproduction that clearly demonstrates this issue, that would greatly help with getting this resolved. |
@hwillson, it's been two months and this issue has not been closed, if you really cares, you should check it out on your testing lab and verify it, If you cannot reproduce this, then close this issue so it wont be a clutter. i dare you to close this issue.. :) |
Oh, their documentation is super misleading. But their documentation DOES state that https://www.apollographql.com/docs/react/api/react-apollo/#optionsfetchpolicy:
|
I mean that's what I said here, #3633 (comment):
Oh, Apollo has bugs with IE for sure. To your other point, I never claimed anyone was stupid. I tried to simply explain the inner working of the complex caching logic that Apollo uses. Nothing I said in my comment was incorrect. People are getting angry because I'm simply explaining the system to them. I didn't write the system, I'm just explaining that this is the INTENDED behavior from the Apollo client. The correctness of their intentions is up for debate, but the facts I stated in my comment are not. Facts:
People are excited about this comment here: #3633 (comment). But it's quite literally the concepts I previously explained. |
OK,
Troubleshooting steps for your situation:
A. Familiarize yourself with Apollo's Making all other cache updates, specifically the B. Make sure to include your original variables in the 'update' method in every place you are using the original query used to get data. This stackoverflow answer was helpful. C. Try mutating your data while using the
Notice the data structure difference between these 2 lines:
As soon as I eliminated that difference in the original variables, the |
I have been having the same issue. I have spent 4 days debugging this but to no avail. The Network shows correct data, meaning the mutation is running and the refetch query is running and the correct data is coming in. However the UI is not rerendering to reflect it. I am now considering completely dumping Apollo 3.0 which has gone from Beta to official and making GraphQL queries with simply Axios or Fetch. Such a large buggy unwieldy bunch of functionality is like a prior day dinasaur reincarnated. Apollo says we need caching and so forth, yes but before that our more important need is that simple things like RefetchQueries work out of the box. Currently this thread shows, even after the release of Apollo3.0 official, they dont work. A huge amount of time is wasted on finagling a feature that should just work to work. And if it still doesnt work, the whole library has to be dumped. God knows by this thread so many people have tried to get it to work for so long. This after noon I am going to implement simple access to Graphql API using fetch or axios and bypass this whole mess. |
Can see your frustration. I could recommend trying a fix per my post above. In particular, the part that helps you reveal a possible miss-match between your variables and the keys in the cache. |
This also took me a long time to figure out, but as long as you call it with the same variables then it will work. It can be super frustrating but I don't think thats a reason to ditch apollo-client, it has a lot of other amazing features. |
Thanks for the response @Ruslan @johnny. However, please note the following
points:
1) The query that is being refetched is a simple list of items after a
delete mutation has run. There are no variables. If there are no variables
being passed to the query where is the question of there being same or
different variables.
2) I have even done an update method to evict the deleted item from the
local cache. The item is deleted from the local cache, however the query is
not refetched.
3) In my network requests, the refetched data shows correctly, with the
deleted item removed. I see 2 network requests, the first to delete and
item and the 2nd to show the revised list minus the deleted item, however
the UI does not update
So the refetch is working at the network request level, but it is most
strangely not updating the UI.
I have been stuck on this for many days. I am at a loss. Dont know what to
do further.
Best regards,
arjuna
…On Wed, Jul 22, 2020 at 8:57 AM Johnny Bell ***@***.***> wrote:
I have been having the same issue. I have spent 4 days debugging this but
to no avail....
This also took me a long time to figure out, but as long as you call it
with the same variables then it will work. It can be super frustrating but
I don't think thats a reason to ditch apollo-client, it has a lot of other
amazing features.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#3633 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AEVWN4TDPM22WG5VW3SZK63R4ZMBNANCNFSM4FHRUBMQ>
.
|
@brahmaforces I think the issue is clear now. In my past experience with Apollo Client I'm not aware of the very latest details there and you may need to make sure you are using the right docs for the version of the client you are using, but this link may be helpful as a starting point: https://www.apollographql.com/docs/react/caching/cache-interaction/ Addition: if you are willing to refetch the list on mutation like you described above, an alternative quick fix could be to abandon the use of UI cache for this query by setting |
Look like I have a specific case. For some clearence, I have student and teacher. Student should send some answer, then teacher should view list of answers. I fetch the time when student send answer and refetchQuery calls in case when there is already done one request, it works fine. But the problem when I completed mutation, then query shows old data. I'm using chromnium. It looks like that fetching a little old data. It works only if you run it on the first time. But if it run and has a cache, it means that you have no data in component and if it calls, it will return old data |
I had the same problem. There is definitely a bug in refetch queries. I
have tried everything. The network shows the correct data. The cache shows
the correct data. But the ui renders old data.
Best regards,
arjuna
…On Thu, Aug 6, 2020 at 8:27 PM Sabit Rakhim ***@***.***> wrote:
Look like I have a specific case. For some clearence, I have student and
teacher. Student should send some answer, then teacher should view list of
answers. I fetch the time when student send answer and refetchQuery calls
in case when there is already done one request, it works fine. But the
problem when I completed mutation, then query shows old data. I'm using
chromnium. It looks like that fetching a little old data.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#3633 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AEVWN4RS7FDH2YULOP7JIFTR7LAELANCNFSM4FHRUBMQ>
.
|
using AC3 and refetch() didn't tirgger update for me. waooo sad ..... :/ |
ok, 2020 here is my work around guys, instead of using
lets call fetchMore since fetchMore is working correctly, so we can pass the initial default variables, yeah sounds funny.
thanks 😄 |
The thing that fixed it for me was changing the const { data, error } = useGetProductsQuery({ |
To sum it up, what works here is to first define appropriate cache policies:
Second: make sure the |
I had this problem. I solved it by doing what @agileapplications did, but I didn't set a However, my issue was caused because I'm using apollo-client on the server, to query my Hasura db. It seemed that doing the above wasn't enough, so I had to set certain queries as Now I don't have to worry about making a change, constantly repeatedly refreshing the page and seeing no changes in the UI anymore. Edit 1: (May 8, 2021) |
Thanks all - looks like this issue has been resolved. |
On my side, variables are matching 100% but still, UI is not updating! 😐 |
Checklist when refetch queries is not working:
const { data } = useQuery(TODOS, {
variables: { first: 10 },
fetchPolicy: 'network-only', // Important!! Apollo will default use cache first
nextFetchPolicy: 'network-only' // Important!!
})
const [updateTodo] = useMutation(UPDATE_TODO, {
variables: { id: 1, title: 'Foo' },
// The variables NEEDS to match EXACTLY! Double check!
refetchQueries: [{ query: TODO, variables: { first: 10 } }]
}) Now, once your application is working correctly, you can start to think about reducing network requests. |
I have to add that none of the solutions mentioned here worked - fetchPolicy, variables ordering etc I ended up moving the logic from |
@palerdot your solution worked for me. Thanks for the link! |
FYI another simple thing you can do that worked for me—I didn't want to mess around with how we were refetching since it's pretty complex already, but while the apollo hook doesn't return a different object, it does cause a rerender where its called. And additionally, the query's function ParentComponent() {
const { data, refetch, loading } = useQuery(MY_QUERY, { notifyOnNetworkStatusChange: true, /* whatever */ });
const [loadCount, setLoadCount] = useState(0);
useEffect(() => {
if (loading === false) {
setLoadCount(value => value + 1);
}
}, [loading]);
// this will rerender every time a refetch happens
return <MyComponent loadCount={loadCount} />;
}
const MyComponent = memo(function MyComponentImplementation(props) {
return <div>Whatever</div>; // not actually using loadCount, but it does make it rerender.
}); |
I realised that simply keeping the state in a top level component and
updating the state will cause the component to rerender. Refetch is
wasteful because it makes another trip to the server.
…On Sun, Jul 17, 2022, 13:27 Omar Diab ***@***.***> wrote:
FYI another simple thing you can do that worked for me—I didn't want to
mess around with how we were refetching since it's pretty complex already,
but while the apollo hook doesn't return a different object, it does cause
a rerender where its called. And additionally, the query's loading state
does change. So a simple option is to just either A) pass the loading state
as a prop to whatever you want to have a rerender triggered on (would
render twice per refetch), or B) keep track of a loadCount that you can
pass whenever refetches complete to trigger the rerender for whatever is
failing to update. For example:
function ParentComponent() {
const { data, refetch, loading } = useQuery(/* whatever */);
const [loadCount, setLoadCount] = useState(0);
useEffect(() => {
if (loading === false) {
setLoadCount(value => value + 1);
}
}, [loading]);
// this will rerender every time a refetch happens
return <MyComponent loadCount={loadCount} />;
}
const MyComponent = memo(function Implementation(props) {
return <div>Whatever</div>
});
—
Reply to this email directly, view it on GitHub
<#3633 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AEVWN4WHYLXOXHZHA7BF5MTVUO4E5ANCNFSM4FHRUBMQ>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
@brahmaforces that depends on your use case, for ours we really did want to refetch and do a round trip to the server. regardless just a workaround that might be helpful to others. |
How has it been resolved? What's the solution? There's a hundred messages and after reading them all I don't see any solution. |
I was running into a similar issue - documenting my fix here although it looks like this wouldn't fix it for the original poster. TL;DR:Switching from refetchQueries: ['namedQuery'] to refetchQueries: [{ query: NAMED_QUERY_DOCUMENT }] fixed the issue for me. See this issue comment for more details. Longer description of issue to hopefully help someone fix this:In my case, we had a list query ( const [deleteGroup] = useDeleteGroupMutation({
variables: { id: group.id },
refetchQueries: ['groups'],
awaitRefetchQueries: true,
}); Worked with no issues. We also added an option to "undo" the group deletion. We'd store the group info, and on undo, would just create a new group with all the old info: const [createSiteGroup] = useCreateSiteGroupMutation({
refetchQueries: ['groups'],
awaitRefetchQueries: true,
});
// Actually deleting group
deleteSiteGroup().then(({ data: deleteData }) => {
const deletedGroup = deleteData!.deleteGroup;
const undo = async () => (
await createGroup({
variables: {
name: deletedGroup?.name,
},
})
);
// Call the undo somewhere conditionally, like in a toast notification
await undo();
}) That worked fine too. The old group got recreated (with all the same data except a new The issue was when we tried to delete that restored group - exact same code as above should have fired, and it did. A network request to delete the group was successful, and another followup to refetch the If we deleted a group, undid the delete, navigated away and came back (i.e. unmounted and remounted the observable query in the restored group), we could successfully delete the restored group and the UI would update. This definitely feels like an Apollo bug related to the The documentation is also lacking around this point. It fails to mention that a named query string will only refetch if it exists in mounted, observable queries, whereas passing an object with a query document will always run the query. This also fixed a separate, unrelated issue where we ran the undo and related |
But what to do when the query has some arguments? Using a string means that you shouldn't have to provide the same object as Apollo should automatically fetch the query with the current query variables. |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Intended outcome:
I'm trying to authenticate a user and have it update my navbar (which uses the same query) to show different options whether the user is logged in or not.
Actual outcome:
Whats actually happening is, after refetching (during the login mutation), im getting new data from my query, and the network tab is showing it, store seems updated, however the component itself is still rendering stale data (outputted via console.log). Doing a hard refresh fixes the issue (since we get new data).
How to reproduce the issue:
I made a little demo video:
https://drive.google.com/file/d/1Zmp1nwJBYnkuO0Cr2x4jUSnY61REkD8X/view
To explain the justification, i have a user query here that checks at a high level:
If the user isn't authed it returns null
In my login component i have it like this:
Versions
"react-apollo": "^2.1.4",
The text was updated successfully, but these errors were encountered: