Apollo-client: Apollo Client 3: Browser becomes unresponsive when using useLazyQuery

Created on 17 Jul 2020  路  3Comments  路  Source: apollographql/apollo-client

I migrated the code to Apollo Client 3. At one point I've used useLazyQuery to fetch results. When a user selects an option from a dropdown field, the app fetches another data based on previous selection. It was working fine with previous version of Apollo Client.
Now, the browser becomes unresponsive right after I select an option from the dropdown.

Client configuration is simple.

Apollo 2:

import { ApolloClient } from "apollo-client";
import { createHttpLink } from "apollo-link-http";
import { InMemoryCache } from "apollo-cache-inmemory";

const httpLink = createHttpLink({
  uri: `${process.env.REACT_APP_SERVER_URL}`,
});

const client = new ApolloClient({
  link: httpLink,
  cache: new InMemoryCache(),
});

export default client;

Apollo 3:

import { ApolloClient, InMemoryCache } from "@apollo/client";

const client = new ApolloClient({
  uri: `${process.env.REACT_APP_SERVER_URL}`,
  cache: new InMemoryCache(),
});

export default client;

useLazyQuery implementation looks like this:

const [loadCaseDetails, { loading: caseDetailsLoading }] = useLazyQuery(
    CASE_DETAILS,
    {
      onCompleted: (d) => {
        setGenes([...d.caseDetails.genes]);
        setMutations([...d.caseDetails.mutations]);
      },
    },
  );

Anyone having issue like this after migrating?

Most helpful comment

We're not quite ready to publish @apollo/[email protected] yet, but I've published an @apollo/[email protected] release that includes #6588, which fixes some problems with onCompleted (and onError), if you want to try that in the meantime. 馃檹

All 3 comments

why do you use setters, you can simply take the data which then updates the rest, no?

const [loadCaseDetails, { data: caseDetailsData, loading: caseDetailsLoading }] = useLazyQuery

since version 3 data becomes undefined when you change variables on the query, see https://github.com/apollographql/apollo-client/issues/6603 and updates after getting the results

@maapteh Thanks. It solved the issue. I removed the onCompleted and instead used data directly.

We're not quite ready to publish @apollo/[email protected] yet, but I've published an @apollo/[email protected] release that includes #6588, which fixes some problems with onCompleted (and onError), if you want to try that in the meantime. 馃檹

Was this page helpful?
0 / 5 - 0 ratings