apollographql / apollo-client

:rocket:  A fully-featured, production ready caching GraphQL client for every UI framework and GraphQL server.
https://apollographql.com/client
MIT License
19.4k stars 2.66k forks source link

useLazyQuery does not fire onCompleted when being used with "no-cache" and "network-only" fetchPolicy #11327

Open pt-hieu opened 1 year ago

pt-hieu commented 1 year ago

Issue Description

Hi Apollo Client team, I am running into an issue where onCompleted is not fired when running the execute function of useLazyQuery with fetchPolicy set to no-cache or network-only

const [execute] = useLazyQuery(
    gql`
      query Test($id: String!) {
        node(id: $id) {
          ... on Train {
            passengers {
              totalCount
            }
          }
        }
      }
    `,
    { client: apolloClient, variables: { id: "1" }, fetchPolicy: "no-cache" }
  );

  useEffect(() => {
    execute({
      onCompleted() {
        console.log("Hello world");
        alert("Hello world");
      },
    }).then((data) => {
      console.log("Hello from promise", data);
    });
  }, []);

The above snippet is from the reproduction repo, the expected behaviour would be to see an alert dialog appearing (because onCompleted gets called) but there's only 2 "Hello from promise" logs in console which means there's actual data returned

Note

Link to Reproduction

https://github.com/pt-hieu/apollo-lazy-oncompleted-reproduce

Reproduction Steps

bignimbus commented 1 year ago

Hi @pt-hieu 👋🏻 thanks for reporting this! Was there a version of Apollo Client you used where this was working?

pt-hieu commented 1 year ago

Thanks for replying @bignimbus, I am not sure about this but we previously used 3.7.14 and then upgraded the package to 3.8.4, one thing that I don't really understand is how our production does not have this bug