andres martinez
andres martinez

Reputation: 1439

how await a hook after execute a query in apollo Gql and REACT?

i have the follow problem, i want to execute a query that need and ID user, but i have consult the ID user from a hook, the code:

export default function useGetDatosPersonales() {
  
    const [viewer, refetchViewer] =  useViewer();
    const {data} = useQuery(qUsuario,{
        variables: {
            user: viewer && viewer.id
        }
    });
    const functionCall = (render) => { 
       if(data !== null && data !== undefined){
           console.log(data.user.email);
        }
    }

    return [functionCall,data];
    
}

the query qUsuario is:

query qUsuario($user:ID!){
  user(id:$user){
    email,
    firstName,
    lastName,
  }
}

but in the first time i got the follow error:

[GraphQL error]: Variable "$user" of required type "ID!" was not provided.

and then in few milliseconds later, the query works!

How i can say to the apollo that wait for the id before execute the query

Upvotes: 1

Views: 10802

Answers (1)

Dmitry S.
Dmitry S.

Reputation: 4524

You need to use useLazyQuery, because useQuery don't wait never.

Or you may use async-await syntax for use client instance directly and have full control under asynchronous.

Upvotes: 4

Related Questions