[英]GraphQL refetchQueries after mutation, this.props not updated
This problem makes no sense to me at all... I am adding an author to a database using a mutation like this:这个问题对我来说毫无意义......我正在使用这样的突变将作者添加到数据库中:
this.props
.addAuthorMutation({
variables: {
name: this.state.name,
age: this.state.age
},
refetchQueries: [
{
query: getAuthorsQuery,
variables: {
awaitRefetchQueries: true
}
}
]
})
.then(() => this.submitBookAlso());
Then simply...那么简单...
submitBookAlso() {
console.log(this);
console.log(this.props);
}
For some mysterious reason, when I inspect (this) in the console, this.props.getAuthorsQuery.authors contains my added author, however, (this.props) in the console does not.出于某种神秘的原因,当我在控制台中检查 (this) 时,this.props.getAuthorsQuery.authors 包含我添加的作者,但是,控制台中的 (this.props) 没有。 The new author is added to a dropdown in another component successfully, and is in the database, but I cannot seem to get the author after the mutation.
新作者已成功添加到另一个组件的下拉列表中,并且在数据库中,但我似乎无法在突变后获得作者。 I would like to filter it...
我想过滤它...
let author = this.props.getAuthorsQuery.authors.filter(
author => this.state.name === author.name
);
console.log(author);
...but this just gives me an empty array. ...但这只是给了我一个空数组。 What I am wanting to do is trigger another mutation when that one is finished (adding a book to a separate database collection with the new added author as it's author).
我想要做的是在完成另一个突变时触发另一个突变(将一本书添加到单独的数据库集合中,新添加的作者作为作者)。
I get the same result without using awaitRefetchQueries, and also using onCompleted instead of.then().我在不使用 awaitRefetchQueries 的情况下得到相同的结果,并且还使用 onCompleted 而不是 .then()。 These are my current dependencies:
这些是我当前的依赖项:
"dependencies": {
"apollo-boost": "^0.1.16",
"graphql": "^14.0.2",
"react": "^16.5.2",
"react-apollo": "^2.2.4",
"react-dom": "^16.5.2",
"react-scripts": "2.0.3"
},
Thanks!谢谢!
Try passing awaitRefetchQueries
in directly to the mutation and not nested inside of refetchQueries
like this: 尝试将
awaitRefetchQueries
直接传递给突变,而不是像这样嵌套在refetchQueries
内部:
this.props
.addAuthorMutation({
variables: {
name: this.state.name,
age: this.state.age
},
awaitRefetchQueries: true,
refetchQueries: [
{
query: getAuthorsQuery
}
]
})
.then(() => this.submitBookAlso());
// Pass Refetch when declaring useQuery example in hooks // 在钩子中声明 useQuery 示例时传递 Refetch
const {data, error, loading, refetch} = useQuery("Your query")
const [addAuthorMutation] = useMutation("Your Mutation",{
variables: { "your variables"},
onCompleted: (data) => { "dispatch your action creators"},
refetch()
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.