apollo-client
apollo-client copied to clipboard
useLazyQuery does not populate "error" field if errorPolicy is "all"
Issue Description
QueryResult.errors was recently deprecated in https://github.com/apollographql/apollo-client/pull/11954, with message "Please use error.graphQLErrors instead." But this field is not consistently populated. If a graph resolver throws, the errors property is always set, but error appears to be only populated if errorPolicy is "none".
Link to Reproduction
https://codesandbox.io/p/devbox/snowy-sky-ypkmvp?workspaceId=125b734b-01fa-4547-907b-915bf55739e9
Reproduction Steps
Click on "Load All People" in linked sandbox. Observe that console log of the result has an undefined error field, but populated errors. Change errorPolicy to "none". Now both will be populated.
@apollo/client version
3.11.4
Thanks for the report @gbettencourt!
I can't promise we can look at this until after next week as the team will be gone to a conference, but we will try to take a look sometime after. Thanks for bringing this to our attention!
We ran into this also when changing server side client.query to use errorPolicy: 'all'. The errors field is set to the graphQLErrors but the error result key is undefined. This is inconsistent with the hooks and prevents us from being able to see stuff like protocolErrors, clientErrors, networkError, etc.
In the source code it says error is "The single Error object that is passed to onError and useQuery hooks, and is often thrown during manual client.query calls.".
Imo this should be returned in error also when doing client.query calls.
We were working on trying to set our default policy to all but it seems like there are some bugs/inconsistencies with this policy.
We also ran into this, and additionally, I found that if you call the executor function twice without awaiting the promise, where the first call fails and the second one doesn't, it will also not set the error field on the result of the one that fails.
loadAllPeople({
variables: { fail: true },
}).then((result) => {
// result.error is undefined
console.log("failed query result error", result.error);
});
loadAllPeople({
variables: { fail: false },
}).then((result) => {
console.log("success query result error", result.error);
});
If I call them sequentially by awaiting each of them, it works fine. If I call the failing one after the successful one, it also works fine.
Not entirely sure if it's related to the above, but it worked in 3.10.8, but doesn't since 3.11.0.
Here is a reproduction of this case: https://codesandbox.io/p/devbox/relaxed-tdd-y97ftn
Hey @gbettencourt 👋
We've done a lot of work in the 4.0 branch related to errors and this issue is fixed there. We've removed errors and now put everything on an error field which should provide a lot more consistency for error handling. We've done a big overhaul to our test suite as well to ensure these types of things won't go unnoticed in the future.
If you want to try this out, we released the first 4.0 alpha so please check out the changelog! Since this code change has been merged and released, I'm going to go ahead and close this issue. Please open a new issue if you're experiencing issues with the alpha version related to this change. Thanks!
Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo Client usage and allow us to serve you better.
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. For general questions, we recommend using our Community Forum or Stack Overflow.