score:2

Accepted answer
// Add `userError` to use in combination with `user` to check if the query is loading
const { data: user, error: userError } = useSWR('/api/user', userFetcher)

const [
  createForm,
  { data: createFormData, error: createFormError, loading: createFormLoading },
] = useMutation(ADD_FORM, {
  refetchQueries: [{ query: FORMS }],
})

const addFormClicked = async (data) => {
  const res = await createForm({
    context: {
      headers: {
        authorization: `Bearer ${cookieData}`,
      },
    },
    variables: {
      name: data.name,
      user: user.id,
    },
  })
  Router.push(`/formBuild/${res.data.createForm._id}`)
}

if (userError || cookieError) {
  return <div>Something went wrong</div>
}

if (!user || !cookieData) {
  return <div>Loading...</div>
}

// Render form

Related Query

More Query from same tag