javascript - 在react-apollo中使用refetchQueries时使用现有变量

标签 javascript graphql apollo react-apollo prisma-graphql

我正在使用 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 时如何重用它们???

以下是我读过的有关此问题的一些资源👇

https://github.com/apollographql/react-apollo/issues/817

https://github.com/apollographql/apollo-client/issues/1900

最佳答案

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/

相关文章:

python - 如何为 graphql 查询配置 JSON?

reactjs - '类型错误: parse is not a function' when using gql

javascript - 如何在 Apollo 中使用 readFragment 中的数据?

javascript - 在 AngularJS/ngAnimate 中显示/隐藏时向左/向右滑动动画

javascript - 范围未在 http.get 的成功函数内更新 UI

file - 有没有办法通过 postman 将文件上传到 GraphQL API?

caching - 寻求帮助了解 Apollo Client 本地状态和缓存

express - 如何解决 GraphQL 订阅服务器中的 webSocket 错误

javascript - 根据所选选项更改同一选择中的选项

javascript - Laravel 使用 onchange 隐藏表单