简体   繁体   English

突变内refetch查询的Angular Apollo使用响应

[英]Angular Apollo use response from refetch query within a mutation

I'm working on an Angular project with Apollo and GraphQL. 我正在使用Apollo和GraphQL进行Angular项目。 I have a mutation that updates a list of accounts with relevant details associated with it. 我有一个变异,可使用相关的详细信息更新帐户列表。 After the successful mutation, I am using refetchqueries to query the API for updated list of accounts. 成功完成突变后,我正在使用refetchqueries查询API以获取更新的帐户列表。 Everything works till this part. 一切工作到这一部分。

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

The 'data' for the subscription returns response from the mutation but is there a way I could use the data returned by 'queryAccounts' which is also run as part of this mutation? 预订的“数据”返回突变的响应,但是有没有办法我可以使用“ queryAccounts”返回的数据呢?

There seems to be a way to do this in react but I was unsuccessful to do something similar in Angular. 似乎有一种方法可以做到这一点,但是我没有在Angular中做类似的事情。

You can have a watchQuery that will always update when refetched 您可以拥有一个watchQuery,该查询将在重新获取时始终更新

this.apollo.watchQuery({
            query: query,
            variables: variables
        })
        .valueChanges
        .pipe(
            map(res => res.data)
        )
        .subscribe(data => {
            //Updated your data here
        }, err => {

        })
       })

You mentioned there are ways to do it in react and which I don't think is true. 您提到了可以通过反应来做到这一点的方法,但我认为这是不正确的。

Let me explain how mutations work in Apollo. 让我解释一下在Apollo中突变是如何工作的。

  1. You run apollo.mutate() with refetchQueries option 您使用refetchQueries选项运行apollo.mutate()
  2. When you subscribe to it, the HTTP request is made (in most cases, sometimes WS or some other transportation layer) 订阅时,将发出HTTP请求(在大多数情况下,有时是WS或某些其他传输层)
  3. Apollo receives the response from the server. Apollo从服务器收到响应。
  4. Updates the store. 更新商店。
  5. Observable you subscribed to emits the result (your console.log(data) ) 您订阅的Observable发出结果(您的console.log(data)
  6. Next, each query specified in refetchQueries starts a new HTTP request and runs against the server. 接下来,在refetchQueries指定的每个查询refetchQueries启动一个新的HTTP请求并针对服务器运行。
  7. Apollo updates the store on each response. Apollo会根据每个响应更新商店。
  8. Components that listen to those queries get updated results 侦听这些查询的组件将获得更新的结果

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

Basically, refetchQueries is not to fetch or access data. 基本上,refetchQueries不会获取或访问数据。

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

相关问题 如何使用 Apollo Angular 测试查询、变异、订阅服务? - How cant test Query, Mutation, Subscription services using Apollo Angular? Apollo + Angular 突变不适用于变量 - Apollo + Angular mutation is not working with variables 何时在 Apollo-Angular 中使用 watchQuery 或 query? - When to use watchQuery or query in Apollo-Angular? 在 Angular 中动态创建 Apollo GraphQL 突变调用 - Dynamically creating Apollo GraphQL mutation call in Angular Graphql - 使用 Apollo 客户端 Angular 的突变操作不起作用 - Graphql - Mutation operation with Apollo Client Angular not working 如何在Angular 5中使用GET方法使用Apollo graphql查询 - how to use apollo graphql query using GET method in angular 5 AG_Grid,Angular-Apollo,来自Apollo查询的数据无法为网格生成rowdata - AG_Grid, Angular-Apollo, data from Apollo query fails to generate rowdata for grid 从阿波罗角度上传阿波罗服务器文件 - Apollo server file upload from apollo angular Apollo 客户端 Angular:如何将从查询中获得的数据作为参数传递给 graphql 中的另一个查询? - Apollo Client Angular: how to pass the data obtained from a query as an argument in another query in graphql? Angular Apollo:错误:GraphQL 错误:“Mutation”类型的“AuthLogin”字段上的未知参数“用户名” - Angular Apollo: Error: GraphQL error: Unknown argument “username” on field “AuthLogin” of type “Mutation”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM