You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Nuxt.js+Pinia中如何获取Apollo useQuery的结果?

在Pinia中使用@vue/apollo-composable的useQuery问题

我尝试使用@vue/apollo-composable包中的useQuery函数,但该函数不返回Promise,仅返回result、loading等ref对象,因此无法直接在Pinia store中使用这些数据。

当前我的代码如下:

fetchArticle: function (id: string) {
      // check if article is already in cache
      const cache = this.articles.find(a => a.id == id);
      if (cache && !cache.partial) return cache;
      // fetch article from server
      const { result: article } = useQuery<{ article: Article }>(GET_ARTICLE, { id });
      // update state, but... when `article` contains data? 
},

在store中,我不知道如何等待请求结束。我尝试将useQuery转换为返回Promise,但此方法无效,Nuxt.js在服务端出现冻结,代码如下:

fetchArticle: async function (id: string) {
      // check if article is already in cache
      const cache = this.articles.find(a => a.id == id);
      if (cache && !cache.partial) return cache;
      // fetch article from server
      const { onResult, result } = useQuery<{ article: Article }>(GET_ARTICLE, { id });
      const article = result.value?.article || (await new Promise(r => onResult(({ data }) => r(data.article))));
      if (!article) return;
      const data = { ...article, partial: false };
      this.articles = this.articles.map(a => (a.id == id ? data : a)) as Article[];
      // return article
      return article;
},

相关信息:

  • Store:Pinia
  • 版本:Nuxt 3.1.2;@vue/apollo-composable 4.0.0-beta.2

内容的提问来源于stack exchange,提问作者Raphael V.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 19:45:21