【问题标题】:Apollo Angular Services阿波罗角服务
【发布时间】:2020-10-18 07:18:03
【问题描述】:

我正在使用 grapqhl-codegen 将我的查询转换为可注入的服务,一切都像一个魅力,此外,我不能 updateQuerys 了,因为我只有服务,我不能将服务传递到 updatesQuerys 数组。我希望 userGql 服务重新获取数据,而不是使用缓存:

loginAttempt() {
    const data = this.loginForm.value;
    this.loginGql
      .mutate({ data }, {updateQueries: [CANT ADD SERVICE HERE]})
      .pipe(
        pluck("data", "login"),
        switchMap(async (token: string) => {
          await this.storage.setToken(token);
          return this.userGql.fetch().toPromise();
        })
      )
      .subscribe((res) => console.log("res: ", res));
  }```

【问题讨论】:

    标签: angular typescript graphql graphql-codegen


    【解决方案1】:

    如果您希望在突变后重新获取查询,您需要使用refetcQueries。 updateQueries 用于手动更新缓存(也已弃用,请参阅 update),因此它不会给您想要的。

    我知道至少有两种使用refetchQueries的方法。

    一个是查询名称,这将在正在查看相关查询时起作用:

    this.loginGql
      .mutate({ data }, { refetchQueries: ["UserQuery"] }) // <-- this
      .pipe(
        pluck("data", "login")
      )
      .subscribe((token: string) => {
        this.storage.setToken(token);
        // ... no need to manually fetch the query here
      })
    

    另一种选择是参考服务文档。我相信这会强制获取查询。

    this.loginGql
      .mutate(
        {
          data
        },
        {
          refetchQueries: {
            query: this.faqQuestionsQuery.document  // <-- this
          }
        }
      )
      .pipe(
        pluck("data", "login")
      )
      .subscribe((token: string) => {
        this.storage.setToken(token);
        // ... no need to manually fetch the query here
      })
    

    不要忘记变量在这里也很重要。

    【讨论】:

      猜你喜欢
      • 2021-09-09
      • 2019-02-08
      • 2019-03-06
      • 2021-09-17
      • 2020-10-04
      • 2021-10-28
      • 2019-01-31
      • 2013-02-02
      • 2021-12-28
      相关资源
      最近更新 更多