Home > OS >  How to make a GraphQL lazy query with Apollo client after the first query is successful
How to make a GraphQL lazy query with Apollo client after the first query is successful

Time:04-27

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"

CodePudding user response:

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)
  • Related