[英]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中突变是如何工作的。
apollo.mutate()
with refetchQueries
option refetchQueries
选项运行apollo.mutate()
console.log(data)
) console.log(data)
) refetchQueries
starts a new HTTP request and runs against the server. refetchQueries
指定的每个查询refetchQueries
启动一个新的HTTP请求并针对服务器运行。 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.