简体   繁体   English

阿波罗 Angular 服务

[英]Apollo Angular Services

Iam using grapqhl-codegen to convert my queries into injectable Services, Everything works like a charm, besides, I cannot updateQuerys anymore, since I only have services and I cannot pass the service into the updatesQuerys array.我正在使用 grapqhl-codegen 将我的查询转换为可注入的服务,一切都像魅力一样工作,此外,我不能 updateQuerys 了,因为我只有服务,我不能将服务传递到 updatesQuerys 数组中。 I want the userGql service to refetch the data, not use the cache:我希望 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));
  }```

If you wish to refetch a query after a mutation, you need to use refetcQueries .如果您希望在突变后重新获取查询,则需要使用refetcQueries updateQueries is used to manually update the cache (also is deprecated, see update instead) so it would not give you what you want. updateQueries用于手动更新缓存(也已弃用,请参阅update ),因此它不会为您提供所需的内容。

I know of at least two ways to use refetchQueries .我知道至少有两种使用refetchQueries的方法。

One is by query name, this will work when the query in question is being watched:一种是查询名称,这将在正在查看相关查询时起作用:

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
  })

Another option is to reference the service document.另一种选择是参考服务文档。 I believe this will force-fetch the query.我相信这将强制获取查询。

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
  })

Don't forget that variables also matter here.不要忘记变量在这里也很重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM