'How to make a GraphQL lazy query with Apollo client after the first query is successful

I'm trying to query a GQL endpoint using Apollos useLazyQuery hook just after an initial query is successful. I thought I could easily do this by just checking to see if the "data" variable is returned successfully by the first query, then just making the next request there, but I'm getting an error, here's what my code looks like:

  const [getUserByUsername, { data, loading, error }] = useLazyQuery(GET_USER_BY_USERNAME);

  const [getReachRelationship, {data, loading, error}] = useLazyQuery(GET_REACH_RELATIONSHIP);

here's the error I keep getting "Cannot redeclare block-scoped variable"



Solution 1:[1]

The problem is that you are redeclaring data loading and error twice.

const [getUserByUsername, { data: user, loading: isUserLoading, error: isUserError }] = useLazyQuery(GET_USER_BY_USERNAME);
const [getReachRelationship, {data: reachRelationship, loading: isReachRelationshipLoading, error: isReachRelationshipError}] = useLazyQuery(GET_REACH_RELATIONSHIP);

console.log(user)
console.log(reachRelationship)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 KnowYourElements