-
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
useLazyQuery run after component rerender #7484
Comments
Similar issue here. UseLazyQuery seems to send a request to the server everytime my component re-renders.
|
Same here |
Also having this problem, fired lazyQuery, after it finishes, other queries run in sequence, some are even using old variables from previous calls. Seems to be a problem in cache handling, it works if I use the "no-cache" fetchPolicy. |
Yes same here. In my case, if I change fetchPolicy to "no-cache" and query runs, I have same old value. |
I believe I am experiencing this same issue. I have a After poking around a bit in |
I think we can use |
I am experiencing this as well, although, for some reason, only if the previous intended call of the lazy query returns an error. If the query resolves corretly and returns data, no follow up requests are made. |
Also having the same issue. useLazyQuery re-runs the query on component re-render. |
This issue is also happening to me and it seems a bug since the documentation states "The useLazyQuery hook is perfect for executing queries in response to events other than component rendering." I have tried every type of fetchPolicy without any success (even though it does not feel right because it still creates a new network request), the requests are always re-executed on component render resulting in data going back to undefined and then to the new request request (onCompleted is always re-executed as well). This totally invalidates the objective and usage of useLazyQuery. |
same issue here |
When does the 3.5 release go live? I have the same issue when using conditionally two different lazyQueries inside a |
@Pedro-Puga Are you confirming that this issue (or a separate issue you have) is fixed by 3.5? We’re planning on releasing 3.5 on the 27th, so if you’d like to try the beta by installing @apollo/client@beta, I would be eternally grateful! |
No... my comment was before trying your beta, I'm sorry, I tried using the |
I have also been having this problem but just discovered something that might be the cause. I have a React component that builds out the ApolloClient and creates all of its various links. I found that some of the links were not memoized and so were being rebuilt with each render, and also that I was incorrectly setting the dependencies for the |
I'm so sorry form my mistake (closing this issue ) . I'm very tired I guess :-) |
I guess all I'm trying to say is that while I don't know exactly why the problem occurs, it does seem to be triggered in my case by frequently rebuilding the |
Hey everyone! I had a call with @Pedro-Puga last week about As an alternative, you can use Another odd detail about |
Wow, thanks for that explanation @brainkim! I think it'd be really helpful to add to the documentation for |
But in previous version of Apollo, this issue was not exist.In my case, this useLazyQuery is launched only with click the button and variable is handed over v onClick handler.And this variable are not changed.Is it primitive value. Your explanation does not make sense or I dont understand useLazyQuery. Another thing is Apollo cache. Which is horible. |
@pronovaso - I think I had the same problem you described, which is why I found this issue. I'm not commenting as to whether this should be considered a bug or not. Instead I'll just provide the work-arounds that seem to resolve the issue for me. I am using Apollo Client v3.4.0, btw. I found two techniques to prevent the
That seems to work for me. (Previously I had it set up with
That seems to work too, but I don't like it as much as option 1 because it seems to rely too heavily on the query name which is otherwise pretty arbitrary. Hopefully this helps you. |
I totally agree with @pronovaso , this behaviour doesn't make sense with useLazyQuery. Moreover, this issue is not properly workarounded yet, apart from use useQuery instead with cumbersome "skip" logics... this issue has been open for an year now and I think it should be addressed ASAP... |
I have to fully support @manuFL @pronovaso stance on this. The expected behaviour of useLazyQuery is that requests are entirely manual and under the control of the application logic and not automatic in any way. Persisting with this odd pattern of behaviour and ignoring such a significant bug for this long after it has been identified does not inspire confidence in the library in general. |
Fully support @atomless , @manuFL @pronovaso and others. Is there an update to this? I feel like I must be missing something here. I don't understand how something as widely adopted as Apollo Client does not have a simple way to only fetch data on user event, as opposed to fetching data automatically every time the component renders. |
If you're just looking to fetch data once, I think you can do something like this: const client = useApolloClient();
const someEventHandler = async () => {
const queryResult = await client.query({ ...queryOptions });
// do something with queryResult
}; |
FWIW, I've found that this seems to be fixed in the 3.6 beta builds. I was also having a problem where useLazyQuery would seem to skip the cache and that seems to be fixed as well. I wish I had some idea about the release schedule for that version, but they're getting pretty high up in the beta release numbers and have been releasing betas for 5 months. |
Thanks for trying the betas @aub (and @dylanwulf and possibly others in this thread)! Those time windows would be shorter if more folks gave feedback (positive or negative) on the beta/RC releases. The good news is that we're on |
@benjamn FWIW I've been using 3.6 rc1 and it's working great while solving all of the issues I've had related to |
We still have this issue on A workaround we currently use is setting |
Sorry to revive this but would you mind providing an example? I seem to be bumping into something similar, but not quite sure how to tackle this. |
@aub an example of the above would be very helpful, thanks |
@okanji it was fixed for me in 3.6, as noted, so I'm happy. 😀 |
I am still facing this issue, please share if anyone have a good idea |
For me this temp workaround solved it for now: #5912 (comment) |
I know that a lot of time has passed since the beginning of this topic, but I thought that I also had this same problem, my solution was to pass the useLazyQuery variables to the execution function, I hope anyone else can be of help. |
Sooo, same issue !!! |
I have a convenient Solution for this.
Call the function |
For me it was enough to leave the variables immutable |
Hi all.
I switch to new version of Apollo client. Actually v 3.3.6.
I use useLazyQuery in component with useQuery and useMutation. If I clicked on button,which open confirm window,then I call useLazyQuery function called load({variables:{id}}), query runs and give me a data. Thats right.But after run some mutation and query for new data from useQuery, than useLazyQuery runs again with same id. But load function is on handleClick, which not fire and this id is only from this handleClick.
And I dont know why. One difference is, new inMemoryCache. When I use this from package apollo-inmemory cache and not from @apollo/client than everything is allright.
Can you someone explain me, what I doing wrong or maybe bug in InMemoryCache?
Thans and sorry for my english :-)
The text was updated successfully, but these errors were encountered: