阿波罗客户端3:如何将突变结果缓存到嵌套集合中? (集合中的集合)

发布于 2025-01-31 03:04:01 字数 1839 浏览 2 评论 0原文

在我的Apollo Client 3应用程序中,我正在进行突变,并希望将结果缓存到嵌套在集合中的集合中。

具体来说,我在注释列表中,帖子中的每个列表,帖子列表中的每个帖子中创建评论。我的应用程序的数据层次结构看起来像:

user 1
    profile 1
        post 1
            comment 1.1
            comment 1.2
        post 2
            comment 2.1
            comment 2.2
            < write mutation result here >
        post 3
            comment 3.1
            comment 3.2
            comment 3.3
        ...

在这种情况下,我将如何在其父帖的评论收集中最好地缓存创建的评论?我正在查看usemuont钩子的更新修改 config,但不太确定。

对于其他上下文,这里是与上述数据层次结构相对应的查询:

query getUserPosts($userParams: GetUserParams!$postsPaginationParams: CursorPaginationParams!) {
  user(params: $userParams) {
    id
    profile {
      id
      # ...
      ownedPosts(pagination: $postsPaginationParams) {
        items {
          id
          # ...
          featuredComments {
            id
            primaryText
            creationTimestamp
            owner {
              id
              name
            }
          }
        }
        pagination {
          # ...
        }
      }
    }
  }
}

这是我的突变:

input CreateCommentParams {
  ownerId: String!
  postId: String!
  primaryText: String!
}

mutation createComment($params: CreateCommentParams!) {
  createComment(params: $params) {
    id
    owner {
      id
      name
    }
    primaryText
    creationTimestamp
  }
}

这是usemution到目前为止的内容:

useMutation(CREATE_COMMENT_MUTATION, {
  // ...
  update: (cache, { data }) => {
    if (data) {
      const cacheId = cache.identify(data.createComment);
      cache.modify({
        fields: {
          // ...how to update the comments array of the specific post? 
        }
      })
    }
  },
})

In my Apollo Client 3 app, I am doing a mutation and want to cache the result into a collection which is nested within an item of a collection.

Specifically, I am creating a comment within a list of comments, each list within a post, each post within a list of posts. My app's data hierarchy looks like:

user 1
    profile 1
        post 1
            comment 1.1
            comment 1.2
        post 2
            comment 2.1
            comment 2.2
            < write mutation result here >
        post 3
            comment 3.1
            comment 3.2
            comment 3.3
        ...

In this situation, how would I best cache a created comment into its parent post's comment-collection? I am looking at the useMutation hook's update or modify config, but am not too sure.

For additional context, here is query that corresponds to the above data hierarchy:

query getUserPosts($userParams: GetUserParams!$postsPaginationParams: CursorPaginationParams!) {
  user(params: $userParams) {
    id
    profile {
      id
      # ...
      ownedPosts(pagination: $postsPaginationParams) {
        items {
          id
          # ...
          featuredComments {
            id
            primaryText
            creationTimestamp
            owner {
              id
              name
            }
          }
        }
        pagination {
          # ...
        }
      }
    }
  }
}

And here is my mutation:

input CreateCommentParams {
  ownerId: String!
  postId: String!
  primaryText: String!
}

mutation createComment($params: CreateCommentParams!) {
  createComment(params: $params) {
    id
    owner {
      id
      name
    }
    primaryText
    creationTimestamp
  }
}

And here is what the useMutation is so far:

useMutation(CREATE_COMMENT_MUTATION, {
  // ...
  update: (cache, { data }) => {
    if (data) {
      const cacheId = cache.identify(data.createComment);
      cache.modify({
        fields: {
          // ...how to update the comments array of the specific post? 
        }
      })
    }
  },
})

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦情居士 2025-02-07 03:04:01

您需要查找您正在更新和更新其特色的字段的帖子:

useMutation(CREATE_COMMENT_MUTATION, {
  // ...
  update: (cache, { data }) => {
    cache.modify({
      id: cache.identify({
        __typename: 'Post', // Assuming your this is the _typename of your Post type in your schema
        id: postId,
      }),
      fields: {
        featuredComments: (previous, { toReference }) => [...previous, toReference(data.createComment)]
      },
    }),
  }),
})

You need to find the Post you are updating and update its featuredComments field like so:

useMutation(CREATE_COMMENT_MUTATION, {
  // ...
  update: (cache, { data }) => {
    cache.modify({
      id: cache.identify({
        __typename: 'Post', // Assuming your this is the _typename of your Post type in your schema
        id: postId,
      }),
      fields: {
        featuredComments: (previous, { toReference }) => [...previous, toReference(data.createComment)]
      },
    }),
  }),
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文