nuxt-modules / apollo

Nuxt.js module to use Vue-Apollo. The Apollo integration for GraphQL.
https://apollo.nuxtjs.org
MIT License
955 stars 198 forks source link

`useAsyncQuery` return value null when press F5 refresh browser. #633

Closed toknT closed 2 months ago

toknT commented 2 months ago

Environment

Nuxt project info: 11:57:42


Describe the bug

I am using nuxtjs+apollo creating admin panel .Using useLazyAsyncQuery to loading the table data.However if you press f5 refresh browser the table data will be empty.

// ============ findAll
const pageSize = 11;
const page = ref(1)
const tableData = ref<PaginatedSystemSetting>(emptyPaginatedSystemSetting)
const { data, status } = await useLazyAsyncData(async () => {
  await handleCurrentPageChange(1);
})
const handleCurrentPageChange = async (val: number) => {
  page.value = val;
  const loading = ElLoading.service({ fullscreen: true })
  const { data } = await useAsyncQuery<{ data: PaginatedSystemSetting }>(findAllQuery, {
    input: { page: page.value, perPage: pageSize }
  });
  console.debug('useAsyncQuery', data.value);
  if (data.value) {
    tableData.value = data.value.data
  }
  setTimeout(() => { loading.close(); }, 300);
}

Expected behaviour

UseLazyAsyncQuery be fired when navigation back in browser.

Reproduction

No response

Additional context

No response

Logs

No response

toknT commented 2 months ago

Finally I found if using f5 refresh page useQuery will fire onResult twice . So change code like this solve it.

const pageSize = 11;
const page = ref(1)
const tableData = ref<PaginatedSystemSetting>(emptyPaginatedSystemSetting)
const { data, status } = await useLazyAsyncData(async () => {
  await handleCurrentPageChange(1);
})
const handleCurrentPageChange = async (val: number) => {
  page.value = val;
  const loading = ElLoading.service({ fullscreen: true })
  useQuery<{ data: PaginatedSystemSetting }>(findAllQuery, {
    input: { page: page.value, perPage: pageSize },
  }, {
    fetchPolicy: 'network-only',
    prefetch: false,
  }).onResult((value) => {
    console.debug('onResult', value)
    if (value.loading == false) {
      tableData.value = value.data.data;
      setTimeout(() => { loading.close(); }, 300);
    }
  });
}

onresult-twice