阿波罗突变取决于for循环中的先前突变

发布于 2025-01-30 06:17:46 字数 987 浏览 4 评论 0原文

我正在使用React Native和Apollo Client作为一个应用程序,该应用程序创建了一个聊天,给定了一系列选定的用户。我的代码看起来像这样:

        const [addUser, {
    data: userAdded, loading: addingUsers, error: errorAddingUsers,
  }] = useMutation(ADDUSERTOCHAT)

  const [makeChat, {
    data: chat, loading: chatLoading, error: chatError,
  }] = useMutation(NEWCHAT, {
    variables: { ownerId: viewerId },
    onCompleted: () => {
      for (let i = 0; i < selectedFriends.length; i++) {
        addUser({
          variables: {
            chatId: chat.newChat.id,
            id: selectedFriends[i].id,
          },
          onCompleted: () => {
            if (i === selectedFriends.length - 1) {
              navigation.navigate('Chat', { chatId: chat.newChat.id })
            }
          },
        })
      }
    },
  })

现在,这不起作用。我不确定仅在创建聊天之后才能运行Adduser突变,而且我也不确定FO循环是否会为每个选定的朋友运行一个突变。一旦完成过程中的所有操作,我还需要导航到屏幕聊天,我不确定我的状况是否会为此工作。总而言之,我对如何对这些突变进行测序并且无法使其起作用有些迷失。感谢任何帮助,感谢!

I'm using react native and apollo client for an app that creates a chat given an array of selected users. My code looks like this:

        const [addUser, {
    data: userAdded, loading: addingUsers, error: errorAddingUsers,
  }] = useMutation(ADDUSERTOCHAT)

  const [makeChat, {
    data: chat, loading: chatLoading, error: chatError,
  }] = useMutation(NEWCHAT, {
    variables: { ownerId: viewerId },
    onCompleted: () => {
      for (let i = 0; i < selectedFriends.length; i++) {
        addUser({
          variables: {
            chatId: chat.newChat.id,
            id: selectedFriends[i].id,
          },
          onCompleted: () => {
            if (i === selectedFriends.length - 1) {
              navigation.navigate('Chat', { chatId: chat.newChat.id })
            }
          },
        })
      }
    },
  })

Right now, this does not work. I am not sure how to run the addUser mutation only after the chat is created, and I'm also not sure if the for loop will work to run a mutation for every selected friend. I also need to navigate to the screen chat once everything in the process is done and I'm not sure if the condition I have will work for that. In sum, I'm a bit lost with how to sequence these mutations and can't get it to work. Any help is appreciated thanks!

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文