我正在使用 postsConnection
查询进行无限滚动。它包含像 after
这样的变量。
进行投票突变后,我想refetchQueries
...就像这样👇
const upvote = await client.mutate({
mutation: UPVOTE_MUTATION,
variables: {
postId: this.props.post.id
},
refetchQueries: [
{ query: POST_AUTHOR_QUERY }
]
})
上面的代码给出错误,因为POST_AUTHOR_QUERY
接受很少的变量。这是该查询👇
export const POST_AUTHOR_QUERY = gql`
query POST_AUTHOR_QUERY($authorUsername: String! $orderBy: PostOrderByInput $after: String){
postsAuthorConnection(authorUsername: $authorUsername orderBy: $orderBy after: $after) {
....
}
}
我不想手动添加变量。变量已经存储在缓存中。使用 refetchQueries
时如何重用它们???
以下是我读过的有关此问题的一些资源👇
最佳答案
如 the issue you linked 中所述,您应该能够执行以下操作:
import { getOperationName } from 'apollo-link'
const upvote = await client.mutate({
// other options
refetchQueries={[getOperationName(POST_AUTHOR_QUERY)]}
})
来自docs :
Please note that if you call refetchQueries with an array of strings, then Apollo Client will look for any previously called queries that have the same names as the provided strings. It will then refetch those queries with their current variables.
getOperationName
只是解析您传递给它的文档并从中提取操作名称。当然,您可以自己将操作名称作为字符串提供,但这种方式可以避免操作名称将来更改或您误操作时出现的问题。
关于javascript - 在react-apollo中使用refetchQueries时使用现有变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55638965/