Angular Apollo 使用来自突变内重新获取查询的响应

Ven*_*kat 6 apollo graphql angular

我正在使用 Apollo 和 GraphQL 进行 Angular 项目。我有一个突变,它更新了一个帐户列表,其中包含与之相关的相关详细信息。成功突变后,我使用 refetchqueries 查询 API 以获取更新的帐户列表。一切正常,直到这部分。

this.apollo.mutate({
        mutation: mutationCreateNewAccount,
        variables: {
            accountNumber: this.accountNumber,
            accountType: this.accountType,
            routingNumber: this.routingNumber,
            nameOfAcountHolder: this.name
        },
        refetchQueries: [{
            query: queryAccounts,
            variables: { accountNumber: this.accountNumber }
        }]}).subscribe(({ data }) => console.log(data),
Run Code Online (Sandbox Code Playgroud)

订阅的“数据”从突变返回响应,但有没有办法使用“queryAccounts”返回的数据,它也作为此突变的一部分运行?

在反应中似乎有一种方法可以做到这一点,但我没有成功在 Angular 中做类似的事情。

小智 2

你提到有很多方法可以在反应中做到这一点,但我认为这不是真的。

让我解释一下 Apollo 中突变是如何工作的。

  1. apollo.mutate()带着refetchQueries选项运行
  2. 当您订阅它时,会发出 HTTP 请求(在大多数情况下,有时是 WS 或其他一些传输层)
  3. Apollo 接收来自服务器的响应。
  4. 更新商店。
  5. 您订阅的 Observable 会发出结果(您的console.log(data)
  6. 接下来,中指定的每个查询refetchQueries都会启动一个新的 HTTP 请求并针对服务器运行。
  7. Apollo 会根据每个回复更新商店。
  8. 监听这些查询的组件会获取更新的结果

我在文档中对此进行了解释:https ://www.apollographql.com/docs/angular/features/cache-updates.html#after-mutations

基本上,refetchQueries 不是获取或访问数据。