Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wait for useLazyQuery response

I need to call a query when submit button is pressed and then handle the response.

I need something like this:

const [checkEmail] = useLazyQuery(CHECK_EMAIL)
const handleSubmit = async () => {
  const res = await checkEmail({ variables: { email: values.email }})
  console.log(res) // handle response
}

Try #1:

const [checkEmail, { data }] = useLazyQuery(CHECK_EMAIL)
const handleSubmit = async () => {
  const res = await checkEmail({ variables: { email: values.email }})
  console.log(data) // undefined the first time
}

Thanks in advance!

like image 824
Julián Gómez Sibecas Avatar asked May 31 '20 21:05

Julián Gómez Sibecas


1 Answers

This works for me:

const { refetch } = useQuery(CHECK_EMAIL, {
  skip: !values.email
})

const handleSubmit = async () => {
  const res = await refetch({ variables: { email: values.email }})
  console.log(res)
}
like image 90
kurtko Avatar answered Oct 19 '22 23:10

kurtko