-
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
onComplete
not being called after doing a refetch
#11151
Comments
Hi @glenngijsberts 👋🏻 thanks for the report! The team will take a look at this as soon as we can, one of us is on vacation and two of us are traveling to a conference. I'm curious as to whether #10229 may have something to do with this, though it's a surface-level observation that will need to be validated. I can see the issue in your codesandbox but it appears that my fork isn't allowed to query your GraphQL server(?). I'm trying to validate whether adding |
@bignimbus thanks for your quick response 💯 I've added the If this is the way to go, I think it would be nice to clarify this in the docs maybe or mention it somewhere in the release notes 😄 Thanks for your help! |
I've noticed this behavior in 3.8 too and I agree it should be mentioned in the docs |
@glenngijsberts I've already created a similar ticket: |
Thanks all - I'm currently on mobile but will take a closer look once I'm at my laptop. We can definitely call this out in the docs 👍 |
Thanks all, looking forward to making these updates. @av-k I appreciate the cross-link of the issue. Some feedback: I’m sure your comment was meant to be helpful but it could be interpreted as calling a maintainer out in a way that I’m hoping you didn’t intend, just wanted to make you aware 🙏🏻 |
@bignimbus I just wanted to share some context about the already existing context of the known problem with extra details. A request from my side - please do not rush to close tickets in case even the author has not had time to answer your own question. |
I also noticed that updating variables in By updating the variables, in my case // https://rickandmortyapi.com/graphql
const GET_CHARACTERS = gql`
query Query($page: Int) {
characters(page: $page) {
info {
count
}
results {
name
}
}
}
`;
export function Characters() {
const [page, setPage] = useState(1);
const query = useQuery(GET_CHARACTERS, { variables: { page: 1 } }); // Won't call onComplete
// const query = useQuery(GET_CHARACTERS, { variables: { page } }); // Will call onComplete
return (
<>
<div>
{query.data?.characters?.results?.map((char: { name: string }) => (
<div>{char?.name}</div>
))}
</div>
<button
onClick={() => {
query.fetchMore({ variables: { page: page + 1 } });
setPage(page + 1);
}}
>
Next page
</button>
<div> Current page {page}</div>
</>
);
} |
We have a similar issue after updating to 3.8 but only with a unit test We have something like this: const { data, loading } = useQuery(
GET_CHARTS,
{
variables: {
id: chartId,
},
onCompleted,
fetchPolicy: 'no-cache',
}) When running the tests If I remove the Any idea why this might be happening? |
Can confirm - this is a bug that seems to be introduced with 3.8. Reverting back to 3.7.17 fixes it for us. |
Also confirming this is a bug that I've encountered when upgrading to 3.8. Gonna stay with 3.7. |
Hey everyone, I talked with the Apollo Client team and have an update: From the AC maintainers, this has been one of those issues where one group of people think the old behavior was a bug, but other groups of people think this new behavior is a bug, so we chose a path forward that at least has a solution for both groups, even if that requires code changes. See this PR for more info on the behavior: #10229 Adding Depending on what kind of logic is in your callback, another option could be to await the promise returned from refetch and have it perform whatever you need when it’s finished Instead of: const { refetch } = useQuery(QUERY, {
onCompleted: (result) => {
// do something with result
}
}) Do this: const { refetch } = useQuery(...)
<button
onClick={async () => {
const result = await refetch()
// do something with result
}
/> |
I have already mentioned here in August the linked topic with detailed answers regarding the nature of the error (and mentioned there #10229). Described by you approach is badly extendable if 2+ components will use one source data it'll be necessary to manage the data state with |
We are having the same issue but a little more nuanced. We have a query in a component on the page, say |
Hi all 👋🏻 as mentioned above, the current If you still believe there is a bug please open a new issue with a runnable reproduction that demonstrates the buggy behavior. |
Issue Description
After an investigation we found out that refetching (using refetch, polling, fetchMore) is not working properly in combination with the
onComplete
method. On version 3.7.17 it did, in the sense thatonComplete
was called when a refetch was done. On version 3.8.0 (but also 3.8.1) theonComplete
method doesn't get called anymore after a refetch was done. This problem occurs forrefetch
but also usingpollInterval
,fetchMore
and perhaps other methods that are refetching data.Link to Reproduction
https://codesandbox.io/p/sandbox/cool-paper-l7wlk2
Reproduction Steps
We've created reproductions in the codesandbox, that shows the problems between the Apollo client versions.
The text was updated successfully, but these errors were encountered: