QQCif's Frozen Zone

Graphql更新本地缓存注意不要忘记参数

By QQCif

当用来更新本地缓存的查询有参数时,写入缓存也要带上参数。之前只在读取的时候加了variables, 结果界面老是不更新,writeQuery写入的时候也要带上variables.注意下面1和2.

this.$apollo.mutate({
  mutation: CREATE_POST_MUTATION,
  variables: {
    input: newPost
  },
  update: (proxy, { data: { createPost } }) => {
    const data = proxy.readQuery({
      query: POSTS_QUERY,
      // 1
      variables: {
        authorNickname: this.getCurrentUserProfile.nickname
      }
    })
  data.posts.push(createPost)
    console.log(data.posts)
    proxy.writeQuery({
      query: POSTS_QUERY,
      // 2
      variables: {
         authorNickname: this.getCurrentUserProfile.nickname
      },
      data
    })
}